KGW22
asked on
How to assign multiple variables the same value on one line
Is there a way to assign multiple variables the same value and do this on one line. For example,
Dim A as variant, B as Variant, C as Variant
A = B = C = "Test"
The result would be that:
A = "Test"
B = "Test"
C = "Test"
Dim A as variant, B as Variant, C as Variant
A = B = C = "Test"
The result would be that:
A = "Test"
B = "Test"
C = "Test"
Nope there's no way.
hongjun
hongjun
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
VB will do this
A = B = 2
A = False ' false because B is not 2
Msgbox A ' give u false
Msgbox B ' give u nothing because it is not assigned
VB is unlike C programming.
hongjun
A = B = 2
A = False ' false because B is not 2
Msgbox A ' give u false
Msgbox B ' give u nothing because it is not assigned
VB is unlike C programming.
hongjun
a=2;
b=5;
if (b = a + a)
{
puts("does this get printed");
}
' that syntax won't work, but to 'expand a language', you can create function:
' Form1 code
Option Explicit
Private Sub Form_Click()
Dim A As Variant, B As Variant, C As Variant
SetValues "Test", A, B, C
Print A, B, C
End Sub
' assigns value to multiple variables
Sub SetValues(ByVal Value As String, ParamArray Variables())
Dim i As Long
For i = 0 To UBound(Variables)
Variables(i) = Value
Next
End Sub
' Form1 code
Option Explicit
Private Sub Form_Click()
Dim A As Variant, B As Variant, C As Variant
SetValues "Test", A, B, C
Print A, B, C
End Sub
' assigns value to multiple variables
Sub SetValues(ByVal Value As String, ParamArray Variables())
Dim i As Long
For i = 0 To UBound(Variables)
Variables(i) = Value
Next
End Sub
VB requires assignments as one entry per logical line. However, logical lines can be separated by a colon (:). This is not recommended because it reduces visibility.
Programmers often don't care about readability and this often leads to bugs.
As angelIII indicated, you can use
A = "Test" : B = A : C = A
but the following is much more readable and therefore much less likely to create a bug when this code needs maintenance:
A = "Test"
B = A
C = A
(And, of course, your variables should be named such that they are as self-documenting as possible:
strTest1 = "Test"
strTest2 = strTest1
strTest3 = strTest1
Furthermore, variants are highly frowned upon (Dim A as variant, B as Variant, C as Variant) and should only be used where there is a specific need such as when VB requires it for a function.
Most modern-day developers agree that readable code is much more important than "efficient" code since maintenance issues will introduce many more bugs in complex efficient code than they will in readable code, and bugs are what cause code to be abandoned or patched to the point of becoming inefficient!
Programmers often don't care about readability and this often leads to bugs.
As angelIII indicated, you can use
A = "Test" : B = A : C = A
but the following is much more readable and therefore much less likely to create a bug when this code needs maintenance:
A = "Test"
B = A
C = A
(And, of course, your variables should be named such that they are as self-documenting as possible:
strTest1 = "Test"
strTest2 = strTest1
strTest3 = strTest1
Furthermore, variants are highly frowned upon (Dim A as variant, B as Variant, C as Variant) and should only be used where there is a specific need such as when VB requires it for a function.
Most modern-day developers agree that readable code is much more important than "efficient" code since maintenance issues will introduce many more bugs in complex efficient code than they will in readable code, and bugs are what cause code to be abandoned or patched to the point of becoming inefficient!
No is the simple answer, you cannot do it on one line.
You could do it with arrays in a sense:
Dim Ary As Variant
Ary = Array("Test","Test","Test"