Values don't change in for loop - vb.net

I have a List of structure's in my VB.NET program, and I'm looping over them, and change the values of the objects in the list, as follows
Dim retvals As List(Of SomeStruct) = parser.RetrieveData(new_path)
For i As Integer = 0 To retvals.Count - 1 Step 1
dim temp as SomeStruct = retvals(i)
temp.A = GetValueForA()
temp.B = GetValueForB()
Next
When I look into my List of structs after this loop, none of the values were overwritten. Why? I thought that I had references in my list, so if I change reference A to a struct, then reference B to the same struct should see the changes?
What am I missing?

Structs are a value type, whereas classes are a reference type. If you were to be using SomeClass instead of SomeStruct this code would work as you expect.
In the scenario of using a Class, retvals would be a list of pointers to class objects. "dim temp as ..." creates a copy of the pointer, and setting temp.A to something changes the object the pointer points to. So when the code is done, the pointer in the retval still points to the same object which has now been changed.
However, when using structs no pointers are stored. retvals is simply a list of the values stored in your structs. The "dim temp as ..." creates a copy of the entire struct. You modify this struct by changing temp.A, but the original struct never changes since you only modified a copy of it.

I realized what the problem was, the Structure in .NET is a value type, meaning the contents will get copied into the List. So changing the temp variable will not change the original in the List
I fixed it by using a class instead, which is a reference type.

Related

How to dynamically pass a variable (of string type) value to another variable name in vb.net?

