VB.net 3.5 assignmet operator assigning pointer instead of copying? - vb.net

My program has decided to assign pointers instead of make copies of an object, and I'm not sure why. I have something like this:
Public Class Foo
Private myFooData As New List(Of FooData)
Public Sub New(ByVal newFooData As List(Of FooData))
myFooData = newFooData
End Sub
Public Property FooValues() As List(Of FooData)
Get
Return myFooData
End Get
Set(ByVal value As List(Of FooData))
myFooData = value
End Set
End Property
End Class
And it's used like this:
Public Sub Dosomething()
Dim mainFoo as new Foo
For x = 1 to 10
mainFoo.FooValues(x) = New FooData
Next
Dim originalFoo as new Foo
originalFoo.FooValues = mainFoo.FooValues.Take(3).ToList
Dim newFoo as new Foo
newFoo.FooValues = originalFoo.FooValues
newFoo.FooValues(1) += 1
End Sub
Very simplified, but basically what I'm doing. So for some reason today when I change item in newFoo.FooValues, originalFoo.FooValues also changes, and mainFoo does not. I've tried assigning the entire objects as well and I get the same results. Any ideas why this may be happening and how to fix it?

This is how assignment in .Net is supposed to work.
When you called .ToList() in the middle of your second snippet, your code iterates over the set and makes copies into a whole new list. This is why your mainFoo object is "protected" — you created a new instance. If the FooData items being copied are themselves references to objects (hint: they probably are), then only the references are copied. The only exceptions are for strings and value types (primitives and structures), or if you code it by hand.
It's usually a good idea for List properties to make the property readonly:
Public Property FooValues() As List(Of FooData)
Get
Return myFooData
End Get
End Property
This will still let you manipulate the list to your heart's content, but prevents you from completely switching a list instance out from under the class. The same is true for other complex types exposed from a class as a property.
The one thing you don't want to do is change this to be a Structure instead of a Class. This may seem to do what you want at first, but it will cause other problems for you later.

When using Foo.FooValues, you assign references to the List(Of FooData) internally used in your Foos - and no values!
Let's consider the steps
Dim mainFoo as new Foo
mainFoo now has its own backing list.
originalFoo.FooValues = mainFoo.FooValues.Take(3).ToList
originalFoo gets assigned a new backing list storing some of the values of mainFoo.
newFoo.FooValues = originalFoo.FooValues
newFoo used to have a backing list of its own, but now, it uses the one originalFoo uses. The exactly same one (by pointer) and no copy.
Thus changing newFoo will change originalFoo but not mainFoo which has its own list.
Since I don't know what you're trying to achieve after all, I cannot tell how to fix the code, but as you see, it's never a good idea to make some backing list accessible i.e. assignable.
Thus I'd advice to keep the list immutable and private, just giving indexing access.

Related

What's the elegant way to make sure that values of dictionary are called or set in a specific location?

