IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Cross Ref Guide
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesVB.NetOOP Basics  Print This     

Cross Ref > OOP Basics

By Mike Prestwood

VB.Net versus C#: A side by side comparison between VB.Net and C#.

 
OOP Basics
 

Some languages support object-based concepts such as Paradox, Access, and VB Classic. Other languages have OO extensions and fully support object orientation in a hybrid fashion (such as C++ and Dephi for Win32). Finally, some lanages such as C#, VB.Net, Prism, and Java are entirely written in OO. Meaning, every line of code written must occur within a class).

Base Class

[Other Languages] 

Languages Focus

When you create a class, it is either a base class or inherits from another class. Some languages require all classes to inherit from a common base class and some do not.

VB.Net:   System.Object

In VB.Net, the Object keyword is an alias for the base System.Object class and is the single base class all classes ultimately inherit from. Use the Inherits keyword to indicate the parent class and Inherits must precede all declarations in a class.

Syntax Example:  
'Specify both namespace and class:
Public Class Cyborg
Inherits System.Object
End Class
  
'Use Object alias for System.Objct:
Public Class Cyborg
Inherits Object
End Class
  
'Default when not specified is System.Object:
Public Class Cyborg
End Class
C#:   System.Object

In C#, the Object keyword is an alias for the base System.Object class and is the single base class all classes ultimately inherit from.

Syntax Example:  
//Specify both namespace and class:
public class Cyborg : System.Object 
{ }
  
//Use shortcut alias:
public class Cyborg : Object 
{ }
  
//None, default is System.Object
public class Cyborg
{ }




Class..Object

[Other Languages] 

Languages Focus

In short, a class is a data type, and an object is an instance of a class type. A class has methods (routines), properties (member variables), and a constructor. The current values of the properties is the current state of the object. The UML is one of the diagraming disciplines that allows you to document the various changing states of a series of objects.

VB.Net:   Class..End Class..New

Declare and implement VB.Net classes after the form class or in their own .vb files. Unlike VB Classic, you can have more than one class in a .vb class file (VB classic uses .cls files for each class).

Syntax Example:

Class definition:

Public Class Cyborg
  Inherits Object
 
  Public Sub IntroduceYourself()
  MessageBox.Show("Hi, I do not have a name yet.")
 End Sub
End Class

Some event like a button click:

Dim T1 As New Cyborg
T1.IntroduceYourself()
//No need to clean up with managed classes.
//The garbage collector will take care of it.
C#:   class...new

In C#, you use the class keyword to specify a class and you signify its parent with a colon and the name of the parent class. When you instantiate an object from a class, you use the new keyword.

Syntax Example:

Define class:

public class Cyborg : System.Object
{
public virtual void IntroduceYourself()
{
MessageBox.Show("Hi, I do not have a name yet.");
}
}

Create object from class:

Cyborg T1 = new Cyborg();
T1.IntroduceYourself();
//No need to clean up with managed classes.
//The garbage collector will take care of it.




Inheritance

[Other Languages] 

The concept of a class makes it possible to define subclasses that share some or all of the main class characteristics. This is called inheritance. Inheritance also allows you to reuse code more efficiently. In a class tree, inheritance is used to design classes vertically. (You can use Interfaces to design classes horizontally within a class tree.) With inheritance, you are defining an "is-a" relationship (i.e. a chow is-a dog). Analysts using UML call this generalization where you generalize specific classes into general parent classes.

VB.Net:   Inherits ParentClass

VB.Net uses the Inherits keyword followed by the parent class name. If you do not include Inherits, your class inherits from System.Object.

Syntax Example:

In the following example, a terminator T-600 is-an android. 

Public Class Android
End Class
 
Public Class T-600
  Inherits Android
End Class
C#:   : ParentClass

In C#, you use the class keyword followed by the parent class after a colon. If you leave out the parent class, your class inherits from System.Object.

Syntax Example:

In the following example, a terminator T-600 is-an android. 

public class Android
{
}
 
public class T-600 : Android
{
}




Member Field

[Other Languages] 

Also known as a Class Field.

A class variable defined with a specific class visibility, usually private visibility. A member property is different than a member field. A member property uses a member field to store values through accessor methods (getters and setters). For example, it is common to use a private member field to store the current value of a property. The current values of all the class member fields is the current state of the object.

Languages Focus

What modifiers apply to member fields, if any? Typical member field modifiers include scope modifiers (private, protected, etc.) and read-only. Can you initialize the value of a member field when declared ensuring a default value?

VB.Net: 

In VB.Net you can set the visibility of a member field to any visibility: private, protected, public, friend or protected friend.

You can intialize a member field with a default when declared. If you set the member field value in your constructor, it will override the default value.

Finally, you can use the Shared modifier (no instance required) and ReadOnly modifier (similar to a constant).

Syntax Example:
Public Class Cyborg
  Private FSerialNumber As String = "A100"
 
  Public CyborgName As String
  Public CyborgAge As Integer
  Public Shared ReadOnly SeriesID As Integer = 100
End Class
C#: 

In C# you can set the visibility of a member field to any visibility: private, protected, public, internal or protected internal.

You can intialize a member field with a default when declared. If you set the member field value in your constructor, it will override the default value.

Finally, you can use the static modifier (no instance required) and readonly modifier (similar to a constant).

Syntax Example:
public class Cyborg : System.Object
{
  private string serialNumber = "A100";
 
