Methods


Methods are always declared within a class. The syntax from C++/CLI is very similar to C# with the exception that the access modifier is not part of the method declaration but is written before that. The access modifier must be ended with a colon. With Visual Basic, the Sub keyword is used to define a method.

  // C# public class MyClass {    public void Foo()    {    } } // C++/CLI public ref class MyClass { public:    void Foo()    {    } }; ' Visual Basic Public Class MyClass    Public Sub Foo    End Sub End Class 

Method Parameters and Return Types

With C# and C++/CLI, parameters that are passed to methods are defined inside a bracket. The type of the parameter is declared before the variable name. Returning a value from a method, the method is defined with the type to return instead of void.

Visual Basic uses Sub statements to declare a method without returning a value, and the Function statement with a method that does have a return type. The return type is followed after the method name and the brackets.

  // C# public class MyClass {    public int Foo(int i)    {       return 2 * i;    } } // C++/CLI public ref class MyClass { public:    int Foo(int i)    {       return 2 * i;    } }; ' Visual Basic Public Class MyClass    Public Sub Foo1(ByVal i as Integer)    End Sub    Public Function Foo(ByVal i As Integer) As Integer       Return 2 * i    End Sub End Class 

Parameter Modifiers

By default, value types are passed by value, and reference types are passed by reference. If a value type that is passed as a parameter should be changed within a calling method, with C# you can use the parameter modifier ref.

C++/CLI defines a managed reference operator %. This operator is similar to the C++ reference operator & with the difference that % can be used with managed types and the garbage collector can keep track of these objects in case they are moved within the managed heap.

With Visual Basic the keyword ByRef is used for passing parameters by reference:

  // C# public class ParameterPassing {    public void ChangeVal(ref int i)    {       i = 3;    } } // C++/CLI public ref class ParameterPassing { public:    int ChangeVal(int% i)    {       i = 3;    } }; ' Visual Basic Public Class ParameterPassing    Public Sub ChangeVal(ByRef i as Integer)       i = 3    End Sub End Class 

When invoking a method with reference parameters, only the C# language requires you to apply a parameter modifier. C++/CLI and Visual Basic don’t differentiate calling a method with or without the parameter modifier. C# has the advantage here that you can immediately see in the calling method that the parameter values can be changed.

Because of the caller syntax, which is not differentiated, Visual Basic does not allow overloading methods just by changing the modifier. The C++/CLI compiler allows overloading the method just by changing the modifier, but cannot compile the caller because the resolved method is ambiguous. With C# it is possible to overload and use methods with just the parameter modifier, but it’s not a good programming practice.

  // C#       ParameterPassing obj = new ParameterPassing();       int a = 1;       obj.ChangeVal(ref a);       Console.WriteLine(a);      // writes 3 // C++/CLI       ParameterPassing obj;       int a = 1;       obj.ChangeVal(a);       Console.WriteLine(a);      // writes 3 ' Visual Basic       Dim obj as new ParameterPassing()       Dim i as Integer = 1       obj.ChangeVal(i)       Console.WriteLine(i)      // writes 3 

Tip 

C# also defines the out keyword when a parameter is just returned from a method. This option is not available from C++/CLI and Visual Basic. As long as the caller and callee are in the same application domain, there’s really no difference between out and ref behind the scenes, and you can use a method declared with the C# out parameter modifier from Visual Basic and C++/CLI in the same way as ref parameter modifiers. If the method is used across application domains or processes, the attribute [out] can be used with Visual Basic and C++/CLI.

Constructors

Both with C# and C++/CLI, the constructor has the same name as the class. Visual Basic uses a procedure named New. The this and Me keywords are used to access a member of this instance. When invoking another constructor within a constructor, with C# a member initialize is required. With C++/CLI and Visual Basic, it is possible to invoke the constructor as a method.

  // C# public class Person {    public Person()       : this("unknown", "unknown")    { }    public Person(string firstname, string lastname)    {       this.firstname = firstname;       this.lastname = lastname;    }    private string firstname;    private string lastname; } // C++/CLI public ref class Person { public:    Person()    {       Person("unknown", "unknown");    }    Person(String^ firstname, String^ lastname)    {       this->firstname = firstname;       this->lastname = lastname;    } private:    String^ firstname;    String^ lastname; }; ' Visual Basic Public Class Person    Public Sub New()       Me.New("unknown", "unknown")    End Sub    Public Sub New(ByVal firstname As String, ByVal lastname As String)       Me.MyFirstname = firstname       Me.MyLastname = lastname    End Sub    Private MyFirstname As String    Private MyLastname As String End Class 

Properties

To define a property, C# just requires a get and set accessor within a property block. With the set accessor the variable value is automatically created by the C# compiler. This is different both with C++/CLI and Visual Basic. Both of these languages have a property keyword, and it is necessary to define a variable value with the set accessor. C++/CLI also requires a type with the get accessor.

As an extra feature, C++/CLI has a short version of writing a property. Using the property keyword, you just have to define the type and the name of the property; the get and set accessors are created automatically by the compiler. If there’s nothing else needed than setting and returning a variable, the short version is good enough. If the implementation of the accessors requires more, for example, checking the value or doing a refresh, you must write the full syntax for properties.

  // C# public class Person {    private string firstname;    public string Firstname    {       get { return firstname; }       set { firstname = value; }    } } // C++/CLI public ref class Person { private:     String^ firstname; public:    property String^ Firstname    {       String^ get()       {          return firstname;       }       void set(String^ value)       {          firstname = value;       }    }    property String^ Lastname; }; ' Visual Basic Public Class Person     Private myFirstname As String     Public Property Firstname()         Get             Return myFirstname         End Get         Set(ByVal value)             myFirstname = value         End Set     End Property     Private myLastname As String     Public Property Lastname()         Get             Return myLastname         End Get         Set(ByVal value)             myLastname = value         End Set     End Property End Class 

With C# and C++/CLI, read-only properties just have a get accessor. With Visual Basic, you must also specify the ReadOnly modifier. Write-only properties must be defined with the WriteOnly modifier and a set accessor.

  ' Visual Basic     Public ReadOnly Property Name()         Get             Return myFirstname & " " & myLastname         End Get     End Property 




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net