Private Shared Property _dictOfCoinNameNameBasedOnMarketandSymbol As Dictionary(Of String, String)
Get
End Get
Set(value As Dictionary(Of String, String))
End Set
End Property
The usual way, if it's not a dictionary, is to turn the variable into property. Then we can put breakpoint in set and get.
The thing is setting a dictionary as property will make the compiler think I need a set method to set the dictionary. I want a set method to set an item in the property.
I want to be able to replace spread occurrences of
_dictOfCoinNameNameBasedOnMarketandSymbol("key") = "value"
into a code I can set breakpoint too. How should I?
I suppose I can just create a normal sub and replace all occurrences of _dictOfCoinNameNameBasedOnMarketandSymbol("key") = "value" into AssignCoinNameNameBasedOnMarketandSymbolWithKeyValue("key","value")
I wrote
Function GetCoinNameNameBasedOnMarketandSymbol(key As String) As String
Return _dictOfCoinNameNameBasedOnMarketandSymbol(key)
End Function
Sub AssignCoinNameNameBasedOnMarketandSymbol(key As String, coinname As String)
_dictOfCoinNameNameBasedOnMarketandSymbol(key) = coinname
End Sub
And then I will just manually all other occurrences of _dictOfCoinNameNameBasedOnMarketandSymbol. Then I can put breakpoints in AssignCoinNameNameBasedOnMarketandSymbol if I see _dictOfCoinNameNameBasedOnMarketandSymbol get assigned wrong value.
The problem with this technique is there isn't really anyway to enforce it automatically. I just have to go through the code and replace that one by one.
Are there more elegant ways?
If _dictOfCoinNameNameBasedOnMarketandSymbol is not a dictionary then changing it to property with proper get and set value would do just fine. I don't even have to change anything.
I think that I may have initially misinterpreted the question. I will leave my original answer below for completeness but I will answer here based on my new understanding.
It seems to me that what you actually need is an indexed property that will wrap the Dictionary, e.g.
Public Class SomeClass
Private Shared ReadOnly valuesByKey As New Dictionary(Of String, String)
Public Shared Property ValueByKey(key As String) As String
Get
Return valuesByKey(key)
End Get
Set
valuesByKey(key) = Value
End Set
End Property
End Class
You can now just get and set the ValueByKey property and never have direct access - read-only or otherwise - to the Dictionary object inside the class. You can also add whatever code you like before the Return statement in the getter and before or after the existing line of code in the setter.
ORIGINAL ANSWER:
What you want is a ReadOnly property. That's exactly how collections are exposed throughout the .NET Framework, e.g. Control.Controls, ComboBox.Items, DataSet.Tables, DataTable.Columns, DataTable.Rows, etc, etc. In your case, that would look like this:
Public Shared ReadOnly Property CoinNamesByMarketAndSymbol As New Dictionary(Of String, String)
I've taken the liberty of giving that property a sensible name. Note that this is an auto-property, i.e. one where you don't explicitly specify a getter or setter. Without using an auto-property, the equivalent code would this:
Private Shared _coinNamesByMarketAndSymbol As New Dictionary(Of String, String)
Public Shared ReadOnly Property CoinNamesByMarketAndSymbol As Dictionary(Of String, String)
Get
Return _coinNamesByMarketAndSymbol
End Get
End Property
Note that I have declared this property Public. I note that you have declared it Private but that seems to make little sense. What use could such a property be? If it should only be accessed internally then why would you not just use a field?
With the code above, you can now get the Dictionary from the property to get, add or remove items but you cannot replace the entire Dictionary, i.e. you can do this:
Dim coinName = SomeClass.CoinNamesByMarketAndSymbol(marketAndSymbol)
SomeClass.CoinNamesByMarketAndSymbol.Add(marketAndSymbol, coinName)
SomeClass.CoinNamesByMarketAndSymbol.Remove(marketAndSymbol)
But you cannot do this:
SomeClass.CoinNamesByMarketAndSymbol = New Dictionary(Of String, String)

Why does the Default Property access the setter when removing an item

