Default Value when Deserialize in VB.NET 2010 - vb.net

I have a (Serializable) class (Accounts), and inside this class some properties, including a Dictionary of another Serializable class (Symbols).
I Serialize the class (Accounts) and save them into a file to be used for next run.
Now, I have a new version of the application, which contain a new properties inside (Symbols) class. When I Deserialize the (Accounts), it loads the Dictionary correctly, but with un-desired values (the strings are nothing and the booleans are false).
Can I set a default values for those new properties inside the Symbols class when I deserialize? Note that I want this without a For loop, since the Dictionary of Accounts and the Symbols are large, which means I will need a huge double for loop to solve this using for loop.
Thanks.
Ahmad

You can just specify a default value for each property:
For backing fields:
Private _someProperty as Boolean = True
For auto-Implemented Properties:
Public Property SomeProperty As Boolean = True

I managed to work-around this case by making all my variables as Strings (to be handled as objects), and within my code, I check if the value is Nothing, then I will assign the default value when needed... I know this is not the best solution, but at least it worked out!

Related

Identical objects in a list produce different hashes and fail comparison tests

I have a weird issue. I want to implement an extension to List with a function to merge another list into it excluding the duplicate values:
<Extension()>
Public Sub AddUnique(Of T)(ByVal self As IList(Of T), ByVal items As IEnumerable(Of T))
For Each item In items
If Not self.Contains(item) Then self.Add(item)
Next
End Sub
Now, I have a class that I'll be creating objects from, and adding them to a list:
Class DocInfo
Public Property title As String
Public Property fullPath As String
Sub New(title As String, fullPath As String)
Me.title = title
Me.fullPath = fullPath
End Sub
End Class
Then, I have a list as a global variable:
Public docsInfo As New List(Of DocInfo)
And then I have a button handler that adds new items to that list:
Private Sub AddToList_Button_Click(sender As Object, e As RoutedEventArgs)
Dim candidateItems As New List(Of DocInfo)
For Each doc In selectedDocs
candidateItems.Add(New DocInfo(doc.GetTitle(), doc.GetPathName()))
Next
docsInfo.AddUnique(candidateItems)
End Sub
(The doc and selectedDocs variables are outside of the scope of this question.)
Now, the important bit - GetTitle() and GetPathName() return the same strings on every button click (I have the same docs selected between clicks). Meaning that DocInfo objects that are added to the candidateItems, and then added to docsInfo, are identical. Nevertheless, the extension function AddUnique fails, resulting in duplicates in the list.
Puzzled, I ran GetHashCode() on these duplicate DocsInfo class objects:
For Each docInfo In docsInfo
Console.WriteLine(docInfo.title)
Console.WriteLine(docInfo.fullPath)
Console.WriteLine(docInfo.GetHashCode())
Next
And this is the output:
Assem1^Test assembly.SLDASM
C:\Users\Justinas\AppData\Local\Temp\swx5396\VC~~\Test assembly\Assem1^Test assembly.SLDASM
7759225
Assem1^Test assembly.SLDASM
C:\Users\Justinas\AppData\Local\Temp\swx5396\VC~~\Test assembly\Assem1^Test assembly.SLDASM
14797678
With each button click, I am getting identical DocsInfo objects (title and fullPath properties have the same values), yet their hashes are different every time, and every comparison I can think of, fails to acknowledge that these objects are for all intents and purposes idendical.
Why is this happening? And how can I fix the AddUnique extension function to work as intended?
This behavior is because of the difference in .NET between "Reference" types and "Value" types. The fundamental philosophy of these is that for "Reference" types, object identity takes precedence over contents (that is, two different object instances with the same contents are still considered distinct), while for "Value" types, the contents are the only thing that matters.
In VB, Class denotes a reference type while Structure denotes a value type. Their respective behaviors are what you would expect, then: by default, Equals on a Class is equivalent to ReferenceEquals, checking to see if the references are the same, and GetHashCode returns a value based on the object identity. Equals on a Structure does member-wise value equality, and GetHashCode returns a value based on the hash codes of the members.
There are a couple of different options for overriding the default behavior, with differing impacts and levels of intrusiveness.
You can change Class to Structure. If you do so, I would strongly recommend to eliminate any mutable behavior on them (i.e. make all fields and properties ReadOnly), because mutable Structures can be extremely hard to reason about correctly. If you really do have immutable data, though, this is the easiest to maintain because .NET will already do what you want, you don't have to maintain your own Equals or GetHashCode override.
You can override GetHashCode and Equals on your Class to act like the Structure versions. This won't change anything else about your class, but it will make it act like a value type for the purposes of containers and sequences. If you're worried about maintenance, an alternative would be to do something reflection-based, though this shouldn't be used for anything that will be high-throughput because reflection is generally not particularly performant.
I believe the hashing and ordering containers take optional constructor parameters that will let you provide a class for overriding the behavior of the contents without altering the Class itself. You could do something like this. I'd recommend to look at the MSDN docs for HashSet.

Dynamically Change value of Class Variable at Runtime in VB.Net

