Creating classes in VB.Net and C#

Explanation of the creation of classes and objects in the two main languages ​​of the .Net platform.

To explain the creation of classes we will use a simple example based on a main class “bicycle”.

To create a class we must define its properties and its methods, in this example the following terms are used as properties of the bicycle class: Model, Price, NumberOfSpeeds and Speed; as methods of the class are used: Accelerate(km) , Brake() and CheckSpeed().

vb.net

Public Class Bicycle

Public Model as String

Public Price as Double

Public NumberOfSpeeds as Integer

Private Velocity as Integer

Public Sub Speed(ByVal km As Integer)

Speed ​​= Speed ​​+ km

End Sub

Public Sub Brake()

If Velocity > 0 Then

speed = speed -1

End If

End Sub

Public Function QuerySpeed() As Integer

Return Speed

End Function

end class

C#


Class Bicycle

{

public string Model;

public double Price;

public int NumberOfSpeeds

private int Speed

public void Speed(int km)

{

Speed ​​= Speed ​​+ km;

}

public void Brake()

{

if (Speed ​​> 0)

{

speed = speed – 1;

}

}

public int QuerySpeed()

{

return speed;

}

}

Our bike class consists of several properties and methods, the keywords Private and Public define the accessibility of the properties, functions or subroutines. The definition of a private type property or method indicates that it can only be used within the code of the same class, if we created a bicycle type object, the class specifications would not allow us to access the speed property to consult or modify it, since it is defined as private. On the other hand, you can use the Accelerate() and Brake() subroutines since they are of the Public type, and from within them you interact with the private properties, with this we are able to encapsulate the code and make accessible only what we want.

See also  Introduction to Javascript in the browser

Once the class is built, objects of it can be instantiated.

vb.net


Dim objBike as Bike = New Bike

Dim CurrentSpeed ​​as Integer

objBicicleta.Model = “Mountain”

objBike.Price = 200

objBike.NumberOfSpeeds = 21

objBike.Accelerate(5)

objBike.Brake()

CurrentSpeed ​​= objBicycle.QuerySpeed

C#


Bike objBike = new Bike();

int CurrentSpeed;

objBike.Model = “Mountain”;

objBike.Price = 200;

objBike.NumberOfSpeeds = 21;

objBike.Accelerate(5);

objBike.Brake();

CurrentSpeed ​​= objBike.QuerySpeed();

After creating the object objBicicleta from the class, you can modify the values ​​of the properties of type Public, and call the methods of type Public.

In the example, the methods Accelerate(5) are called, passing the number of km that we want to accelerate through the “km” parameter that is defined in the subroutine.

Then the Brake() method is called, which decreases the value of the Speed ​​property by one unit.

Finally, the function CheckVelocity() is used, which returns the value of the Velocity property to introduce it in the variable VelocidadActual.

Loading Facebook Comments ...
Loading Disqus Comments ...