In the code below, when trying to remove an item from the Cases list the code breaks in the Setter with an index out of bounds. When running the debugger in VisualStudio 2017 it successfully goes through the Remove() function and deletes the last item but after returning to Main() it will break on the Setter and the call stack says it is coming from the Remove call. Example code below:
Sub Main()
Dim Cases As Collection = New Collection()
Dim caseIndex As Integer = 2
Cases.Remove(Cases(caseIndex))
End Sub
Public Class Collection
Public WithEvents Cases As List(Of CaseClass)
Public Sub New()
Cases = New List(Of CaseClass)()
Cases.Add(New CaseClass)
Cases.Add(New CaseClass)
Cases.Add(New CaseClass)
End Sub
Default Public Property BeltCase(ByVal Index As Integer) As CaseClass
Get
Return Cases(Index)
End Get
Set(ByVal Value As CaseClass)
Cases(Index) = Value
End Set
End Property
Public Sub Remove(ByRef BeltCase As CaseClass)
Cases.Remove(BeltCase)
End Sub
End Class
Public Class CaseClass
Public test As Int16
End Class
Call Stack:
TestingVBBug.exe!TestingVBBug.Module1.Collection.set_BeltCase(Integer Index,TestingVBBug.Module1.CaseClass Value) Line 25 Basic
TestingVBBug.exe!TestingVBBug.Module1.Main() Line 6 Basic
So why would we be going through the Setter at all. And why does that happen after we exit the remove function?
The problem is caused by your Remove() method, that is, you have a ByRef parameter (for some reason). When you use ByRef, any changes made to the parameter inside the method must be reflected to the variable that was passed to the method. That happens by reassigning the value to the original variable.
In your case, it works like this:
The Remove() method is called and a variable (Cases(caseIndex)) is passed to it.
Some work is done inside the Remove() method which might, or might not include changing the value of the parameter BeltCase.
The value of the parameter BeltCase gets reassigned to the variable that was originally passed to the method (which is Cases(caseIndex)).
As a result of the above step, the setter of the BeltCase property gets called with Index = 2 which raises the out of range exception because Cases(2) doesn't exist (was removed).
To confirm, you can see this problem go away when you replace this line:
Cases.Remove(Cases(caseIndex))
..with:
Dim myCase As CaseClass = Cases(caseIndex)
Cases.Remove(myCase)
That way, you create a new variable which refers to the same CaseClass object and most importantly avoid calling the setter of your Collection.BeltClase property.
However, a better solution would be to not use ByRef in the first place since you don't seem to need it in this situation. So, simply use Public Sub Remove(ByVal BeltCase As CaseClass) instead.
Check this question for more about ByVal and ByRef with objects.
One last thing, please don't call your class Collection because it can be very confusing to anyone looking at your project.

Building an ArrayList in an external class - vb.net

I have an ArrayList defined in Class A. Then I want to build this array in Class B and use it in Class A.
I defined the ArrayList as:
Public arrayList As ArrayList
Then, in Class B I do:
Dim trLogkEmpty As New A
'Loop with strEspece definition
trLogkEmpty.arrayList.Add(strEspece)
'End Loop
The program throws me this error:
NullReferenceException
I don't know why, because strEspece has never become null (I tested it). I don't know if there is another reason.
Also, when I loop through the arrayList elements in Class A, I get again NullReferenceException. This is the loop code:
For Each logkNull In Me.arrayElemWithLogkEmpty
Console.WriteLine(logkNull)
Next
I don't know what happens with the first exception, but the code runs "correctly". In the second exception I guess that is something like I'm loosing the elements values of the array. I don't know how to solve it...any help? I accept different ways to solve it!
You are making two of the same mistake. A NullReferenceException means that you are attempting to access a property or method on an object that hasn't been instantiated yet. You are attempting to access both A and A.arrayList without first creating new instances of them.
So, instead of just:
trLogkEmpty.arrayList.Add(strEspece)
You should have:
Dim trLogkEmpty As New A()
trLogkEmpty.arrayList = New ArrayList()
trLogkEmpty.arrayList.Add(strEspece)
However, I must insist that you avoid ArrayList, and also that you avoid instantiating a public member of a class from outside that class. I would suggest using a strongly-typed collection class such as List(Of T), and having a read-only property in A's take care of its instantiation and visibility so the collection (not its contents) can't be modified outside of A:
Public Class A
Private _myList As IList(Of String)
Public ReadOnly Property MyList As IList(Of String)
Get
If _myList Is Nothing Then
_myList = New List(Of String)
End If
Return _myList
End Get
End Property
End Class
And now you have:
Dim trLogkEmpty As New A()
trLogkEmpty.MyList.Add(strEspece)
You're probably going to need to keep your instance of A around, so class B should probably look somewhat like:
Public Class B
Private _a As A
Public Sub New()
_a = New A()
End Sub
' ... your methods that use _a.MyList
End Class
I got it finally. When I initialised the array in class 'A' I forget to create an instance of ArrayList class, specifically, I forget to put New:
Public arrayElemWithLogkEmpty As New ArrayList
So, partly, #Blackwood was right!
Thank you all and forgive me for my basic knowledge about vb.net.

