Declarations

How procedures and variables (objects) are declared.

Procedures/Methods

In many things, classical VB(A) and VB.NET are almost identical, as in the method signatures for procedures and methods. C# looks very different, but the same information is there, just in a different order and sometimes using different keywords. Once you understand the differences, it’s simple to go between the languages.

In the context of this discussion, a “method” is a procedure that executes actions but does not return a value. A “function” returns a value to the calling procedure.

Passing parameters by reference / by value

Parameters can be passed by reference or by value. By reference means that, if the code makes changes to the object that was passed, these will propagate back to the calling procedure. When something is passed in by value, a copy of the original is made and any changes will not propagate back to the original from the calling procedure.

By reference is the default when passing parameters in classic VB(A). By value is the default in VB.NET and C#.

Method declaration in VB-languages

Public Sub MyMethod(ByVal doc as Word.Document, ByRef counter as Integer)
   'Code comes here
End Sub

Method declaration in C# 

public void MyMethod(Word.Document doc, ref int counter)
{
   //Code comes here
}

Discussion

The VB languages indicate the start of a method through the keyword Sub; the end of the method is recognizable by the keyword combination End Sub. C# denotes the beginning of a method through the keyword void and the opening/closing bracket pair { }.

C# has no term for passing parameters by value as that’s the default. In the VB languages the default may be specified, or left out.

Function declaration in classic VB(A)

Private MyFunction as Boolean()
    Dim RetVal as Boolean
    RetVal = True ' or False
    MyFunction = RetVal
End Function

Function declaration in VB.NET

Private MyFunction as Boolean()
    Dim RetVal as Boolean = True ' or False
    Return RetVal
End Function

Function declaration in C#

private bool myFunction
{
    bool retVal = true; //or false
    return retVal;
}

Discussion

In the VB languages a function is recognizable by the keyword Function. The data type of the return value (or object) follows the function name and list of parameters, preceded by the keyword As.

In C# you can recognize a function by the substitution of a data type for the keyword void.

In VBA, in order to return the value it must be assigned to the name of the function. If you don’t do that, nothing is returned.

In the .NET languages use the keyword Return to return the value.

Variable/Object declaration

The logic behind the use of variables (the commonly used term in VB languages) / objects (C#)  in code is the same across all languages. Only the order of the parts of the declaration vary.

Variable declaration in VB.NET

Private MyString as String
Friend MyDoc as Word.Document

Object declaration in  C#

private string myString;
internal Word.Document myDoc;

Discussion

In the VB languages, the data type is preceded by the keyword As, following the name of the variable.

In C#, the data type precedes the variable/object name.

In classic VB(A) and in VB.NET with Option Strict Off, data type declaration is not enforced. If a variable is declared without assigning a data type it gets the data type Variant (for more, see the page Data Types).

The discussion about variables/objects continues in the topic instantiation.

Leave a Reply