1

I'm struggling with arrays.

I have some double variables that need to be covert to in or mm according to an RB button. I don't have problems with these, but the amount of variables its quite high, I was trying to put these variable into an array and multiple or divide them. But when I try to call the variable, the value stays as the original.

I notice that the array was taking the variable value not the variable itself.

Its there a way to make an array of variables? I've done some research but unfortunately nothing helps.

Here's a sample code of what I'm doing.

Public Shared Variable1, Varible2 as Double
Public Shared VarriablesArray() as Double

Variable1 = TextBox1.Text
Variable2 = TextBox2.Text

VariablesArray = {Variable1, Variable2}

For i = 0 to VariablesArray.GetUpperBound(0)
VariablesArray(i) = VariablesArray(i)*25.4
Next

Until here it works, but when I MsgBox Variable1 its the same value as TextBox1.Text not the value after math.

3
  • want to point out you have a spelling error Public Shared VarriablesArray() as Double *notice the two Rs but you reference it by VariablesArray Commented Mar 26, 2014 at 17:45
  • Converting units can be tricky. If a user click the convert button back and forth numerous times you'll get different numbers as the rounding errors add up. You might want to consider another method if a user is prone to this action. Commented Mar 26, 2014 at 17:47
  • There is a lot of implicit casting/conversion going on here as well. And I'm not sure the point of making everything static/shared. Commented Mar 26, 2014 at 17:51

2 Answers 2

1

Until here it works, but when I MsgBox Variable1 its the same value as TextBox1.Text not the value after math.

This is by design. Double values are copied into the array by value, so when you do the math on the array, you're not changing the original variable, only the "copy" stored in the array.

If you want this to go back into Variable1, you'd need to copy it back:

Variable1 = VariablesArray(0)
Variable2 = VariablesArray(1)
Sign up to request clarification or add additional context in comments.

Comments

0

Reed's explanation is correct. One thing you could do is create a new List or Array and put the modified elements in there. Here is an example. I used List but you could just as easily use Array or something else.

Dim variable1 As Double = 25.0
Dim variable2 As Double = 17.62
Dim variablesList As List(Of Double) = {variable1, variable2}.ToList
Dim newList As New List(Of Double)

For Each item As Double In variablesList
  item *= 25.4
  newList.Add(item)
Next

Console.WriteLine("variable1: " & variable1)
Console.WriteLine("variable2: " & variable2)
Console.WriteLine(Environment.NewLine & "variablesList:")

For Each item As Double In variablesList
  Console.WriteLine(item)
Next

Console.WriteLine(Environment.NewLine & "newList:")
For Each item As Double In newList
  Console.WriteLine(item)
Next

Console.ReadLine()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.