Solved

How to assign multiple variables the same value on one line

Posted on 2002-05-13
7
157 Views
Last Modified: 2011-09-20
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"
0
Comment
Question by:KGW22
7 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 7006160
That is actually valid code though it would get a result of False for A.

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")

0
 
LVL 33

Expert Comment

by:hongjun
ID: 7006164
Nope there's no way.


hongjun
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 7006169
You can have several lines of code on one line, using the : separator (btw this works also in the debug window)
A = "Test" : B = A : C = A

But you cannot assign to several variables at a time...
... unless you develop a proc like this to make it look like:

Private Sub SpreadValue(ByVal v _
  , Optional ByRef a _
  , Optional ByRef b _
  , Optional ByRef c _
  , Optional ByRef d _
  , Optional ByRef e _
  , Optional ByRef f _
  , Optional ByRef g _
)

  If Not IsMissing(a) Then a = v
  If Not IsMissing(b) Then b = v
  If Not IsMissing(c) Then c = v
  If Not IsMissing(d) Then d = v
  If Not IsMissing(e) Then e = v
  If Not IsMissing(f) Then f = v
  If Not IsMissing(g) Then g = v

End Sub

Usage example:

    Dim a, b, c
    SpreadValue "test", a, b, c
    Debug.Print a, b, c


CHeers
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 33

Expert Comment

by:hongjun
ID: 7006173
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
0
 
LVL 18

Expert Comment

by:deighton
ID: 7006230

a=2;
b=5;

if (b = a + a)
{
   puts("does this get printed");
}
0
 
LVL 15

Expert Comment

by:ameba
ID: 7006488
' 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
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7006665
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!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now