OOP concept: is it possible to update the class of an instantiated object?

I am trying to write a simple program that should allow a user to save and display sets of heterogeneous, but somehow related data. For clarity sake, I will use a representative example of vehicles. The program flow is like this:
The program creates a Garage object, which is basically a class that can contain a list of vehicles objects
Then the users creates Vehicles objects, these Vehicles each have a property, lets say License Plate Nr. Once created, the Vehicle object get added to a list within the Garage object
--Later on--, the user can specify that a given Vehicle object is in fact a Car object or a Truck object (thus giving access to some specific attributes such as Number of seats for the Car, or Cargo weight for the truck)
At first sight, this might look like an OOP textbook question involving a base class and inheritance, but the problem is more subtle because at the object creation time (and until the user decides to give more info), the computer doesn't know the exact Vehicle type.
Hence my question: how would you proceed to implement this program flow? Is OOP the way to go?
Just to give an initial answer, here is what I've came up until now. There is only one Vehicle class and the various properties/values are handled by the main program (not the class) through a dictionary. However, I'm pretty sure that there must be a more elegant solution (I'm developing using VB.net):
Public Class Garage
Public GarageAdress As String
Private _ListGarageVehicles As New List(Of Vehicles)
Public Sub AddVehicle(Vehicle As Vehicles)
_ListGarageVehicles.Add(Vehicle)
End Sub
End Class
Public Class Vehicles
Public LicensePlateNumber As String
Public Enum VehicleTypes
Generic = 0
Car = 1
Truck = 2
End Enum
Public VehicleType As VehicleTypes
Public DictVehicleProperties As New Dictionary(Of String, String)
End Class
NOTE that in the example above the public/private modifiers do not necessarily reflect the original code
Let's first distinguish between the set of answers which one can ask about an object in the garage (its attributes) from the set of answers to those questions( its state).
If you are simply looking at a scenario where the set of answers changes, then a simple State Pattern applies. The attributes remain constant, and state changes. All object instantiations remain of a single type with constant attributes.
if you are looking at the more complicated situation where the available attributes for an object in the garage changes, one uses the Decorator pattern. However, I don't think this quite fits your scenario either. The Decorator pattern is for scenarios where there is a tractable number of attributes, but the number of possible combinations is potentially exponential because there is no restriction of which go with which.
The scenario that I think best handles your situation is that the object is actually undefined until identified, with only a Proxy (represented by the vehicle key) created initially, Once the object is completely identified, which it seems occurs all at once, it's full object is instantiated.
It is possible that you might want a Decorator sitting on top of the Proxy, but that might not be necessary either.
--Later on--, the user can specify that a given Vehicle object is in fact a Car object or a Truck object
You are dangerously close to asking for unrestricted downcasting as a feature. This is just not possible in managed code, the CLR provides hard guarantees that illegal downcasts are never possible. It raises the InvalidCastException when you try anyway.
Somewhat more concrete, if the original object was created as a Vehicle then there is no way that you can ever interpret or access that object as though it is a Truck. A Truck has, say, a Cargo property that Vehicle doesn't have. In fact, Vehicle doesn't even have the storage for Cargo. Re-interpreting a Vehicle as a Truck will give it a complete garbage value for Cargo. And much worse, writing the Cargo property will corrupt memory.
Unrestricted downcasting is possible in some languages, like C and C++. Particularly in C it is almost inevitable, void* is the "object class" of C. But these languages are also pretty famous for writing code that crashes at runtime. An illegal downcast is an excellent and common way to induce such a crash. The heap corruption this causes is extremely difficult to diagnose, the crash doesn't happen until much later, far removed from where the original damage was done.
You use the standard Factory pattern to create instances of a specific class that have a desired set of properties. Upcasting to the base class is always valid. Such a factory will return a reference of type Vehicle for example, even though it created a Truck object. Downcasting it later to a Truck will be valid.
Object Oriented Programming works best when you try to model realistic objects, rather than 'magical' objects that do things that don't make sense.
In the real world you can't have a car that is a vague blob, but suddenly becomes a Pickup truck. Thus it makes little sense to model your system this way, and you will run into various problems that cause you to go back to the "magic" again and again.
One can think of the compiler and the runtime environment as a sort of "pocket universe" and one can think of certain rules enforced by the compiler as "Laws of physics" that apply in that universe. In some cases you bend these laws, given certain compensations, but in general you shouldn't try to do this as it can cause huge rifts in the space-time continuum (ie, you can corrupt the internal state of the program).
Instead, I would model it this way. You can have a list of "License Plate" objects, and when you want to "create" a Pickup Truck, you use a Factory class, passing in the License Plate object and it will create a Pickup Truck that uses that license object.
Remember, that objects often contain other objects. A license plate is an object in and of itself, so why not treat it as such? Since you appear to have no real tie between the ambiguous "vehicle" and the license plate, this makes more sense.
My understanding is : You are trying to achieve in VB.net something you can actually dynamically do in JavaScript and its constructors...
I don't know if you can dynamically create Methods, Functions, Events or Properties in VB.net like :
Public Module SampleMembers
Public _PaxNum As Integer = 0
Public _CargoAmount As Integer = 0
Public Function GetPassengerNumbers() As Integer
Return _PaxNum
End Function
Public Function GetCargoAmount() As Integer
Return _CargoAmount
End Function
End Module
And then, declare in your application a basic object like :
Dim MyVehicle As Object
Later on, during runtime, dynamically add members to your vehicle object like :
Public Sub ConvertBaseVehicleToCar(ByRef CurrentVehicle As Object)
' ...
Object.AddMember(SampleMember._PaxNum, CurrentVehicle)
Object.AddMember(SampleMember.GetPassengerNumber(), CurrentVehicle)
' Where Object would have a magical Constructor Modyfier...
' That would be GREAT... of course
End Sub
But you can't do that in VB.net if I'm not mistaken
If it was just about datas...
I would use :
Public Class Vehicle
Private _PropertiesList As New SortedList(Of String, String)
Public Function AddProperty(ByVal PropertyName As String, ByVal PropertyValue As String) As Boolean
If _PropertiesList.ContainsKey(PropertyName) Then
_PropertiesList.Item(PropertyName) = PropertyValue
Return False ' Property replaced !
Else
_PropertiesList.Add(PropertyName, PropertyValue)
Return Property ' New Property added !
End If
End Function
Public Function RemoveProperty(ByVal PropertyName) As Boolean
If _PropertiesList.ContainsKey(PropertyName) Then
_PropertiesList.Remove(PropertyName)
Return True ' Property actually removed !
Else
Return False ' No property with that name !
End If
End Function
Public Function GetPropertiesList() As List(Of String)
Dim NewList As New List(Of String)
Dim CurrentProperty As String
For Each CurrentProperty In _PropertiesList.Keys
NewList.Add(CurrentProperty)
Next
Return NewList
End Function
Public Function GetProperty(ByVal PropertyName As String) As String
If _PropertiesList.ContainsKey(PropertyName) Then
Return _PropertiesList.Item(PropertyName)
Else
Return ""
' Or whatever explicit code of your choice
' like Return "N/A" or Return "#"
End If
End Function
' I would replace this latest function by
Public Property Item(ByVal PropertyName As String) As String
' ...
End Property
' ...
' And the Constructor
Public Sub New(ByVal VehicleType As String)
InitializeType(VehicleType)
End Sub
' With its default Properties like :
Private Sub InitializeType(ByVal ProposedType As String)
ProposedType = ProposedType.Trim().ToUpper()
Select Case ProposedType
Case "CAR":
Item("Type") = "CAR"
Case "TRUCK":
Item("Type") = "TRUCK"
Case "MINIVAN":
Item("Type") = "MINIVAN"
End Select
End Sub
' And add a FINAL ReadOnly Property
Public ReadOnly Property VehicleType() As String
Get
Return Item("Type")
End Get
End Property
End Class
Now, MyVehicle could be anything, a car, a truck, a plane, even PlanetEarth...
Still, I CAN'T mask or add methods, functions, properties upon runtime. My properties are all of type "String"
MyCar.Item("NumberOfWheels") = "6"
' ^^ I'll have to cast this to Integer before using it...
MessageBox.Show(SumOfWheels(MyListOfVehicles).ToString())
' Where :
Public Function SumOfWheels(ByVal ListOfVehicles As List(Of Vehicles)) As Integer
Dim CurrentVehicle As Vehicle
Dim CurrentWheels As Integer
Dim TotalWheels As Integer = 0
For Each CurrentVehicle In ListOfVehicles
If Integer.TryParse(CurrentVehicle.Item("NumberOfWheels"), CurrentWheels)
TotalWheels = TotalWheels + CurrentWheels
End If
Next
Return TotalWheels
End Function
However, I could add a sort of virtual type modyfier : The initial ReadOnly Property VehicleType()
' ...
Public Property VehicleType() As String
' The Getter is the same, but the setter is a litte bit different :
Set(ByVal NewType As String)
InitializeType(NewType) ' Simple ? No ! I'll have to edit the Method...
End Set
End Property
Private Sub InitializeType(ByVal ProposedType As String)
ProposedType = ProposedType.Trim().ToUpper()
Select Case ProposedType
Case "CAR":
Item("Type") = "CAR"
RemoveProperty("CargoHold")
Item("Drivers") = "1"
Case "TRUCK":
Item("Type") = "TRUCK"
RemoveProperty("PaxSeats") ' Well, you actually can have one.. or two..
Item("Drivers") = "1"
Case "MINIVAN":
Item("Type") = "MINIVAN"
Item("Drivers") = "1"
Case "MOTORBIKE":
Item("Type") = "MOTORBIKE"
RemoveProperty("CargoHold")
Item("Drivers") = "1"
Item("PaxSeats") = "1"
Item("NumberOfWheels") = "2"
Case "JETLINER":
Item("Type") = "JETLINER"
Item("Drivers") = "2"
Case "VINTAGEJETLINER":
Item("Type") = "VINTAGEJETLINER"
Item("Drivers") = "3"
End Select
End Sub
' ...
Anyway, I'll have to write codes for specific routines using several vehicles in my Garage. This would be members in my Garage Class. Each time I want to do specific things for a given set of vehicles, I'll had to check what type of vehicle it is and select the correct path of code to run.........
That would become very tricky if you want to have a bunch of sub models of vehicles...
' VEHICLE>MINIVAN
' VEHICLE>MINIVAN>CITROEN
' VEHICLE>MINIVAN>CITROEN>3CV
' VEHICLE>MINIVAN>CITROEN>3CV>BASIC
' VEHICLE>MINIVAN>CITROEN>3CV>COLLECTOR
' VEHICLE>MINIVAN>CITROEN>3CV>DEADHULK
But at least, you can have an usefull Function that retrives ALL vehicles with a specific property in your Garage :
Public Function GetVehicleUsingProperty(ByVal PropertyName As String, ByVal PropertyValue As String) As List(Of Vehicle)
' And a better one :
Public Function GetVehicleUsingProperty(ByVal PropertiesParam As SortedList(Of String, String)) As List(Of Vehicle)
' ... :P
Just the way I see things. Hope someone else could give a better way to implement all of this ?
I would be inclined to have a "Vehicle" base class that can be created normally (not abstract) with the basic properties that you do know. Including the VehicleType you defined set to "Generic" by default.
Create each specific type for each sub type. Defining the correct properties in a ridgid format to enforce good code.
In the base type create a function to Clone Vehicle Properties to a passed in Object. Eg.
Public sub CloneTo(byval OtherVehicle as Vehicle)
When a "Generic" vehicle needs to be more specific create the new child type, pass it to the routine to clone the existing information, and replace the old type with the new one in the Garage Collection.
You would need to evaluate the Child Type for each item in the garage collection to determine the available extended properties, but I think a good solid full tree list of children can minimize this work if all the correct levels are in place (the lower levels would be most commonly accessed and if any properties that can be are placed always at the highest level in the tree) Eg. Vehicle - Car - Sedan. PassengerCapacity for exampel is really a property of a Vehicle.

Reflection - SetValue of array within class?

OK, I've been working on something for a while now, using reflection to accomplish a lot of what I need to do, but I've hit a bit of a stumbling block...
I'm trying to use reflection to populate the properties of an array of a child property... not sure that's clear, so it's probably best explained in code:
Parent Class:
Public Class parent
Private _child As childObject()
Public Property child As childObject()
Get
Return _child
End Get
Set(ByVal value As child())
_child = value
End Set
End Property
End Class
Child Class:
Public Class childObject
Private _name As String
Public Property name As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Private _descr As String
Public Property descr As String
Get
Return _descr
End Get
Set(ByVal value As String)
_descr = value
End Set
End Property
End Class
So, using reflection, I'm trying to set the values of the array of child objects through the parent object...
I've tried several methods... the following is pretty much what I've got at the moment (I've added sample data just to keep things simple):
Dim Results(1) As String
Results(0) = "1,2"
Results(1) = "2,3"
Dim parent As New parent
Dim child As childObject() = New childObject() {}
Dim PropInfo As PropertyInfo() = child.GetType().GetProperties()
Dim i As Integer = 0
For Each res As String In Results
Dim ResultSet As String() = res.Split(",")
ReDim child(i)
Dim j As Integer = 0
For Each PropItem As PropertyInfo In PropInfo
PropItem.SetValue(child, ResultSet(j), Nothing)
j += 1
Next
i += 1
Next
parent.child = child
This fails miserably on PropItem.SetValue with ArgumentException: Property set method not found.
Anyone have any ideas?
#Jon :-
Thanks, I think I've gotten a little further, by creating individual child objects, and then assigning them to an array... The issue is now trying to get that array assigned to the parent object (using reflection).
It shouldn't be difficult, but I think the problem comes because I don't necessarily know the parent/child types. I'm using reflection to determine which parent/child is being passed in. The parent always has only one property, which is an array of the child object. When I try assigning the child array to the parent object, I get a invalid cast exception saying it can't convert Object[] to .
EDIT:
Basically, what I have now is:
Dim PropChildInfo As PropertyInfo() = ResponseObject.GetType().GetProperties()
For Each PropItem As PropertyInfo In PropChildInfo
PropItem.SetValue(ResponseObject, ResponseChildren, Nothing)
Next
ResponseObject is an instance of the parent Class, and ResponseChildren is an array of the childObject Class.
This fails with:
Object of type 'System.Object[]' cannot be converted to type 'childObject[]'.
Firstly I'd get rid of the array part of the equation - I can't see how that's relevant. Try to write code to set the values for a single child.
Secondly, it seems that you're relying on the results of GetProperties being in the desired order - you shouldn't. There's no guarantee as to what order the properties will be returned in. You should know what order you want based on the string you're splitting, and fetch the properties by name.
Thirdly, I suspect the problem is that you've got some read-only properties as well as writeable ones. I suggest you whip up a short console app to check this out, logging what properties you're trying to set before you set it.
If this doesn't help, please post a short but complete console app which demonstrates the problem, and I'm sure we'll be able to fix it.
EDIT: Okay, if you're now stuck just on the array part, I suggest you show a short but complete example of that instead. I suspect the problem is that you've created an array of the wrong type. You can use Array.CreateInstance to create the right kind of array, which should be valid when you then set the property.
There are libraries available to make it easier (and faster) to work with reflection. For instance, Fasterflect allows you to write the following:
parent.Property("child").GetElement(index).SetFieldValue("Name",name);
This will retrieve the property called "child" on the object "parent". Since we expect it to be an array we'll grab the element at position "index" (a single child instance) and set its Name property to "name".
Disclaimer: I am involved in said project as a contributor.