I need to be able to change the value the variable "timeMins" at runtime in the JSON container class below. But, the only way that VB.Net allows me to do this is to declare "timeMins" as a Constant - However, constants cannot be changed at runtime as far as I know in VB.net.
Below is what I have so far...It compiles and runs, but does not do what I need it to do.
Const timeMins As String = "15"
Public Class JSON_Container_Real_Time
<JsonProperty(PropertyName:="Meta Data")>
Private Meta As MetaData
<JsonProperty(PropertyName:="Time Series (" + timeMins + "min)")>
Public Time_Series_Daily As Dictionary(Of String, StockInfo)
End Class
In its current state what you're trying to do is not possible. At namespace level you're only allowed to declare types and constants, so you would need to move the variable declaration inside your class in order to be able to make it a non-constant. However, this means that you cannot use it in the JsonProperty attribute, because attributes require constant values only.
You would have to look for another solution to serialize/deserialize you class.

Multiple Set handlers in a property

I remember coming across some way to declare multiple Set handlers in a property but now I can't figure out how it's done. It's useful in that one can assign different data types and the Set handler does the conversion, but I get the error
'Set' is already declared
thoughts anyone?
It's not possible
It would be nice to be able to write both
sQuantity = "1234"
and
sQuantity = 1234
with two setter functions, but trying to write even one setter function with the wrong parameter type seems doomed to failure:-
error BC31064: 'Set' parameter must have the same type as the containing property.
If Visual Basic doesn't allow conversion between setter parameter type and property type then there is no way it would be possible to have two setter functions. If setter functions are forced to have the same type as the property, then it could not know which to run if there were more than one!
So I'd argue 'not only does it not seem possible, but it is actually not possible!'
There is a workaround
What you can do however, is have two properties of different types changing the same underlying variable, so that you can write
sQuantityFromString = "1234"
and
sQuantityFromInt = 1234
using
Public Shared WriteOnly Property sQuantityFromInt () As Integer
with a setter function that takes an integer as a parameter and with both properties setter functions modifying the same underlying string member variable.
Private Shared m_sQuantity As String = Nothing
As far as I know, you cannot have multiple Set statements for a class property. A property cannot be overridden.
You can use a setter functions (this is mostly a paradigm in Java) and overload that if you need to. Then I would also suggest making the property readonly.
One other option is to have the property be defined as an Object and in the set check the TypeOf of the value being used to set the property and do whatever business logic you want. The only problem with this approach is that then your property doesn't have type checking.

Comparing String to class name

I have a little Problem:
I have a method that parses an incoming string for certain values, if a value is found, a new class is instantiated. The class name is identical to the parsed string. At the moment, my code looks like this:
Public Class Test1
End Class
Public Class Important
End Class
Public Class DoWork
Public Sub DoWork(incoming as String)
Select case incoming
case "Test1"
dim myobj as new Test1
Case "Important"
dim myobj as new Important
End Select
End Sub
End Class
I do not like the string literals like "Test1" - i could store them in a constant, but if the class names change, they have to be changed too. Is there a way to replace the literals with the Name of class?
I know that me.gettype produces the result for instantiated objects, but what about the simple name for a class, which is no object at this moment?
If your string is in correct format you can use Type.GetType(string) method to retrieve type. Then you can use Activator class to create instance if you have default constructor on that type.
Rafal's answer is good if you're stuck with the current situation, with the incoming string parameter. But it's still a bit fragile. What if the incoming parameter changes? What if you want to restructure your code, moving some classes to different namespaces or assemblies? What if those strings change - do you now have to rename your classes and recompile? You don't see the magic strings explicitly now, but they're still there.
So ask yourself - where are those strings coming from? Are they generated internally by your code? If so, you might want to generate, instead of strings, an Enum value that corresponds to the class to be instantiated. If they're external strings that you map to your classes, consider having explicit mapping (in a configuration file, for instance) that map String->Type. It's a bit more cumbersome, but a lot more flexible.

What kind of array is Foo() as Foo()?

We generated a class from an XML file a while back. I think we used xsd.exe.
One of the main node collections in the XML file was rendered as:
<System.Xml.Serialization.XmlElementAttribute("PRODUCT")> _
Public Property PRODUCT() As PRODUCT()
Get
Return Me.pRODUCTField
End Get
Set
Me.pRODUCTField = value
End Set
End Property
And sure, there's PRODUCT class defined later on, and it worked fine. Serialized and deserialized fine. Didn't need to worry about it or manipulate it.
Only now we have to revisit and manipulate the data.
But what kind of collection (array?) is Public Property PRODUCT() As PRODUCT(), and how do we loop over it? And add to it?
Basic question, I know. Probably got too comfortable with generics and now xsd has thrown something at me which isn't List(of T) I'm running scared.
Don't be confused by the two sets of parens there. The first set, is simply the parens after the name of the property, whereas the second identifies the return type as an array of Product objects.
Similar to: Public Property IDs() As Integer()
That property returns only an array of integers, and the parens near IDs() only exist because you're declaring the property.
Since it appears to be a standard array of Product objects, you can loop over it with any number of normal loops:
For Each p As PRODUCT In obj.PRODUCTS()
...
Next
or
For i As Integer = 0 To obj.PRODUCTS.Length-1
...
Next i
Your code
Public Property PRODUCT() as PRODUCT()
Returns an array of Objects Of Type PRODUCT. Now whether that Type is a Collection, Structure, or Array I do not know with the code you have provided. The simplest way to loop over it would be as such.
For each prod as PRODUCT in rtnPRODUCTS
'Do Something
Next