I am a former VFP programmer and was amazed by some powerful technics of VFP such as declaring dynamically a variable and assign to it a name from another string variable. I am looking for how to do the same in vb.net. I search but most solutions suggest array or list where I could not use the specific meaningful name of the variables.
I have a list of many variables in a table and for each variable I would like to dynamically declare a variable that have the name of the variable and assign to to it the variable value. Below is just 5 % of the full list
partial list of the variables
I can declare all the variable one by one but I would prefer a shorter way if any.
How could you assist me?
I have not tried anything.
You can't dynamically create variables in a strongly typed language. (You could create a dynamically typed variable, but that's not what you're looking for.)
Take a look at the Dictionary class, which is a collection of pairs (name and value), like a classic "hash array" or an object in JavaScript.
Edit. When you create a dictionary in VB.Net, you specify the datatype of the key (usually a String) and the datatype of the value. If all your values are Integers, yo do something like: Dim myDict As New Dictionary (Of String, Integer). If you really need to store different classes of objects (using the same dictionary), you may do: Dim myDict As New Dictionary (Of String, Object) (but you'll lose type safety).

What do YOU name a List object that holds every letter in a String

I'm trying to get better at naming my variables and methods and this situation keeps coming up.
Take for example this code:
String word = "Hello";
List<String> wordList = new ArrayList<>();
The List I created is simply going to contain every letter inside the word variable, but by simply appending "-List" to the word, this could lead to confusing code.
I would simply like to know what you'd name the List Object instead of "wordList". Or maybe you would use "wordList"?
A broader question would be something like:
What would you name an object that was formatted into an object of a different class but still held the same "information" as the original object?

VBA Extra key/item pairs in scripting.dictionary

This code was working for several days, looping through a dictionary just fine. The loop started breaking, and I have extra keys in the dictionary and I have no idea where they came from.
I am creating a scripting.dictionary with the following:
Dim riskDict As New Scripting.Dictionary
After which, the object is empty, as seen in the watch:
I then add my first key/item pair:
riskDict.Add "Weight", Array("WP", 0)
And after running this one line of code, I now have this:
Where did these two extra keys (Item 2 and 3) come from?! This is a problem, since later in my code I use:
For Each key In riskDict
temp = riskDict(key)
...
Next key
And this loop breaks, since it starts referring to keys which are empty. This does not seem to have been happening until just now, and the code has been running fine for a few days. (I changed some things elsewhere in the code, but completely unrelated to this.)
Let me know if I am doing anything ridiculous, or missing something obvious, and thanks!
Make riskDict private or clear your array before using it. Hard to tell what your doing with it since there is critical elements missing.
Dim riskDict As New Scripting.Dictionary
Although this is an old question, it might still be of interest to other users.
Before the first item has been added to an initialized dictionary object, the local window of the VBA editor must not be switched on if a breakpoint exists in the scope of this Dictionary object. Otherwise a key-value pair (0, Empty) might be inserted into the dictionary. This dictionary entry cannot be deleted again in the immediate window, even Dictionary.RemoveAll fails. This is the case even if the local window has been closed again in the meantime.
The exact circumstances under which this behavior occurs are not clear to me: I've only observed this behavior of Dictionary objects in class modules so far, but on the same line of code it sometimes occurs, sometimes not.
This SO discussion also describes the behavior when a watch is set on the Dictionary object. There, this behavior is associated with the property of dictionaries that when a query is made for an unknown key, this key is automatically added with a value of 'Empty'.

When declaring an object, how to use variable as the name?

Can I use the value inside of a variable to name an object? If so, what is the syntax for that declaration?
Every object has to be given a distinct name. Dim XXXX as NEW_ARRAY is named XXXX. Since I will have 10,000 objects, I would like to automate the creation of those objects using a loop. But, if the object creation loop uses the same name over and over again, I understand that the object would overwrite itself 9999 times. There would only be one instance of that object.
I would like to use the value of a variable as that distinct name. However, I think that typing in a name of a variable in the name position while declaring the object would only overwrite the first object over and over again.
Is there a specific syntax that puts the VALUE of a previously declared variable as the name of an object?
So, XXXX= 1111
Dim "XXXX" as NEW_ARRAY would be named 1111
Then XXXX=2222
Dim "XXXX" as NEW_ARRAY would be named 2222
Then XXXX=3333
Dim "XXXX" as NEW_ARRAY would be named 3333.
Object doesn't have name, variable has. #StevenDoggart already elaborate about that for you.
If the question was "Can I use the value inside of a variable to name a new variable?" Short answer is "no, you can't". There is no point to have such specific feature in .NET.
You can achieve similar behavior using dictionary, as suggested in many posts asking about dynamic variable name in .NET. You can see dictionary key as variable name, and dictionary value as variable value. As far as I can see, what you can do given such dynamic variable name feature exists in .NET, can be done equally well using dictionary.

Total Nos. of items in array variable

I have an array variable (string type). It contains certain no. of items, that I donot know how many they are.
I need to run a loop for that many nos. that the array contains. I tried LBound and UBound loop but it says my array is not a system array.
How can I know how many items my array contains?
Thanks
Furqan
You can use the Length property of the array object.
From MSDN (Array.Length Property):
Gets a 32-bit integer that represents the total number of elements in all the dimensions of the Array.
Read about arrays in VB.NET and the Array class for better understanding of arrays in VB.NET and the .NET framework.
Update:
However, for looping over an array you should simply use a For Each loop (as an array is treated like any other collection in .NET) - this way you will not make any silly mistakes with array bounds and off by ones:
For Each item As arrayItemType in MyArray
' do stuff with item
Next
See the example on this page.
You look at the length:
To get the number of items in the first dimension: arrayName.GetLength(0)
If you need the index, use GetUpperBound(0)
Some helpful examples here.
Like Oded said, you can use the Length-propery of the Array. This would look something like this:
Dim data As String() = {"one", "two", "three", "four"}
For i = 0 To data.Length - 1
Console.WriteLine(data(i))
Next
If you just want to loop all strings in your array, you can use For Each as well:
For Each s As String In data
Console.WriteLine(s)
Next
If the compiler is telling you that your variable is not a system array, then chances are, it's not an array. If it's not an array, you won't be able to get its bounds through any means.
Inspect the variable in the locals window and verify that your variable is of the type that you think it is. It's probably not an array after all.