How to implement class constructor in Visual Basic? - vb.net

I just would like to know how to implement class constructor in this language.

Not sure what you mean with "class constructor" but I'd assume you mean one of the ones below.
Instance constructor:
Public Sub New()
End Sub
Shared constructor:
Shared Sub New()
End Sub

Suppose your class is called MyStudent. Here's how you define your class constructor:
Public Class MyStudent
Public StudentId As Integer
'Here's the class constructor:
Public Sub New(newStudentId As Integer)
StudentId = newStudentId
End Sub
End Class
Here's how you call it:
Dim student As New MyStudent(studentId)
Of course, your class constructor can contain as many or as few arguments as you need--even none, in which case you leave the parentheses empty. You can also have several constructors for the same class, all with different combinations of arguments. These are known as different "signatures" for your class constructor.

If you mean VB 6, that would be Private Sub Class_Initialize().
http://msdn.microsoft.com/en-us/library/55yzhfb2(VS.80).aspx
If you mean VB.NET it is Public Sub New() or Shared Sub New().

A class with a field:
Public Class MyStudent
Public StudentId As Integer
The constructor:
Public Sub New(newStudentId As Integer)
StudentId = newStudentId
End Sub
End Class

Related

Hiding function on nested class

Public Class Class1
Private names As List(Of String)
Private _class2 As New Class2
Public Sub AddName(ByVal name As String)
names.Add(name)
_class2.Add()
End Sub
Public ReadOnly Property AddAge(ByVal name As String) As Class2
Get
_class2.index = names.IndexOf(name)
Return _class2
End Get
End Property
Public Sub Clear()
names.Clear()
_class2.Clear()
End Sub
Public Class Class2
Private _age As List(Of Integer)
Protected Friend index As Integer
Public Property Age() As Integer
Get
Return _age(index)
End Get
Set(ByVal value As Integer)
_age(index) = value
End Set
End Property
Public Sub Add()
_age.Add(0)
End Sub
Public Sub Clear()
_age.Clear()
End Sub
End Class
End Class
How can I hide ,Sub Clear and Sub Add on class2, so they'll only be visible on class1, like;
Public Sub Clear()
names.Clear()
_class2.Clear() '<<<<<<<
End Sub
I want they do not be visible on Sub Main(), like they are below.
Sub Main()
Dim person As New Class1
person.AddAge("kid").Clear() '<<<<<<
person.AddAge("kid").Add() '<<<<<<
End Sub
If I put Protected, I class1 cannot access it. If I put Protected Friend, Sub Main() can still access them. Thanks for your help and time.
Used -Hans Passant- comment.
"Trust in .NET follows assembly boundaries. If you get two classes in one assembly then there are two programmers that know how to find each other if there's a problem. The only way to get what you want is to put these classes in a separate class library project. Which then lets you use Friend. And whomever writes that Main method doesn't have to be friendly."

Accessing a list from another class vb.net

I have these two classes class FootballAdmin makes use of the import Football from the projects references, what i need to do is in class MainForm is for the updateView method to access the list held by FootballAdmin and display it in the teamSheetListBox, i am unsure how access the list as indicated by ?????
Imports Football
Public Class FootballAdmin
Private fTeam As List(Of FootballTeams)
Public Sub New()
fTeam = New List(Of FootballTeams)
End Sub
Public ReadOnly Property Teams() As List(Of FootballTeams)
Get
Return fTeams
End Get
End Property
End Class
Public Class MainForm
Private fFootballAdmin As FootballAdmin
Public Sub New()
InitializeComponent()
fFootballAdmin = New FootballAdmin
updateView()
End Sub
Private sub updateView()
For each team As String In ????????
teamSheetListBox.Items.Add(team)
Next
End Sub
End Class
Help please!
The big hint I am going to give you is that team in your loop:
For each team As String In ????????
teamSheetListBox.Items.Add(team)
Next
Isn't going to be a String. It will be the same type: FootballTeam as in your FootballAdmin Class. Consider what you have access to in your MainForm that can get you to those types.

how to access class from inherited class

