Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to assign multiple variables the same value on one line

Posted on 2002-05-13
7
Medium Priority
?
166 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 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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 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…
Suggested Courses

721 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