How to dynamically pass a variable (of string type) value to another variable name in vb.net? - 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).

Related

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?

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.

Values don't change in for loop

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.

Visual Basic add to Array dynamically and sort

Still having trouble can you please help?
This needs to be written in Visual Basic
Here is a statement from the Main part of the program...
mylist.ForEach(AddressOf ProcessLink)
What this statement says is the following.... "For each item in the ArrayList "mylist" send item to the sub program "ProcessLink"
Note that ProcessLink is going to receive multiple groups of data from the ArraList "mylist"
ProcessLink then takes each value sent to it and turns it into "P.myName" and P.myValue"
I need ProcessLink to then add these values to an array. And each time it receives a batch of data from the ArrayList "mylist" it will add those values to the same Array. ProcessLink will then sort the array based on "P.Value"
I then need ProcessLink to output the name value pairs in the array and output the result as...
Response.Write("<tr><td>" & P.myName & "</td><td>" & P.myValue & "</td></tr>")
What should the code in ProcessLink look like?
I really reccomend using generic object lists rather than Arrays. You will get all of the functionality you need + 10x more. Sorting, adding, etc are way easier. With generic lists you don't have to worry about declaring the Array size, or any of the difficulties when working with Arrays containing objects. Take a look to the following for more information including the code sample:
.NET Object Collections Using Generics 101:
http://allen-conway-dotnet.blogspot.com/2009/11/net-object-collections-using-generics.html
List(Of T) Class:
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

Array in VB .Net

I have an array Newstr(20) When I fill it, I need to know how many of its indexes has been filled ? and find out the values.
I need to know how many of its indexes has been filled ?
Arrays don't keep that information. They only know how many spots you allocated. You have to track how many you assigned yourself. More than that, if you're working with a collection where you don't know how many items there will be, arrays are really the wrong choice in the first place. You should use a List(Of T) instead.
You could populate the array with a known string, then test for that string to see how many elements in your array are filled.
I would - however - suggest using an array list. You can get the number of elements added to the list from the Count property. This is the MSDN entry for Array Lists.
In order to find which of the elements have been filled, you can use a LINQ construction like this:
Dim input() = New String() {"abc", "def", "ghi", "", Nothing}
Dim output = input.Where(Function(i) Not String.IsNullOrEmpty(i)).ToArray
When you run this code, the output array will contain "abc", "def" and "ghi".
You can modify the selector of the Where to suit your preference if you're coding for a different type of array.
For instance the selector for Integer? will be:
input.Where(Function(i) (Not i Is Nothing) Or (i <> 0)).ToArray
Of course, you'll have to be coding in .NET 3.5+ in order to get access to LINQ.