CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Oct 2009
    Posts
    28

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: multiple declaration

    Quote Originally Posted by pacerier View Post
    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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    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

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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.

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    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 ¿

  6. #6
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    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.

  7. #7
    Join Date
    Oct 2009
    Posts
    28

    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?

  8. #8
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    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

  9. #9
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: multiple declaration

    Hannes, the thread is already moved to VB.NET (Post #6)

  10. #10
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: multiple declaration

    Quote Originally Posted by Shuja Ali View Post
    Hannes, the thread is already moved to VB.NET (Post #6)
    Thanx for pointing that out! I need glasses!

  11. #11
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Re: multiple declaration

    Quote Originally Posted by HanneSThEGreaT View Post
    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.

  12. #12
    Join Date
    Oct 2009
    Posts
    28

    Re: multiple declaration

    what's the difference between vb 6 and vb.net? is vb.net a new version of vb6?

  13. #13
    Join Date
    Jul 2009
    Location
    Kuwait
    Posts
    170

    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

  14. #14
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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
  •  





Click Here to Expand Forum to Full Width

Featured