Question Grouping String Variables with their names

blueston

New member
Joined
Dec 13, 2010
Messages
3
Programming Experience
1-3
I am trying to reduce the amount of code I have and make it more maintainable and flexible. (I.E Do it the right way). I have a bunch of (48) Form controls (text boxes) and when I click next on that form I save all their text values to some 48 global string variables. The names of these variables are the same as the Text Boxes but with "_saved" appended to them.

For any given operation instead of having a separate line for each control it's nice to just use a loop. I can easily write some code to operate on each Control (set visibility, enabled etc) by using Me.Controls and a For Each Loop. I can even make collections of fractional groups of the 48 controls. For example, If cntrl.name.startswith("sometext") collection.add or perform some other operation only on that subgroup etc.

I'd like to do something similar with my Saved Public String Variables. For example I would like to do the following loop somehow but don't know how to group Variables together and keep their name in a way where I can define that array or collection on 1 line or just a couple lines.

' But this next line only groups the values
Group_of_string_Variables = {Var1, Var2, Var3, Var4}

For Each Name_of_String_Variable In Group_of_string_variables
For Each Control in Me.Controls
If Name_Of_String_Variable.StartsWith(control.name) Then
Value_Of_String_Variable = control.text
End If
Next
Next

But I can not access the name of a variable or find a way to easily group in key/value 48 different ones without needing much more than 50 lines of code to make either a 2-dim array, a collection, or a hash.

I'm sure someone will tell me I should be doing it a completely different way. That would be ok as long as I don't have to change too much of my other code. I thought of making an object of some kind instead of a variable so I could access 2 object properties- name and value then I could group those objects in a collection as I have done before. But that seems like it would be many more lines of code and I'm not sure it would work. Sorry for the long post but I anticipate people would ask for more details so I try to provide it all up front.
 
Thank you John H for the response. The Dictionary looks like another interesting way to group some variables. But it seems to suffer the same problem as collections and hashes where I have to use a separate line for every entry (dictionary.add (item1)). For some reason this really bugs me. Why can't I just define a big chain of items when I'm declaring it all on one line, like you can for a simple array. You can do (array = {var1, var2, var3}. So why can't I do (collection/hash/dictionary= {Key1, value1, key2, value2, key 3, value3} etc. It would be so much easier. Maybe I am spoiled by other languages.

I use the _saved variables for a couple purposes. One, I am using a succession of forms with next buttons, when I click next the current form closes and the next one opens, but I need to save the data entered on that form before it closes. Then when I get to the very end, I write all these values to an xml file. Oh boy, as I write this I just thought of another possibility- maybe I can just set all the form's visibility to off, instead of closing it completely, then all those values will still be there hidden. Then i can just use the control.text values and I can get rid of all the _saved variables. I'll look into that right now, and if it turns out that works for me I will feel pretty dumb.
 
But it seems to suffer the same problem as collections and hashes where I have to use a separate line for every entry
Like you said, "for each control it's nice to just use a loop", which is what you can do with a Dictionary using the controls name (key) and text (value) to fill it.
 
...You can do (array = {var1, var2, var3}. So why can't I do (collection/hash/dictionary= {Key1, value1, key2, value2, key 3, value3} etc...

In VB10 you can do this:

VB.NET:
Dim theDictionary As New Dictionary(Of String, String) From {{"a", "b"}, {"c", "d"}}
 
Hi MattP, thanks for that.

Also JohnH thanks for your help. I can't use a loop to add my variables to a collection/dictionary of some kind because that loop would have to operate on an existing collection/group of some kind (For Each item in Group1, .add(group2) etc). Kind of a chicken / egg thing.

Anyway, I think I am making progress towards solving this. I've got rid of all my saved variables and now I am hiding my forms so I can use all their contents still between form navigations. The only thing remaining to do is clean up the form closing events. If I close the last visible form, the other invisible forms are stuck open and the program does not end. I think this is why I wanted to save the variables in the first place. Anyway, I just have to come up with a clean way to end from any form close in my program unless it really is going to the next or previous form. Sounds a little tricky but I should be able to solve it.
 
I can't use a loop to add my variables to a collection/dictionary of some kind because that loop would have to operate on an existing collection/group of some kind
Like the collection of controls you're getting values from? These keys you have must come from a collection of some sort initially in either case.
If I close the last visible form
I can't reliably remember so far back as VB 2005, but at least for last two versions of VB there's a ShutDown Mode in project properties set to 'when last form closes' by default, and this can be set to 'when main form closes'. So that would solve that, but IMO I'd rather store the values and close the forms.
 
Back
Top