?
Solved

Use of 2 = in an expression?

Posted on 2003-03-11
9
Medium Priority
?
210 Views
Last Modified: 2010-05-01
I've seen a weird expression in VB which uses multiple equal signs in a single line.
For example:
    Dim sCMD_Line As String
    sCMD_Line = Command$() = ""
    MsgBox sCMD_Line   'Returns True if nothing was passed to command line.

It appears to be doing:
If Command$() = "" Then sCMD_Line = True Else sCMD_Line = False

My question is where does this come from (VB, Basic, Qbasic, etc.) and would there be any good use for this, also does this have any other uses?
0
Comment
Question by:Frog357
[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
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 101

Expert Comment

by:mlmcc
ID: 8115920
It probably comes from a C programmer.

I wouldn't use it because of readability issues.

mlmcc
0
 
LVL 1

Accepted Solution

by:
SilentRage earned 200 total points
ID: 8116002
I use it often.  It's an efficient way of storing boolean values.

Private Sub SetupForm()
  'If txtInput.Text is blank Enable will be set to false
  IsBlank = txtInput.Text <> ""

  'If IsBlank is true, enable these controls
  Control1.Enable = IsBlank
  Control2.Enable = IsBlank

  'If IsBlank is true, DISABLE these controls
  Control3.Enable = Not IsBlank
  Control4.Enable = Not IsBlank
End Sub

First I tested txtInput.Text to see if the user has provided some input.  If there is NO input, then I want controls 1 and 2 to be enabled, and 3 and 4 disabled.  If there IS input, then I want controls 1 and 2 to be disabled and 3 and 4 to be enabled.

Here's the typical way to set IsBlank to the proper value.

If txtInput.Text <> "" Then
    IsBlank = True
Else
    IsBlank = False
End If

Here's the more enlightened coder's way to set IsBlank

IsBlank = IIf(txtInput.Text <> "", True, False)

I think my favorite way is the best, takes less room, more readable than IIf's in my opinion - and fastest.  Of course, speed is negligable in this case, but in times when every millisecond counts... Remember this little trick.
0
 
LVL 1

Expert Comment

by:SilentRage
ID: 8116026
heh, switch out the <> with = and IsBlank will store the right value.

I wish we could edit our replies...
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 4

Author Comment

by:Frog357
ID: 8116064
I agree with the edit our replies too, but you deserve the points, thanks for your feedback.

Do you know where / when this comes from?  It doesn't seem like traditional basic to me, most code is easy to understand (in my perspective).
0
 
LVL 4

Expert Comment

by:TigerZhao
ID: 8116093
not agree to mlmcc
in C
 A = B = C
mean
 A = C
 B = C

in VB
 A = B = C
just mean
 A = (B = C)
equal to
 A = IIf(B = C, True, False)
also equal to
 If B = C Then
   A = True
 Else
   A = False
 End If

I offen use
 A = (B = C)
it's readable and effective
0
 
LVL 1

Expert Comment

by:SilentRage
ID: 8116129
I really have no idea when it originated, and I don't have a previous version of VB to test it on.  It just seems logical to me - not really a special feature.  It's just an expression.

bool = 1 + 2
bool = 1 < 2
bool = 1 > 2
bool = 1 = 2
bool = object1 Is object2
bool = object Is Nothing

in all those cases you're just using operators.  So logically using an = sign isn't strange at all.  It's just not normally thought of by newer programmers.
0
 
LVL 1

Expert Comment

by:SilentRage
ID: 8116142
bah, another edit wish:

everything you stick in a If statement may be assigned to a variable.

'Test to see if this expression is true
If (1 = 1) And (2 = 2) And (3 = 3) Then
...
End If

'Now just store the result of the expression
bool = (1 = 1) And (2 = 2) And (3 = 3)

I just wanted to get it more firmly ingrained in your head that If statements TESTS the VALUE of an expression.  While variables STORE the VALUE of an expression.
0
 
LVL 4

Author Comment

by:Frog357
ID: 8116341
When you put it together like:
bool = 1 + 2
bool = 1 < 2
bool = 1 > 2
bool = 1 = 2
bool = object1 Is object2
bool = object Is Nothing

I remember seeing something similar to this before!
I'm not a new programmer but I am a 100% self taught programmer so I find new things every day that I haven't taught myself yet ;)

Thanks again for the help!
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 8116618
Tiger.  I knew that but I assumed since it was in VB it was a translation from A= B==C

mlmcc
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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

752 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