
32
|
C# & VB.NET Conversion Pocket Reference
same signature, you must fully qualify the name by adding
the name of the module, just as if you were invoking a
Shared method from the class.
If you are writing a class in C# to be used in VB.NET and
want to make it look like a module, you can simply mark
every member in the class as static, then reference the
Microsoft.VisualBasic.DLL assembly and add the
StandardModule attribute (from the Microsoft.VisualBasic.
CompilerServices namespace) to your class, as follows:
[Microsoft.VisualBasic.CompilerServices.StandardModule]
public class Math
{
public static int Add(int num1, int num2)
{
return num1+num2;
}
}
A VB project can then refer to the Add function directly with-
out the class name.
If Statements
Both languages have conditional if statements. However, the
syntax and semantics differ between the languages. In C#,
the conditional statement must be enclosed in parentheses,
as follows:
if (x > 10)
DoSomething( );
Notice that if is in lowercase, there are parentheses sur-
rounding the conditional statement, and that unlike in VB,
there is no need for
Then at the end. This example shows an
if statement with only one task to perform. If there are mul-
tiple tasks to perform, they are enclosed in curly brackets, as
follows:
if (x < 10)
{
DoTask1( );
C#
C#
C#