Results 1 to 10 of 10

Thread: Simple One line C# to vb translation

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36

    Simple One line C# to vb translation

    I am trying to translate this code to vb.net I am sure its very simple, but I cant figure out the syntax.

    if ((j += nbrOfRanks[c]) > (N-5))

    its the conditional statment that is the problem +=

    Thanks,
    -ST

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    VB Code:
    1. if ( (j=j + nbrOfRanks(c)) > (N-5) )

    I think thats it
    j+= is the same as j=j+
    and I don't think VB.Net uses square brackets for arrays

  3. #3
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    You just have to change the square brackets
    VB Code:
    1. If ((j += nbrOfRanks(c)) > (N-5)) Then
    2. ...
    3. End If
    \m/\m/

  4. #4
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Der PT, perhaps you can help me.
    I think it could be a problem of some setting in the IDE, but, also if it wonders me, I continue to have the problem that Sooner Toucan expose. For example:

    VB Code:
    1. Private Sub BtnProvaCondizione_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnProvaCondizione.Click
    2.         Dim J As Integer = 3
    3.         Dim nbo As Integer = 3
    4.         Dim N As Integer = 2
    5.  
    6.         If (J += nbo) > N Then
    7.  
    8.         End If
    9.  
    10.  
    11.     End Sub
    12.  
    13.  
    14.     Private Function SubJ(ByVal NumJ As Integer, ByVal NumNbo As Integer) As Integer
    15.  
    16.         NumJ += NumNbo
    17.         Return NumJ
    18.  
    19.     End Function

    In the first sub '+=' is underlining and compiler doesn't accept it!
    I try with a lot of combination of brackets, but I can't solve the problem.

    The second one it's ok, but if I try to change it in:


    VB Code:
    1. Private Function SubJ(ByVal NumJ As Integer, ByVal NumNbo As Integer) As Integer
    2.  
    3.         'NumJ += NumNbo
    4.         'Return NumJ
    5.         return NumJ += NumNbo
    6.     End Function

    Putting the instructions on a unique line, the problem comes back and compiler refuse it!

    I tried to copy your posted code, changing nbrOfRanks(c) in nbo, but i had the same problem!

    Could you correct my syntax or suggest me something to avoid this strange behaviour?
    Thanks !
    Live long and prosper (Mr. Spock)

  5. #5
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Just a guess here, but I think this is a difference between c# and vb. That is, c# supports an "assignment within expression", but vb does not. Of course, I may be wrong.

  6. #6
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Thank you for your reply Mike
    I presume you verified a compiler's behaviour similar to mine.
    It's possible we have some wrong setting, or PT Ex. could not have verified his code. If it's so, the question is: why I can write this without problem?

    VB Code:
    1. Private Sub BtnProvaCondizione_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnProvaCondizione.Click
    2.         Dim J As Integer = 3
    3.         Dim nbo As Integer = 3
    4.         Dim N As Integer = 2
    5.  
    6.         'If (J += nbo) > N Then
    7.         If (J = J + nbo) > N Then
    8.  
    9.         End If
    10.  End Sub
    11.  
    12.  
    13.     Private Function SubJ(ByVal NumJ As Integer, ByVal NumNbo As Integer) As Integer
    14.  
    15.         'NumJ += NumNbo
    16.         'Return NumJ
    17.         'return NumJ += NumNbo
    18.         Return NumJ = NumJ + NumNbo
    19.     End Function

    I believed that J+=nbo meant the same as J=J+nbo....only a shorter way to give the same order to compiler.
    Now I discover it's different! Really....every day it's possible to learn something new!
    Live long and prosper (Mr. Spock)

  7. #7
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Unfortunately, the equal sign in VB is confusing. C# is nice because it has = and ==. I think that's what's going on here.

    Correct me if I'm wrong, but I don't think it's a setting. Guessing the PT Exorcist did not check that code in the IDE. No offense PT Exorcist, just a guess.

    j += nbo is way different that j = j + nbo if it's used as an expression and not an assignment. j += nbo assigns j to j + nbo. Now if you just had a line that said j = j + nbo, then it does the same thing. But, when used as an expression, like if ( j = j + nbo), it actually returns a boolean - "is j equal to j plus nbo?"

    Same thing when you return NumJ = NumJ + NumNbo, you're returning a boolean. Booleans in VB are stored as 2 byte numbers, so there's no type mix up. Confusing, no doubt, but that's VB.

  8. #8
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    I don't know C#, neither C++, but I've heard something sbout = and ==
    I think you are probably right.
    Thank for your help, now the situation has a sense!
    Live long and prosper (Mr. Spock)

  9. #9
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Sorry, don't mean to confuse by referring to some other language. In C#, and other languages, there is a distinction when it comes to "equals". VB seems to take care of this for you behind the scenes, which is quite convenient, but leads to confusion at the same time (as in this case).

    In C#, = is the assignment operator, that is, a = b will assign the value b to variable a. a == b is the equality operator, that is, "does a equal b?".

    In VB, you always use =, which is used for both assignment and equality.
    VB Code:
    1. a = b
    will assign b to a, but
    VB Code:
    1. If (a = b) ...
    will not assign b to a, it will just test "does a equal b?"

    As a side note, VB programmers moving to other languages sometimes get bit by this. They *might* write code that says
    PHP Code:
    if (b) ... 
    when they're trying to test for equality, not realizing that they are assigning a to b, and then acutally testing for the value of a.

    The VB code for that would be
    VB Code:
    1. a = b
    2. If a ...

    Not only do you change the value of a, but you don't even end up testing what you thought. Nasty.

    I guess this all goes back to an "assignment within expression". C# can do this, VB cannot.

  10. #10
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Very interesting. I was looking for something to refresh my memory about the use of = and == , but you prevent me!
    Things like these are basics, but sometimes it happens they reveal themselves later than at the beginning. I'm using Vb.Net from an year about,(I have always used Assembler for firmware, before), but only now I realize this...thank you, Mike!
    Live long and prosper (Mr. Spock)

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