Solved

How to assign multiple variables the same value on one line

Posted on 2002-05-13
7
160 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

840 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