![Método Constructor en Visual Basic.NET con POO [64] 1 64](https://www.programadornovato.com/wp-content/uploads/2022/07/64-535x251.png)
Método Constructor en Visual Basic.NET con POO [64]
En Programación Orientada a Objetos (POO), un un Método Constructor en Visual Basic.NET es una subrutina cuya misión es inicializar un objeto de una clase. En el constructor se asignan los valores iniciales del nuevo objeto.
Para entender esto vamos a hacer el siguiente ejercicio:
- Crear la clase llamada Persona.
- Inicializar los datos de esa persona (Nombre y edad).
- Forzar a que esa clase sea inicializada con los datos.
Codigo del Método Constructor en Visual Basic.NET con POO: https://github.com/programadornovato/VisualBasic/commit/cb8c0545ba58bfe70d4948eab9ccf5f68724eea6
Public Class Persona
Dim nombre As String
Dim edad As Integer
Sub New(_nombre As String, _edad As Integer)
If _nombre <> "" Then
nombre = _nombre
End If
If _edad > 0 Then
edad = _edad
End If
End Sub
'Sub inicializar(_nombre As String, _edad As Integer)
' If _nombre <> "" Then
' nombre = _nombre
' End If
' If _edad > 0 Then
' edad = _edad
' End If
'End Sub
Sub mostrar()
Console.WriteLine("Nombre=" & nombre & " Edad=" & edad)
End Sub
End Class
Public Class Persona
Dim nombre As String
Dim edad As Integer
Sub New(_nombre As String, _edad As Integer)
If _nombre <> "" Then
nombre = _nombre
End If
If _edad > 0 Then
edad = _edad
End If
End Sub
'Sub inicializar(_nombre As String, _edad As Integer)
' If _nombre <> "" Then
' nombre = _nombre
' End If
' If _edad > 0 Then
' edad = _edad
' End If
'End Sub
Sub mostrar()
Console.WriteLine("Nombre=" & nombre & " Edad=" & edad)
End Sub
End Class
Curso de VB.NET⛓️: https://www.youtube.com/watch?v=aiquJHzxNWw&list=PLCTD_CpMeEKSFwAFjvrfpvSwxmbs2maMo&ab_channel=programadornovato
[CURSO] C##️⃣: https://www.youtube.com/watch?v=NKPMGY6NCko&list=PLCTD_CpMeEKQSOU8Vf9VHXrZa2rc8X0X5&index=1&t=3s&ab_channel=programadornovatoprogramadornovato
[CURSO] C# CON FORMULARIOS#️⃣: https://www.youtube.com/watch?v=l0_U4oyOuns&list=PLCTD_CpMeEKTBih1VgeunCjc83ZQ6UBMI&index=1&ab_channel=programadornovatoprogramadornovato
[Curso] C# MYSQL#️⃣: https://www.youtube.com/watch?v=-5CXNXHIzWk&list=PLCTD_CpMeEKR_4q0-7BxGHXqH0bgpqw5q&ab_channel=programadornovato
[CURSO] C++ DE 0 A HEROE 🦸: https://www.youtube.com/watch?v=APN8aCyPvww&list=PLCTD_CpMeEKTofxs7iottRxJ5YPM7BOcc&ab_channel=programadornovato
[Curso] Java Netbeans GUI Completo☕: https://www.youtube.com/watch?v=18UA7X2ss8g&list=PLCTD_CpMeEKThfXo8D-RXOGu5FarO7_qv&ab_channel=programadornovato
Anterior tutorial Siguiente tutorial
Ave que vuela, a la cazuela.