I have a few variables called: _2sVal, _3sVal, _4sVal, etc
I want to change each of their values through a loop.
Like:
For i = 1 To 10
'set the value
Next
I've tried putting them in a dictionary like:
Dim varDict As New Dictionary(Of String, Integer)
varDict.Add("2sVal", _2sVal)
varDict.Add("3sVal", _3sVal)
varDict.Add("4sVal", _4sVal)
I can retrieve the value using
MsgBox(varDict(i.ToString & "sVal"))
But when I try to change it like
varDict(i.ToString & "sVal") = 5
It doesn't do anything. No errors or exceptions either, just the value stays unchanged
When you are using
varDict.Add("4sVal", _4sVal)
You are not putting the _4sVal variable inside the dictionary, but its value.
Then, changing the dictionary will not change the _4sVal, since there is no reference of it inside the dictionary.
What I mean is
varDict("4sVal") = 5
will change the value of dictionary but not the variable _4sVal itself.
I think the correct to do is define that variables as Properties, defined like:
Property _4sVal As Integer
Get
Return varDict("4sVal")
End Get
Set(value As Integer)
varDict("4sVal") = value
End Set
End Property
This way you will not have to change anything in the rest of your code. It will be transparent.
Related
I have a list of objects. Using a for loop I search for the object I want to modify and after finding my object I want to completely modify my object in tempMach.lstnVar. Below is my code:
For Each var As ListenVariable In tempMACH.lstnVar
If var.varName = newLstnVar.varName Then
var = DeepClone(newLstnVar)
Exit For
End If
Next
Deep clone function creates a deep clone of the newLstnVar. The loop assigns the value into var, but when the loop is done there are no change in tempMACH. I'm really confused how = operator works with objects. When I use = does it assign the reference or the value? When I declare a variable in for loop such as var in this case, is it just a variable with the same value in tempMACH.lstnVar, or is it a reference to it?
You can't use a For...Each if you want to replace the object itself since you can't assign anything to the loop variable. Instead use a For-loop:
For i As Int32 = 0 To tempMACH.lstnVar.Count() - 1
Dim obj As ListenVariable = tempMACH.lstnVar(i)
If obj.varName = newLstnVar.varName Then
tempMACH.lstnVar(i) = DeepClone(newLstnVar)
Exit For
End If
Next
You can use a For-loop only if the collection implements IList or IList(Of T), for example a List(Of String) or String(). Otherwise you have to re-create the sequence.
I have defined a structure in my code and have a list of this structures"
Structure Parcel
Public name As String
Public type As String
End Structure
Dim ParcelList As New List(Of Parcel)
Then I'm trying to set some values to an element of the list which name is known to me
For Each myParcel As Parcel In ParcelList
If (myParcel.name = "Parcel1") Then
myParcel.type="Type1"
End If
Next
Unfortunately values in my list don't change at all. what am I doing wrong?
As Parcel is a Structure, it is passed by value so when iterating through collection, you are modifying a copy of your structure.
To better understand this case, you should understand what For Each really is. Your code can be translated into:
Dim enumerator As List(Of Parcel).Enumerator = ParcelList.GetEnumerator()
While enumerator.MoveNext()
' Here you have a local copy of your Structure
Dim myParcel As Parcel = enumerator.Current
Dim flag As Boolean = Operators.CompareString(myParcel.name, "Parcel1", False) = 0
If flag Then
' Here you modify your local copy
myParcel.type = "Type1"
End If
End While
If Parcel was a Class, it would be passed by reference so no local copy would be created and line myParcel.type = "Type1" would change proper object existing in your collection.
As already Stated this is because you are modifying a local copy of a value type. One way round this is to access the items in the list by ordinal and replace the ordinal value type with a new type:
For i As Integer = 0 To ParcelList.Count - 1
If ParcelList(i).name = "Parcel1" Then
ParcelList(i) = New Parcel With {.name = ParcelList(i).name, .type = "Type1"}
End If
Next
But really you should change the Sturcture to a Class
When checking for strings use Equals instead of '='.
If (myParcel.name.equals("Parcel1")) Then
myParcel.type="Type1"
End If
Strings are in fact 'Objects'. When you compare Strings (example StringA = StringB), you check the allocation of the String in Memory instead of the contents of the string.
Even better would be:
If (myParcel.name.ToUpper().equals(Cstr("Parcel1").toUpper())) Then
myParcel.type="Type1"
End If
That way you ignore any difference case-wise.
example:
myParcel.name = "teST"
myParcel.name.equals("test")
is False
myParcel.name.ToUpper().equals(Cstr("test").toUpper())
is true
I am trying to assign a value to global variable, which has a Property of type Double. This Property is passed as Object and the assignment fails.
In the example code below, the value is never assigned to the actual object, but only locally:
Public Class Form1
Friend Home As New Building
Private Sub AssignValues() Handles Me.Load
'Objects of different types are added to a list
Dim listObjects As New List(Of Object)
listObjects.Add(Home.Surface)
'All the Objects in listObjects are assigned a value that
'is stored as String
For Each o As Object In listObjects
SetProperty(o, "45.6")
Debug.Print("Surface = " & Home.Surface.ToString)
Next
End Sub
Private Sub SetProperty(ByRef Variable As Object, ByVal Value As String)
Select Case Variable.GetType
Case GetType(Double)
Variable = CDbl(Value)
Case Else
'...
End Select
End Sub
End Class
Public Class Building
Dim _surface As Double = 0
Public Property Surface As Double
Get
Return _surface
End Get
Set(ByVal value As Double)
_surface = value
End Set
End Property
End Class
The program invariably outputs Surface = 0 instead of 45.6. What am I doing wrong?
I tried to pass the Variable as reference, as suggested here, but without success. I also read about using Reflection, but there ought to be something simpler than that...
When your adding home.surface to the list, your adding a copy of the double to the list and then adjusting that copy. Stick a watch on "o" and see how it changes whilst home.surface remains the same.
If you want to use reflection, try something along these lines.
Dim prop As Reflection.PropertyInfo = o.GetType().GetProperty("Surface")
prop.SetValue(o, 45.6)
With Variable.GetType you will get always Object, because this is the type of Variable. What you can do with an Object is converting/casting it into a different type (like Double).
The best way to determine the "original type" from where the Object comes would be including an additional variable telling it. Another option might be converting the given Object into the target Type and see if it is not nothing/does not trigger an error. But this second option is not too accurate, mainly when dealing with "equivalent types" like Doubles/Integers.
The code I have is:
Dim Dbase() As String = Nothing
Dbase(0) = Db_ComboBox.Text
I have declared Dbase as array and assigned Nothing, Db_ComboBox is a combobox.
For that assignment statement, I'm getting the following error: "Reference 'Dbase' has a value of 'Nothing'"
What is the reason for this error, and how can I take the value from the combobox and save it in the array?
You need to change this:
Dim Dbase() As String = Nothing
to this (declare an array of 1 element):
Dim Dbase(0) As String
And then this line will work:
Dbase(0) = Db_ComboBox.Text
If you need to change your array size you can use Redim or Redim preserve, as required.
If you anticipate contents of Dbase to change often, I am all with #Joel's suggestion about switching to List(Of String) instead of handling array sizes manually.
Let's look at your code:
Dim Dbase() As String = Nothing
Dbase(0) = Db_ComboBox.Text
Especially the first line. That first line creates a variable that can refer to an array, but the = Nothing portion explicitly tells it, "Do not create a real array here yet". You have, effectively, a pointer that doesn't point to anything.
I get here that what you really need is a List collection that you can append to over time:
Dim Dbase As New List(Of String)()
Dbase.Add(Db_ComboBox.Text)
Dbase() IS NOTHING. Look at this example:
cargoWeights = New Double(10) {}
atmospherePressures = New Short(2, 2, 4, 10) {}
inquiriesByYearMonthDay = New Byte(20)()() {}
That's how you declare arrays.
More examples: http://msdn.microsoft.com/en-us/library/vstudio/wak0wfyt.aspx
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.