I have two classes:
class class2
inherits class1
public sub modify()
'modify property of class1
end sub
end class
How can I modify class1 in a sub in class2?
You just call it. Example:
Public Class class1
Private _Value As String = String.Empty
Property Value() As String
Get
Return _Value
End Get
Set(ByVal value As String)
_Value = value
End Set
End Property
End Class
Public Class class2
Inherits class1
Public Sub modify()
Value = "modified"
End Sub
End Class
And to show it works:
Dim c2 As New class2
c2.modify()
MessageBox.Show(c2.Value)
You are asking about properties, note that only protected and public properties are visible to inherited classes.
You need the MyBase keyword when you are overriding an existing function in the parent class. Other protected or public properties or functions can be accessed regulary without any special keyword.
One tip I wanted to add to the above comments regarding accessing base class info is where you have a base class without a default contructor or want to use a specific constructor This is a good opportunity to use Mybase. You have to call the constructor before any additional actions take place in this scenario.
Public Class MyClass
Inherits baseClass
Public Sub New()
mybase.new("Oranges")
End Sub
End Class
Public Class baseClass
Private _someVariable as String
Public Sub New(byval passedString as string)
_someVariable = passedString
End Sub
End Class

Overloaded Constructor in Abstract Class in vb.NET

I have an abstract class in vb.net with two subclasses. In the abstract class I have a constuctor that looks like this:
Public Sub New(arg1 as String, arg2 as String)
Me.arg1 = arg1
Me.arg2 = arg2
End Sub
I would like to create a second constructor that doesn't take any arguments and just initializes the args to default values. It would look like this:
Public Sub New()
Me.arg1 = "123"
Me.arg2 = "456"
End Sub
When I attempt to create a new subclass using the second constructor the compiler complains that I'm missing two args to the constructor.... Is there a reason I can't overload the constructor in the abstract class?
Thanks
There's no "abstract" in VB. If you mean abstract in the c# sense (MustInherit in VB parlance), then you need to define both constructors in your subclasses, as constructors are not inherited.
Example:
Public MustInherit Class SuperClass
Public Property ValueOne As String = String.Empty
Public Property ValueTwo As String = String.Empty
Public Sub New()
Me.New("123", "456")
End Sub
Public Sub New(ByVal tValueOne As String, ByVal tValueTwo As String)
Me.ValueOne = tValueOne
Me.ValueTwo = tValueTwo
End Sub
End Class
Public Class SubClass
Inherits SuperClass
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal tValueOne As String, ByVal tValueTwo As String)
MyBase.New(tValueOne, tValueTwo)
End Sub
End Class
If your second constructor is in the sub class, it must call the constructor in the base class.
Public Sub New()
MyBase.New("123", "456")
End Sub

Similar classes with different signatures

I have two classes:
Public Class Subscribing
Private _subscribingObjects As IList(Of String)
Public Sub Add(ByVal obj As SubscribeObject)
'...code...'
End Sub
Public Sub Remove(ByVal index As Integer)
'...code...'
End Sub
End Class
Public Class Providing
Private _providingObjects As IList(Of String)
Public Sub Add(ByVal obj As ProvideObject)
'...code...'
End Sub
Public Sub Remove(ByVal index As Integer)
'...code...'
End Sub
End Class
Is there a more elegant way to add do this? One class would suffice, but since the Add methods have different arguments, then one really wouldn't work.
Any help would be appreciated.
this?
Public Class SubscribingProviding(Of t)
Private _subscribingObjects As IList(Of String)
Public Sub Add(ByVal obj As t)
'...code...'
End Sub
Public Sub Remove(ByVal index As Integer)
'...code...'
End Sub
End Class
Your add functions should be fine. As long as you have different variable types being passed in you can have the function names be the same. Your remove Subs will not be allowed in the same class because it is using the same parameter Integer.
Eh.. probably not. They are different enough that you cant even Interface them.
I personally wouldn't mix the two responsibilities (of subscribing and providing) in one class. The classes themselves can easily be simplified by just inheriting from List(Of T)
Public Class Subscribing
Inherits List(Of SubscribeObject)
End Class
Public Class Providing
Inherits List(Of ProvideObject)
End Class
If you really want to get down to one class and make sure that it can only accept SubscribeObject and ProvideObject respectively, implement a common interface in both SubscribeObject and ProvideObject. Then create a generic class that accepts the interface:
' Common interface '
Public Interface ISubscribeProvideObject
End Interface
' SubscribeObject and ProvideObject both implementing the common interface '
Public Class SubscribeObject
Implements ISubscribeProvideObject
'...'
End Class
Public Class ProvideObject
Implements ISubscribeProvideObject
'...'
End Class
' Generic class accepting both types '
Public Class SubscribingProviding(Of T As ISubscribeProvideObject)
Inherits List(Of T)
'... Add() and Remove() methods only needed if custom logic applies ...'
End Class