Solved

How to assign multiple variables the same value on one line

Posted on 2002-05-13
7
161 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 143

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

726 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