I have the following class which I want to serialize to XML:
<Serializable()> _
Public Class Settings
Public Shared var1 As Boolean = False
Public var2 As Boolean = False
End Class
I create a new instance and serialize it using my own method...
SaveSerialXML(PathToFile, New Settings, GetType(Settings))
...however the shared (static) variable is not included in the output:
<?xml version="1.0" encoding="utf-8"?>
<Settings>
<var2>false</var2>
</Settings>
Does anyone know of a way to serialize shared members of a class to XML?
Serialization is about serializing instances. Shared variables do not belong to an instance.
You can cheat add a redirect instance property for serialization.
For example:
Public Property Var1Instance As Boolean
Get
Return Var1
End Get
Set(value As Boolean)
Var1 = value
End Set
End Property
I've often found myself wanting to implement my own Xml serialization and not use the .NET XmlSerializer.
I do so by adding an Xml property to my class that directly handles the serialization via Xml Literals. That property could be a String or an XElement depending on how I'm going to use it.
Here's how the Xml String property would look for your sample class:
Public Property Xml() As String
Get
Return <Settings>
<var1><%= var1 %></var1>
<var2><%= var2 %></var2>
</Settings>.ToString
End Get
Set(ByVal value As String)
Dim xValue = XElement.Parse(value)
var1 = xValue...<var1>.Value = "true"
var2 = xValue...<var2>.Value = "true"
End Set
End Property
This places all the serialization logic in one place and avoids the use of attributes to control how an instance gets serialized. The other benefit I've found is that this allows me to deserialize a class that does not have a default constructor.
In production scenarios I would add an extension method that converts a String to a Boolean instead of comparing to "true" since valid Xml Boolean values include true, false, 1 and 0.
Related
I want to save a communication inside a class. After that I plan to serialize the class o a XML file, where all datapoints are decoded between a tag.
Therefore I want to explain my communication protocol first.
The message Frame Looks like the following
LIE01
LIE02
When the communication ends, I have around 3000 of this telegrams inside a raw variable.
Here I describe the Messages:
LIE01: Header + 1 data word
LIE02: Header + 2 data words
My idea was to decode the frame and save it in a list (or array) of structures that are public properties of my class.
Public Class Com
Public Structure sLIE01
Public Property Header As Int16
Public Property data1 As Int16
End Structure
Public Structure sLIE02
Public Property Header As Int16
Public Property data1 As Int16
Public Property data2 As Int16
End Structure
Public Property LIE01 As List(Of sLIE01)
Get
?
End Get
Set(ByVal value As List(Of sLIE01))
?
End Set
End Property
Public Property LIE02 As List(Of sLIE02)
Get
?
End Get
Set(ByVal value As List(Of sLIE02))
?
End Set
End Property
End Class
Unfortunatelly I am more a beginner than an expert, so that I have no idea, how to write the code to Set or Get a specific LIE message.
Even I'm not sure, whether my way is a common way for this purpose or not.
You could use auto implemented properties in your code and skip getters and setters altogether (https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/auto-implemented-properties). You'll then be able to assign lists to them like:
Dim newList as new List(of sLIE01)()
ComInstance.Lie01 = newList
You can also operate on those list properties directly (just make sure you initialize them in class constructor to avoid NullReferenceException):
Dim lie as sLie01
ComInstance.Lie01.Add(lie)
Also consider replacing structures with classes: https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/structures-and-classes
If you still want to use Get Set it would look like this...
Private _LIE01 As List(Of sLIE01)
Public Property LIE01 As List(Of sLIE01)
Get
Return _LIE01
End Get
Set(value as List(Of sLIE01))
_LIE01 = value
End Set
End Property
Been working a lot with custom classes lately and I love the power you can have with them but I have come across something that I'm not able to solve and/or find anything helpful online.
I have a list of a class with properties I'm looking to only store information pulled from a database into.
Public Class CustomClass
Public _Values As String
Public _Variables As String
Public ReadOnly Property Values() As String
Get
Return _Values
End Get
End Property
Public ReadOnly Property Variables() As String
Get
Return _Variables
End Get
End Property
Sub New(ByVal values As String, ByVal variables As String)
_Values = values
_Variables = variables
End Sub
End Class
I will be iterating through some database entries, and I'm looking to store them into the appropriate property when I hit them (since I won't have them all available immediately, which is part of my problem). I want to just be able to add either the value or the variable at a time and not both of them, but since I have the sub procedure 'New' passing two arguments, it will always require passing them both. I've found the only way around this is by making them optional fields which I don't feel is the right way to solve this. Is what I'm looking to do possible with a class or would it be simpler by using a structure?
You can overload the constructor:
Friend Class Foo
' using auto-implement props:
Public Property Name As String ' creates a _Name backing field
Public Property Value as Integer
Public Sub New(newN as String, newV as Integer)
' access "hidden" backing fields if you want:
_Name = newN
_Value = newV
End Sub
Public Sub New() ' simple ctor
End Sub
Public Sub New(justName As String)
' via the prop
Name = justName
End Sub
End Class
You now have 3 ways to create the object: with full initialization, partial (name only) or as a blank object. You will often need a "simple constructor" - one with no params - for other purposes: serializers, Collection editors and the like will have no idea how to use the parameterized constructors and will require a simple one.
If rules in the App were that there was no reason for a MyFoo to ever exist unless both Name and Value being defined, implementing only the New(String, Integer) ctor enforces that rule. That is, it is first about the app rules, then about coding convenience.
Dim myFoo As New Foo ' empty one
myFoo.Name = "ziggy" ' we only know part of it
Since the default of string is nothing, you could pass nothing for the value you don't have. IE
Collection.Add(New CustomClass("My Value",Nothing))
Every type has a default, so this works with more than just strings.
How to change VB.NET T4 template under Entity Framework 6 so that instead it generating a property
Public Property name As String
It would generate
Public Property name As String
Get
Return _name
End Get
Set(ByVal value As String)
[Set](_name , value, "name")
End Set
End Property
Private _name As String
I don't know if things are changed inEF 6 as I have started using Code first approach.
What you can do is if it is EF will generate partial class for you, you can extend the same and define your own getter and setter.
But why would you like to do it as in your case set and get is public?
What benefit are you trying to achieve.
I have successfully deserialized my xml file using XmlSerializer in .Net but trying to serialize that data back to an xml file is becoming frustrating. When I try to serialize my classes I only get the root tag of the xml with no child elements. How can I serialize all my objects to get the correct xml with data? I have seen somewhere where someone suggested to add the classes to be serialized in a collection and then serialize that collection but I can't wrap my head around that or is there a simpler way of doing it? Any help is appreciated! Here is my code:
Public Shared Function SerializeXml() As Byte()
Dim serializer As New XmlSerializer(GetType(Data))
Dim nameSpaces As XmlSerializerNamespaces = New XmlSerializerNamespaces()
Dim mStream As New MemoryStream()
Dim result As Byte()
Dim target As New Data()
nameSpaces.Add(String.Empty, String.Empty)
serializer.Serialize(mStream, target, nameSpaces)
result = mStream.ToArray()
Return result
And here is a generic sample of the xml with attributes:
<?xml version"1.0">
<RootTag>
<ChildTag Label="Label1" Value="Value1"/>
<ChildTag Label="Label2" Value="Value2"/>
</RootTag>
Edit: Here is my Data class:
Imports System.Xml.Serialization
<XmlRoot("DATA", [Namespace]:="", IsNullable:=False)>
Public Class Data
Inherits Model
<XmlElement("CONFIGURATION")>
Public Property Configuration() As DataConfiguration
Get
Return Me._Configuration
End Get
Set(value As DataConfiguration)
Me._Configuration = value
End Set
End Property
Private _Configuration As DataConfiguration
<XmlElement("FIELD")>
Public Property Field() As Field
Get
Return Me._Field
End Get
Set(value As Field)
Me._Field = value
End Set
End Property
Private _Field As Field
<XmlElement("LIST")>
Public Property ListRoot() As List(Of ListRoot)
Get
Return Me._ListRoot
End Get
Set(value As List(Of ListRoot))
Me._ListRoot = value
End Set
End Property
Private _ListRoot As List(Of ListRoot)
End Class
This is your issue here, <XmlRoot("DATA", [Namespace]:="", IsNullable:=False)>. The IsNullable property, when set to false, will omit the XML for items if they are equal to nothing. If you set the IsNullable to True, then it will emit a tag like this <ListRoot xsi:nil = "true" />. In your code example, since you just created a new Data class like this Dim target As New Data(), all of the members are Nothing by default. Since you've set the IsNullable = False, you should only see that root tag, and that would be a valid serialization of the data.
If I have a class object A, and it has properties such as a0, a1, a2... If this class has 100 properties like this (up to a99). I would like to display each of these properties, but I do not want to have 100 lines of code of calling this as following
print A.a0
print A.a1
print A.a2
...
print A.a99
The code is too inefficient, so I am wondering if there is a way to loop through these properties. Thank you.
.NET provides the ability to examine an object at runtime through a process known as reflection. The purpose of the original post was to iterate through an object's properties in an automated fashion rather than by manually coding explicit statements that displayed each property, and reflection is a process to accomplish this very thing.
For this particular purpose, looping through an object's properties at run-time, you use the GetProperties() method that is available for each Type. In your case, the Type you want to "reflect" is A, so the type-specific version of GetProperties returns a list of the instance properties for that object.
When you ask .NET to return the properties of an object, you can also specify what's called a binding flag that tells .NET which properties to return - public properties, private properties, static properties - a myriad of combinations from about twenty different values in the BindingFlags enumeration. For the purposes of this illustration, BindingFlags.Public will suffice, assuming your A0-A999 properties are declared to be public. To expose even more properties, simply combine multiple BindingFlag values with a logical "or".
So, now armed with that information, all we need to do is create a class, declare its properties, and tell Reflection to enumerate the properties for us. Assuming your Class A exists with property names A0-A999 already defined, here's how you'd enumerate ones starting with "A":
// Assuming Class "A" exists, and we have an instance of "A" held in
// a variable ActualA...
using System.Reflection
// The GetProperties method returns an array of PropertyInfo objects...
PropertyInfo[] properties = typeof(ActualA).GetProperties(BindingFlags.Public);
// Now, just iterate through them.
foreach(PropertyInfo property in properties)
{
if (property.Name.StartsWith("A")){
// use .Name, .GetValue methods/props to get interesting info from each property.
Console.WriteLine("Property {0}={1}",property.Name,
property.GetValue(ActualA,null));
}
}
There you have it. That's C# version rather than VB, but I think the general concepts should translate fairly readily. I hope that helps!
This MSDN code sample illustrates how to iterate over a class's properties using reflection:
http://msdn.microsoft.com/en-us/library/kyaxdd3x.aspx#Y900
Create a VB.Net Console application, copy and paste this code into the Module1.vb file and run it.
Module Module1
Sub Main()
For Each prop In GetType(TestClass).GetProperties()
Console.WriteLine(prop.Name)
Next
Console.ReadKey(True)
End Sub
End Module
Public Class TestClass
Private _One As String = "1"
Public Property One() As String
Get
Return _One
End Get
Set(ByVal value As String)
_One = value
End Set
End Property
Private _Two As Integer = 2
Public Property Two() As Integer
Get
Return _Two
End Get
Set(ByVal value As Integer)
_Two = value
End Set
End Property
Private _Three As Double = 3.1415927
Public Property Three() As Double
Get
Return _Three
End Get
Set(ByVal value As Double)
_Three = value
End Set
End Property
Private _Four As Decimal = 4.4D
Public Property Four() As Decimal
Get
Return _Four
End Get
Set(ByVal value As Decimal)
_Four = value
End Set
End Property
End Class