Solved

How to assign multiple variables the same value on one line

Posted on 2002-05-13
7
163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

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!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

729 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