Declare a variable with a generated name

Is it possible to declare a variable using a generated name. E.g.

Var xyz123 as String

where xyz123 is a string that I’ve generated earlier in the program?

I can’t see a way to do it directly, but can anyone suggest a work-around? I need to create around 300 variables like this.

Any guidance much appreciated.

Ian.

What are you actually, in effect trying to do?

There is more than likely a decent solution for your problem, but you haven’t told us what your problem is, only what your proposed solution is.

If you decide to answer this, do not include anything about “I need to create 300 variables” – that’s your solution, not your problem.

2 Likes

No, you don’t.

A simple array with 300 elements will do the job perfectly well.
If you insist on randomly generating names in this way, you can store the values associated with them , into a dictionary, or Pairs

Seems like some bad habit acquired from some other platform, probably some scripted language, not a compiled one.

1 Like

When there is no solution, there is no problem :wink:

I have an array of lines from an incoming data source. I need to keep track of these lines of data for various processing purposes throughout the program, so I want a way to be able unambiguously to identify them.
For example the data on line 200 may read “one.two.three.four” and I want to know whether any other lines in the array read 'one.two.three". I have a tree control that will eventually contain a hierarchy of

one > two > three > four

etc. (this editor won’t let me indent unfortunately). So I want to be able, when I see a line reading one.two.three.four, to know that I have to attach a string “four” to the existing tree under “three” - if that part of the tree exists. To do that I want to be able to find in the tree control “three” as an existing child of “two” which is in turn a child of “one”. The actual data is much more complex than this of course.

I can work through the data and attach a new item to the tree control, but only if I can find the point in the tree where it needs to attach. That’s why I want to have predictable names for things in the tree, to help me to identify and attach the new items.

I hope this helps explain what I need,

Ian.

You can actually… it’s called a Dictionary.

Var d as new Dictionary
d.value("xyz123") = "abc"
1 Like

To me, the best way to achieve what was most recently asked for, is an in - memory database holding what amounts to a tree view data

You only need 2 fields:
Self, and Parent

The first row would be

One,
Then

Two,One
Three,Two

and so on

This is linear and in the given example assumes that there is only one child, but real data wont be like that.

Instead you will have

Colors,Null
Red, Colors
Green,Colors
Blue,Colors
Crimson,Red
Tomato, Red
Peacock,Blue
Mint,Green
Spearmint,Mint
Peppermint,Mint

Armed with that, you can easily build a hierarchical view of the data.
Just find any row with no parent
Then (if expanded), all rows that have that as the parent.
And so on

If it saves time, you can also save the fullpath as a field.

Colors,Null,Null.Colors
Red, Colors,Null.Colors.Red
Green,Colors,Null,Colors,Green
Blue,Colors,Null.Colors,Blue
Crimson,Red,Null.Colors.Red.Crimson
Tomato, Red,Null.Colors.Red.Tomato
Peacock,Blue,Null.Colors.Blue.Peacock

and so on

This is simplistic, and also assumes none of the words get duplicated (eg Red only occurs in one place)
If that is not the case, each row needs to be assigned a unique number, and the data starts to look like

DESC,ID,PARENT
Colors,0,NULL
Red,1,0
Blue,2,0
Green,3,0
Crimson,4,1
Peacock,5,2

1 Like

Annotating such chains as keys in dictionaries will do the job.

You just need to create you proper set of methods to handle it as you wish.

The concept of detection goes by lines like those:

Var tree As New Dictionary

If not tree.HasKey("root") Then Tree.Value("root") = True // Just an example

If tree.HasKey("root/op1") Then
  MessageBox ("root > op1 was already created")
Else
  Tree.Value("root/op1") = True // created, annotate
End

...

Oof. Every client I have that used dictionaries instead of classes regrets it.
I have one project where that’s actually their biggest source of problems.

When I parse stuff into hierarchic structures I use classes and interfaces.

I’m glad others have chimed in with some suggestions for you, this sounds more complex than I can help you with abstractly via the forum. If you’d like to DM me your actual data, I could be posed for an interesting challenge today.

2 Likes

The focus here was to solve what he asked, but as you said, creating a “Tree Class” (hierarchic structure) with proper methods for adding nodes, reading nodes, deleting nodes, etc is beyond a simple answer.

at least a simple class can solve complicated tasks and is better for debug.
if you can’t handle it with a Dictionary try something else.

Thanks for all of the replies and suggestions. I think that I am going to have to re-think this piece of work; what seems to the human eye a simple logical structure turns out to be an intractable (for me, anyway) mass of complexity!

Proverbe Shadok :rofl: