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
Printable View
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
VB Code:
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
You just have to change the square brackets
VB Code:
If ((j += nbrOfRanks(c)) > (N-5)) Then ... End If
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:
Private Sub BtnProvaCondizione_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnProvaCondizione.Click Dim J As Integer = 3 Dim nbo As Integer = 3 Dim N As Integer = 2 If (J += nbo) > N Then End If End Sub Private Function SubJ(ByVal NumJ As Integer, ByVal NumNbo As Integer) As Integer NumJ += NumNbo Return NumJ 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:
Private Function SubJ(ByVal NumJ As Integer, ByVal NumNbo As Integer) As Integer 'NumJ += NumNbo 'Return NumJ return NumJ += NumNbo 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! :rolleyes:
Could you correct my syntax or suggest me something to avoid this strange behaviour?
Thanks !:)
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.
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:
Private Sub BtnProvaCondizione_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnProvaCondizione.Click Dim J As Integer = 3 Dim nbo As Integer = 3 Dim N As Integer = 2 'If (J += nbo) > N Then If (J = J + nbo) > N Then End If End Sub Private Function SubJ(ByVal NumJ As Integer, ByVal NumNbo As Integer) As Integer 'NumJ += NumNbo 'Return NumJ 'return NumJ += NumNbo Return NumJ = NumJ + NumNbo 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! :p :p
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.
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!:)
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.
will assign b to a, butVB Code:
a = b
will not assign b to a, it will just test "does a equal b?"VB Code:
If (a = b) ...
As a side note, VB programmers moving to other languages sometimes get bit by this. They *might* write code that says
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.PHP Code:if (a = b) ...
The VB code for that would be
VB Code:
a = b 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.
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! :)