Delcon
Delcon

Reputation: 2555

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?

Upvotes: 0

Views: 2158

Answers (3)

Christopher Richmond
Christopher Richmond

Reputation: 656

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

Upvotes: 0

LarsTech
LarsTech

Reputation: 81620

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)

Upvotes: 1

MichaelS
MichaelS

Reputation: 7103

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.

Upvotes: 0

Related Questions