VB code conversion

Cags

Contributor
Joined
Feb 19, 2004
Messages
695
Location
Melton Mowbray, England
Can anybody tell me how this code can be written in c# as I don't fully understand it, thanks.
Visual Basic:
                If Not ((objType.Attributes And TypeAttributes.Abstract) = TypeAttributes.Abstract) Then
Also am I correct in believing this is a bitwise operation? Or have I misunderstood the code.
 
You are correct. And is bitwise and logical in VB. The only differences worth mentioning are that C# doesn't need the Then, and the different operators: And becomes &, = becomes ==, and Not becomes !.
C#:
if !((objType.Attributes & TypeAttributes.Abstract) == TypeAttributes.Abstract) {
}
 
marble_eater said:
You are correct. And is bitwise and logical in VB. The only differences worth mentioning are that C# doesn't need the Then, and the different operators: And becomes &, = becomes ==, and Not becomes !.
C#:
if !((objType.Attributes & TypeAttributes.Abstract) == TypeAttributes.Abstract) {
}

At the risk of being labelled a total geek, it's actually:
if (! ((objType.Attributes & TypeAttributes.Abstract) == TypeAttributes.Abstract))

;)
 
Jaco said:
At the risk of being labelled a total geek, it's actually:
if (! ((objType.Attributes & TypeAttributes.Abstract) == TypeAttributes.Abstract))

;)
Either that or this:
C#:
if ((objType.Attributes & TypeAttributes.Abstract) != TypeAttributes.Abstract) {
}
I guess I'm just to lazy to paste my code in a code editor to check for errors. Maybe I should add a disclaimer in my signature that none of my code is tested...
 
At the risk of beating a dead horse, doesn't the 'Then' keyword in VB translate into the open curly brace? and the 'End If' keywords translate to the close curly brace? (roughly)

Unless the vb if was meant to be a one liner like
Visual Basic:
If objTYpe = Something Then GoKillSomething()

But assuming it's not wouldn't the following be closer?

Visual Basic:
If objType = Something Then

Becomes

[csharp]
if (objType == Something) {
[/csharp]

The reasong being in the original line of code there was no 'end if' to represend the close curly brace.

Obviously not a finished piece of code, but it illustrates the point.
 
Okay, we have too many geeks beating dead farm animals here.

Arguably, one could say that the Then and End If keywords equate to curly braces since they surround the code block to be executed conditionally.

Alternatively (this is my take), one can argue that while the End If is completely analogous to the closing curly brace, the opening curly brace is only implied. The Then keyword is, to an extent, a relic from a time when the only If statement was a single-line If statement (though the Then would still be a necessity for a single-line If statement). Since in VB the conditional expression isn't required to be surrounded in parentheses, the Then is necessary to syntactically separate the conditional expression and the conditionally executed code statements, hence it might be more analogous to the closing parenthesis of the conditional expression (this is consistent with single-line If statements too), or possibly the combination of the closing parenthesis and the opening brace (the opening parenthesis would be implied).

Here is one interpretation of the syntactical analogy:
Code:
Multi-line
C#        VB
--------------
if(       If
    <conditional expression>
) {       Then
    <conditionally executed code>
}         End If

Single-line
C#        VB
--------------
if(       If
    <conditional expression>
) <code>  Then <code>

Take that, you effing horse.
 
Back
Top