VB2010 Express - Variable inside of a variable - Executing explicitly variable value.

Let me best describe this issue.

I am trying to get Visual Basic (2010 Express) execute the value of a string variable, rather than the varialble itself.

As an example (then I'll quote the context).

Dim tmpString1 as string = LocalFolder
Dim tmpString2 as String = "GlobalVariables.str" & tmpString1
Dim tmpVal1 as String = "Hello"
GlobalVariables.strLocalFolder = "World"

tmpString2 = tmpVal
' Result = "Hello"

' Result = "World" ... 
' This should result in "Hello"

Open in new window

In other words I want VB to process the line
tmpString2 = tmpVal

Open in new window

GlobalVariables.strLocalFolder = tmpVal

Open in new window

but this is the struggle.

That's the end goal, let me explain in context so perhaps a better approach can be suggested.

I have a long list of registry keys I want to import into a variable with the same name. These variables are stored within a GlobalVariables class that already exist and have default values set (the registry setting value should overwrite that of the default setting).

The string looks like this:
Dim strSettingList As String = "LocalFolder, SlideDuration, PictureX, PictureY"

Open in new window

Then I am converting the string into an array:
Dim strSettingArray() As String = Split(strSettingList, ",")

Open in new window

I'm then declaring several more strings I will use to construct the registry lookup:
Dim intCount As Integer = Nothing
Dim tmpString As String = Nothing
Dim tmpVariable As String = Nothing

Open in new window

I am using a For, Next loop to read each item in the strSettingArray array, go and get that registry key and apply it to the variable name of the same name inside my GlobalVariables class.

read registry key LocalFolder and apply it to the GlobalVariables.strLocalFolder variable.
read registry key SlideDuration and apply it to the GlobalVariables.strSlideDuration variable.

I am trying to make the For Next loop work because I am going to have about 50 different registry keys, and would like to dynamically add new keys just be expanding the original strSettingList comma delimited string.

This is how I'm doing that:
 For intCount = LBound(strSettingArray) To UBound(strSettingArray)
                tmpVariable = "GlobalVariables.str" & strSettingArray(intCount)
                If Not My.Computer.Registry.GetValue("HKEY_CURRENT_USER\SOFTWARE\MyApplication", CStr(strSettingArray(intCount)), Nothing) Is Nothing Then tmpVariable = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\SOFTWARE\MyApplication", CStr(strSettingArray(intCount)), Nothing)
                MsgBox(tmpVariable) ' This returns the correct value as defined in the registry
                MsgBox(GlobalVariables.strLocalFolder) ' Returns the old value of the variable defined in its declaration, but it should be the same as tmpVariable.

Open in new window

The last step is to apply the value to a text box with the same name, ie:
"txt" & tmpVariable & ".text" = tmpString

Open in new window

and I am hitting the same hurdle.

Any advice or suggestions would be appreciated. I've spent the last 24 hours trying to figure out a solution, well and truly stumped on this.

Thanks much.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Sorry, can you explain your concrete problem with a little bit more context?

Especially as I don't understand "I have a long list of registry keys I want to import into a variable with the same name"..

Why variable, what type? Have you considered using a generic List?
graemelucasAuthor Commented:
Thanks for the response, and sure.

I have a long list of registry keys. These keys are the settings for my application:

 Upon load, I am try to get the application to read these registry keys and apply the values to a variable with the same name, that are part of a public class.

Lastly, I am trying to get these same values to populate the text of its associated text boxes:

Does that help clarify?

graemelucasAuthor Commented:
Why variable, what type? Have you considered using a generic List?

I don't know what a generic list is, variable type is string.

There's probably a much easier way to do this. Thanks in advanced for the assistance.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

ste5anSenior DeveloperCommented:
I would use the built-in application settings in the first place.

Another solution would be using a separate class to store those values. The entire loading code would be also placed there. In this case I would consider using reflection.

I would never use global variables, cause the represent state, which makes code much harder to test.
graemelucasAuthor Commented:
Hi ste5an.

application settings makes sense, but I'm not quite grasping reflection, is it possible for some examples the MS examples on the link don't seem to make sense in my context?

Thank you.

Looks like application settings is a much better approach, I will use that, thank you.

So then how would I achieve this?

RegistryKeyList = "LocalFolder, SlideDuration, PictureX, PictureY"

read registry value LocalFolder and apply it to My.Settings.LocalFolder and txtLocalFolder.text
read registry value SlideDuration and apply it to My.Settings.SlideDuration and txtSlideDuration.text
read registry value PictureX and apply it to My.Settings.PictureX and txtPictureX.text
read registry value PictureY and apply it to My.Settings.PictureY and txtPictureY.text

graemelucasAuthor Commented:
Okay, My.Settings is making more sense, and seems I can depreciate the registry and move into using application.settings, thank you.

My revised question would then be:
Can you list all user application.settings and apply the values to a textbox with the same name?  The idea that it's automatic, so I don't have to add a line of code for each setting, just list it in application settings and let the rest happen automatically?

as in:
My.Settings.LocalFolder > txtLocalFolder.text
My.Settings.SlideDuration > txtSlideDuration.text
My.Settings.PictureX > txtPictureX.text
My.Settings.PictureY > txtPictureY.text

A text box for each setting can be assumed.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
This is all built-in for you already!  You don't need any code at all...this can all be done thru the IDE.

Select the TextBox.
In the Properties Pane, at the very top, click on the "+" next to "(ApplicationSettings)".
Click on the "(PropertyBinding)" line that appears below, then click on the "..." on the right side of it.
Find the "Text" entry, click on it, and change the dropdown on the right to "LocalFolder".
Repeat for the other TextBoxes.

The values will be loaded and save automatically for you.

Here's a video on YouTube showing how to do it:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
graemelucasAuthor Commented:
Thank Mike, you know I just found that just a few moments ago.. GREAT!

I can't seem to link however a text box text value with a setting that stored as a byte.

I have a text box to display a duration (in seconds) between 1 and 100. I created an application setting called SlideDuration > Byte > 10 but can't select it in the application setting binding, assuming because of the value type.  I can only select settings stored as strings, not bytes or booleans.

Thanks, this is so much easier than I was anticipating. appreciate the assistance.
graemelucasAuthor Commented:
I've gone off topic to my original question. I'm going to mark this as completed and ask a new question about data bindings.

Thanks so much for your input, I'm on a much better track now.
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Personally, I'd switch from a TextBox to a NumericUpDown; then you could set Minimum and Maximum values on it and it's guaranteed to be a valid numeric value at all times.
graemelucasAuthor Commented:
Perfect, thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.