When declaring an object, how to use variable as the name? - vb.net

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.

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?

Read/get the current value of an object in Project VBA

I'm struggling to save the current value of an object (at least I think it's an object) in MS Project VBA.
Here's what I'm trying to do, save the value of my current header text.
dim a As String (or Variant/Object?)
a = Application.FilePageSetupHeader.Text.Value
This obviously doesn't work, but gets the point across.
The syntax for this is:
expression. FilePageSetupHeader( ** Name, ** Alignment, ** Text** )
with Name and Text being passed as a string.
If I get this to work, I'll also be interested in extracting the NormalType value from this object. Notice I'll have to specify the item:
GridlinesEditEx Item:=4, NormalType:=1
Any help would be appreciated!
FilePageSetupHeader is a method of the application object, not a property (see MSDN). The MS Project object model does not have a header object to interrogate, so unfortunately the headers (and footers) are write-only.

Referring to a variable in a variable

This is probably quite simple to do but I am very new to visual basic so please bear with me.
I know similar questions have been asked, but none of them seem to answer my question. I have an array created earlier, and want to randomly choose one value and change it.
I have two random number generators ('GeneratorP1' and 'GeneratorP2'), which are added to some text a create a variable name which is stored in Loc1:
Dim Loc1 As String
Loc1 = "th" & GenerateP1() & "(" & GenerateP2() & ")"
eg: th5(4) is stored in Loc1
How would I go about changing the value of th5(4)?
EDIT: I have 5 arrays (th1, th2, th3, th4, th5), with indexes up to 4
If I'm understanding your question correctly (which I'm not sure I am), you're wanting to use the string stored in variable Loc1 as a variable name?
You said you created an array earlier, so why do you need to randomly generate the variable (GenerateP1) and the index (GenerateP2)? If you have a single array, can't you use randomly pickthe index, then assign whatever value you want to it?
th5(GenerateP2()) = "my value"
If you had multiple arrays (th1, th2, th3, etc.) I would suggest using a Dictionary to randomly pick which array to use. Alternatively you could use a multidimensional array and generate both indexes
th(GenerateP1(), GenerateP2()) = "my value"
If possible, you should consider using a List instead of an Array.
Instead of having multiple arrays, use a 2-dimensional array
Dim th(5,4)
So instead of trying to reference th1(3), you could use th(1,3) or th5(2) would be th(5,2)

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.