I need to assign index to the following four different variables.
Dim first_name As String
Dim last_name As String
Dim age As String
Dim weight As String
These four lines need to be put in a For-Next statement
first_name = first_name_TextBox.Text
last_name = last_name_TextBox.Text
age = age_TextBox.Text
weight = weight_TextBox.Text
For-Next statement instead of the previous four lines
Dim TextBox_objects() As TextBox = {first_name_TextBox, last_name_TextBox, age_TextBox, weight_TextBox}
Dim string_variables_with_index() As Object = {first_name, last_name, age, weight} 'I need to replace this line with something functional
For i = 0 To TextBox_objects.GetUpperBound(0)
string_variables_with_index(i) = TextBox_objects(i).Text
Next
I spent some time searching for a solution to this problem but I did not find a solution. Thanks for any help.
Well, the issue is the difference between using a "object", and a "non object".
In effect, if you use strings in that "object" array, the problem is that a string in VB is not a object, but a string type.
And without using a object, then you can't really get a pointer to the object! The term is called "immutable" (strings in VB are not objects, or perhaps better stated they are immutable).
So, you need to use a "real" object for this to work, or better stated a "mutable" object type.
So, you would have to do it this way:
Dim first_name As New StringBuilder
Dim last_name As New StringBuilder
Dim age As New StringBuilder
Dim weight As New StringBuilder
And then the assignment line would then be:
string_variables_with_index(i).Append(TextBox_objects(i).Text)
And then you can now use EITHER the index, or say this:
debug.print(weight.ToString())
I think the above approach (even if strings WERE mutiable)? Its clunky - not really improving your code, or even the amount of code you have to type here.
What gains to you make by having to type by hand all the textboxes into a new array defined? You STILL having to type in all the textbox names by hand again, and you STILL having to type in all the string variable names AGAIN by hand into that object array, right?
So, while using a StringBuilder object (mutable) will work? You not really saving much, if anything here. And on top of this, you THEN have to write a for/next loop for this to work.
And it not really clear why you need (or want) to write code this way? So, this is ONE BIG case in which you should describe what you actual goal here is?
Say you trying to return values for a form?
Then set the form visible = false, and use a variable with a reference to the form, that way you can/are passing around ALL values (all textbox and other controls can thus be used, even things like checkboxes etc.) by referencing the form object.
And, since you ARE and you HAVE to declare the strings?
And then then re-type the string variables AGAIN to put them into that array?
Such code is VERY error prone due to one having to type in all those controls, and all those variables, and the "order" and "alignment" is VERY easy to mess up. So, now you have very "touchy" code, that with great ease you can make mistakes with. (hint: better coders are not actually better, they just write code that easer to write and maintain in the first place!, so, then they never have to deal with such difficult code later on).
And your example is simple. What happens when you have 25 controls? This fast becomes too much work, and it's error prone.
One simple mis-aligned textbox, or variable, and your code is broken.
I would suggest you dump your declare of string variables, and use some type of collection, especially a dictionary which is ideal for this type of problem.
And for the about the "same" efforts as just 4 textboxes?
Same amount of work to create a "general" routine that sends all the TextBox values to that dictionary list.
Say like this:
Dim fVars As New Dictionary(Of String, String)
Call LoadValues(Me, fVars)
Debug.Print(fVars("first_name"))
Debug.Print(fVars("last_name"))
So, with above, it don't matter if you have 5 controls, or 25. And we don't need to declare 25 variables either, do we?
The LoadValues routine (which could be a global utility routine you write).
This:
Public Sub LoadValues(f As Form, kV As Dictionary(Of String, String))
For Each c As Control In f.Controls
If TypeOf c Is TextBox Then
kV.Add(c.Name, c.Text)
End If
Next
End Sub
And, of course really nice? You can add more control types to the above. So, say a checkbox, combo box or whatever could be added to the above code (right now, it only supports textbox, but more control types could be added).
So, you call this routine, and now you have a nice "key pair" of values for all controls. And this code is re-usable.
And, you can even pass it around to other routines.
So, .net has a rather "handy" built in dictionary collection type.
You could even use the above approach on form load to say get/grab/save the existing values, and then on save or even during editing, you could highlight controls changed by a user into a different color. So, this approach is not a "one off" effort, but gives rise to all kinds of use cases.