  public string cyborgName; 
  public int cyborgAge = 0;
  public static readonly int seriesID = 100;
}




Member Method

[Other Languages] 

Also known as a Class Method.

A code routine that belongs to the class or an object instance (an instance of the class). Methods that belong to the class are called class methods or static methods. Methods that belong to an object instance are called instance methods, or simply methods.

When a method returns a value, it is a function method. When no value is returned (or void), it is a procedure method.

Methods frequently use method parameters to transfer data. When one object instance calls another object instance using a method with parameters, you call that messaging.

VB.Net:   Sub, Function

VB.Net�uses the keywords sub and function. A sub does not return a value and a function does. Many programmers like to use the optional call keyword when calling a sub to indicate the call is to a procedure.

Syntax Example:

Class definition:

Public Class Cyborg
� Inherits Object
 
Public Sub IntroduceYourself()
��� MessageBox.Show("Hi, I do not have a name yet.")
� End Sub
End Class

Some event like a button click:

Dim T1 As New Cyborg
T1.IntroduceYourself()
C#: 

In C#, you indicate a method with parens, no parens indicates a member property. Use the void return type for methods that do not return a value.

Syntax Example:

Define class:

public class Cyborg : System.Object
{
public virtual void IntroduceYourself()
{
MessageBox.Show("Hi, I do not have a name yet.");
}
}

Create object from class:

Cyborg T1 = new Cyborg();
T1.IntroduceYourself();




Member Modifier

[Other Languages] 

Languages Focus

Traditional private, protected, public, etc. member modifiers are documented under the member visibility topic of the Cross Reference Encyclopedia. With member modifiers here, we address additional member modifiers such as method and field modifiers.

VB.Net:  "Member Modifiers"

The method modifiers include�MustOverride,�NotOverridable, Overridable, Overrides. Specify�VB.Net member modifiers as follows:

Public Overrides Function SomeFunction() As Double

The field modifiers include ReadOnly and Shared. Specify field modifiers as follows:

Public ReadOnly SomeField As String

More Info / Comment
C#:  "Member Modifiers"

The method modifiers are abstract, extern, new, partial, sealed, virtual, and override. Specify C# member modifiers as follows:

abstract SomeMethod() {..}

The field modifiers are const, readonly, static, volatile. Specify field modifiers as follows:

readonly int MyAge;

More Info / Comment




Member Property

[Other Languages] 
VB.Net:   property, get, set

VB.Net uses a special property keyword along with special get and set methods to both get and set the values of properties. For a read-only property, leave out the set method. The value keyword is used to refer to the member field. Properties can make use of any of the access modifiers (private, protected, etc).

My preference for VB.Net code is to start member fields with "F" ("FName" in our example) and drop the "F" with properties that manage member fields ("Name" in our example).

Syntax Example:
Public Class Cyborg
Private FCyborgName As String
 
  Public Property CyborgName()
Get
Return F
CyborgName
End Get
 
Set(ByVal value)
F
CyborgName = value
End Set
End Property
End Class
C#:   no (), get, set

In C#, parens indicate a method and the lack of parens indicate a property. You use special get and set methods to both get and set the values of properties.

C# 3.0 introduced auto-implemented properties for use when no additional logic is required.

pulic int VendorID {get; set;}

For a read-only property, leave out the set method.

The value keyword is used to refer to the member field. Properties can make use of any of the access modifiers (private, protected, etc). It is common to use a lowercase member names for member fields ("name" in our example) and uppercase properties to manage member fields ("Name" in our example).

Syntax Example:
public class Cyborg : System.Object
{
  private string cyborgName;
 
  public string CyborgName
  {
  get {return cyborgName;}
  set {cyborgName = value;}
  }

}




Member Visibility

[Other Languages] 

General Info: Class Visibility Specifiers

In OOP languages, members of a class have a specific scope that indicates visibility. Standard visibility includes private, protected, and public. Private members are usable by the defining class only (fully encapsulated). They are invisible outside of the class except by friendly classes. Protected members are usable by the defining class and descendant classes only (plus friendly classes). Public members are usable wherever its class can be referenced.

Languages Focus

Traditional member visibility specifiers for fully OOP languages are private, protected, and public. Many modern OOP languages implement additional member visibilities.

Additional member modifiers are documented under the Member Modifiers topic.

VB.Net:  "Access Modifiers"

In VB.Net, you specify each class and each class member's visibility with an access modifier preceding class or member. The VB.Net access modifiers are the traditional Public, Protected, and Private plus the two additional .Net modifiers Friend and Protected Friend.

Friend indicates members are accessible from types in the same assembly. Protected Friend indicates members are accessible from types in the same assembly as well as descendant classes. OO purist might object to Friend and Protected Friend and I suggest you avoid them until you both fully understand them and have a need that is best suited by them.

Syntax Example:
Public Class Cyborg
Inherits Object
 
  Private FName As String
End Class
C#:  "Access Modifiers"

In C#, you specify each class and each class member's visibility with an access modifier. The C# access modifiers are the traditional public, protected, and private plus the two additional .Net modifiers internal and protected internal.

Internal indicates members are accessible from types in the same assembly. Protected internal indicates members are accessible from types in the same assembly as well as descendant classes. OO purist might object to internal and protected internal and I suggest you choose private, protected, or public over them until you both fully understand them and have a need that is best suited by them.

Syntax Example:
public class Cyborg
{
private String FName;
}




Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]