?
Solved

Use of 2 = in an expression?

Posted on 2003-03-11
9
Medium Priority
?
216 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
  • 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
Industry Leaders: 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 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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…
Suggested Courses

616 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