-
October 25th, 2009, 01:38 PM
#1
multiple declaration
ok this sounds dumb but i just wanted to do something simple
Code:
dim this, that, alll, duno, etc, anddd, more as integer
this = that = etc = more = 6
doesn't work, is there a way to do this in vb?
-
October 25th, 2009, 02:06 PM
#2
Re: multiple declaration
Originally Posted by pacerier
ok this sounds dumb but i just wanted to do something simple
Code:
dim this, that, alll, duno, etc, anddd, more as integer
this = that = etc = more = 6
doesn't work, is there a way to do this in vb?
Nope. Plus, in the DIM statement, only more is an Integer. Everything in RED is actually a VARIANT, and slower to use.
Declare each variable's type. You can include multiple per statement.
Assigning a value to more than one item isn't allowed.
Code:
Dim this as Integer, that as Integer, etc as Integer
this = 6 : that = 6 : etc = 6 : more = 6
-
October 26th, 2009, 08:08 AM
#3
Re: multiple declaration
Doing this :
Code:
Dim this As Integer, that As Integer, alll As Integer, duno As Integer, etc As Integer, anddd As Integer, more As Integer
more = 6
etc = 7
that = 5
this = that = etc = more
MsgBox that
Would give me 5
Doing this :
Code:
Private Sub Form_Load()
Dim this As Integer, that As Integer, alll As Integer, duno As Integer, etc As Integer, anddd As Integer, more As Integer
more = 6
etc = 7
that = 5
that = etc = more
MsgBox this
End Sub
Would give 0
-
October 26th, 2009, 08:37 AM
#4
Re: multiple declaration
An expression A=B=C will evaluate like this:
A is assigned the result of B=C, which is a boolean expression like in the if statement.
If B equals C then A will become True, if B is different to C, A becomes False.
If A however is not a boolean, but an integer, it becomes -1 for true and 0 for false.
A multi assignment statement will likewise evaluate:
A=B=C=D=6.
If all variables are assumed 0, then D=6 will be false, so we have
A=B=C=0
C is 0 will be true, being -1 in an integer representation, so we have
A=B=-1
So B=-1 becomes 0 (false) for B being 0. This reduces the evaluation to
A=0
That's what the result should turn out.
-
October 26th, 2009, 08:54 AM
#5
Re: multiple declaration
The whole point I'm still trying to figure out as well, is why is this even allowed in the compiler. Surely it should be ¿
-
October 26th, 2009, 02:24 PM
#6
Re: multiple declaration
[Moved to VB.NET]
Sorry about bumping the thread, but OP is actually asking a question related to VB.NET. I have moved few of the threads to VB.NET forum earlier that were posted by the same member.
So in vb.net dim this, that, alll, duno, etc, anddd, more as integer would declare all of them as integers.
-
October 27th, 2009, 09:10 AM
#7
Re: multiple declaration
hey sry what exactly is the difference between vb and vb.net?
im using microsoft visual basic 2008, is that vb.net?
-
October 27th, 2009, 09:18 AM
#8
Re: multiple declaration
Yes, that is VB.NET, in this case
This :
http://www.codeguru.com/forum/forumdisplay.php?f=12
Is the VB.NET Forum.
Where you posted was for VB 6, mainly
-
October 27th, 2009, 12:35 PM
#9
Re: multiple declaration
Hannes, the thread is already moved to VB.NET (Post #6)
-
October 28th, 2009, 12:50 AM
#10
Re: multiple declaration
Originally Posted by Shuja Ali
Hannes, the thread is already moved to VB.NET (Post #6)
Thanx for pointing that out! I need glasses!
-
October 28th, 2009, 02:33 AM
#11
Re: multiple declaration
Originally Posted by HanneSThEGreaT
The whole point I'm still trying to figure out as well, is why is this even allowed in the compiler. Surely it should be ¿
The reason it is allowed is because it is correct syntax.
As WoF explains, VB.NET (possible VB as well?) treat the = after the first assignment as boolean comparisons and not as assignments.
It is a syntax silliness in my book doing nothing but add confusion, but it is perfectly legal VB.NET syntax.
-
October 29th, 2009, 03:07 AM
#12
Re: multiple declaration
what's the difference between vb 6 and vb.net? is vb.net a new version of vb6?
-
October 29th, 2009, 03:50 AM
#13
Re: multiple declaration
There are a lot of articles in internet about the difference between vb6 and vb.net.
Check this link
http://www.thescarms.com/vbasic/vb6vsvbnet.aspx
-
October 29th, 2009, 10:33 AM
#14
Re: multiple declaration
To explain why is the syntax A=B=C is logically allowed:
It is simply to store the result of a comparison.
B=C, when used as a term, is a boolean expression which evaluates to true or false. (Or will be treated as-1 and 0, if the other terms are numerical).
A=B=C replaces the statement If B=C Then A=True Else A=False
Likewise you can do A=B>C or A=B<=C to keep results of comparisons in A.
A=B=C=D must therefore be syntactically correct, although it does not make much sense.
I use this kind of conditional often when having to switch an array of controls on and off.
Say you have an array of 10 checkboxes and you want box 0 to 4 enabled, box 5 to 9 disabled, not knowing what their current state is.
Code:
For i=0 to 9
chkBox(i).Enabled = i<5
Next
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|