Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assignment or Equality

Status
Not open for further replies.

RichardF

Programmer
Oct 9, 2000
239
0
0
GB
Code:
        If (iKeyId = CInt(InputBox("Enter Key ID"))) Then
            Stop
        End If

        iKeyId = CInt(InputBox("Enter Key ID"))
        If iKeyId Then

In the first one iKeyID will always be 0.

I believe this is because VB thinks i want to do compare and not assign. Does anyone know a way round this or shall i keep with method B.....

Rich.





Programmers are tools for converting caffeine into code
 
The first method indeed compares the current value of iKeyId with whatever is typed in the inputbox. The second one first assign a new value to iKeyId and then compares it to True.

The first method will work in C++ for example, but not in VB. VB doesn't have a distinct operator for assigning (=) or comparing (==). It depends on the context of the code.

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
I would go with method B in any case, as it gives you the ability to inspect the value coming back from the InputBox while in Debug. Nice to have while troubleshooting. :)

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks for your responses.

Chiph:
I agree, although it doesnt apply in this particular situation (due to the simplicity of the statement), it would most certainly be useful in more complex situations. And some would argue it improves readability.

Coming from a C++ background i personally prefer using method A but have no problem having to use method B.


Ruffneck:
Thats the problem when C programmers work with VB !! I would be interested to see how C# handles the equivalent.


Regards,
Rich.
 
I think c# will handle it pretty much the same as c++

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
What may be confusing it is you're inside an if-test, but aren't actually testing anything. Try something like this:
Code:
if ((iKeyId = CInt(InputBox("Enter Key ID"))) > 0) Then
Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Unfortunately, that didnt work.

Also if Option Strict is On then you also get a compile msg :
Option Strict On disallows implicit conversions from 'Boolean' to 'Integer'.


Best i could come up with is :
Code:
        If iKeyID.Parse(InputBox("Enter Key ID")) > 0 Then
            Stop
        End If

Regards,
Rich



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top