how to access class from inherited class - vb.net

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

Related

Inheritance confusion vb.net

I searched over 20 articles and I am still slightly confused with inheritance. I have three classes as such:
Class A
Private _Mode As String
Public Function returnMode() As String
Return _Mode
End Function
Class B
Inherits Class A
Private _Mode As String = "modeb"
Class C
Inherits Class A
Private _Mode As String = "modec"
Now any time I create a B or C class, I would like the object to return the child class _Mode. I can make the New() function _Mode = "modeb" but I wanted to know a better way or more correct way.
How do I properly declare the variable _Mode?
One option would be to make _Mode Protected instead of Private so you can set it from the subclasses, and set the value is the subclass constructors:
Class A
Protected _Mode As String
Public Function returnMode() As String
Return _Mode
End Function
Class B
Inherits Class A
Public Sub New()
_Mode = "modeb"
End Sub
Class C
Inherits Class A
Public Sub New()
_Mode = "modec"
End Sub

Accessing private functions and variables in a public class from another class

As the title says, I've run into a problem where I have to call certain private functions in a class.
Public Class Class1
private Type
Private Name
private Function()
I have tried doing the following:
Public Class Class1
Dim copyClass As Class1
Public Shared Instance As Class1
Public Sub New()
MyBase.New()
copyClass = Me
End Function
Public Function createInstance() As Class1
Instance = copyClass
Return Instance
End Function
Then in my other class, Class2, I have added:
Public Property callingObject As wdCopyPatch
Get
Return copyObject
End Get
Set(value As wdCopyPatch)
copyObject = value
End Set
End Property
now, I can just do the following from within a function in Class1
Dim Ob as Class2
Ob.callingObject = createInstance()
This allows me to use copyObject from Class2 but only gives me access to Class1's Public Functions and variables. What can I do to be able to access Class1's Private functions and variables without making everything public?
Any advice or comments are appreciated :)
As per my comments, here's some code:
Sandbox is my class with a private function, and a public property getting info from that private function.
otherclass, calls this property of sandbox.
Public Class sandbox
Public ReadOnly Property myHiddenValue() As String
Get
Return get_that_sucker()
End Get
End Property
Private Function get_that_sucker()
Return "boo!"
End Function
End Class
Public Class otherClass
Public Sub mySub()
Dim mysandbox As New sandbox
MsgBox(mysandbox.myHiddenValue)
End Sub
End Class

VB.NET - reference to an object under construction is not valid when calling another constructor

Is there any difference between doing this:
Public Class Class1
Protected Test1 As String
Public Sub New(ByVal test2 As String)
Test1 = test2
End Sub
End Class
Public Class Class2
Inherits Class1
Public Sub New()
MyBase.New("called from class 2")
End Sub
End Class
and this:
Public Class Class1
Protected Test1 As String
End Class
Public Class Class2
Inherits Class1
Public Sub New()
Test1 = "Called from class 2"
End Sub
End Class
In the first example, the superclass instance variable is initialised in the constructor. In the second example, the superclass instance variable is initialised in the subclass.
The reason I ask is because I am trying to do this from the subclass:
Public Sub New()
MyBase.New( System.Configuration.ConfigurationManager.AppSettings.Item("PurgeFile" & Me.GetType.Name), & _
System.Configuration.ConfigurationManager.AppSettings.Item("PurgeHeader" & Me.GetType.Name) )
End Sub
and I am getting an error: "reference to an object under construction is not valid when calling another constructor".
You can't use Me within MyBase.New() call, so following part of your code is invalid:
Me.GetType.Name
Update
There is huge difference your 2 samples: First one doesn't allow Class1 initialization without constructor parameter and the second one does.
I would rather think about something like:
Public MustInherit Class Class1
Public MustOverride ReadOnly Property Test1 As String
End Class
Public Class Class2
Inherits Class1
Private _Test1 As String = "Called from class 2"
Public Overrides ReadOnly Property Test1 As String
Get
Return _Test1
End Get
End Property
End Class
Reference from MSDN

VB generics with constraints -- type casting and inheritance?

Take this scenario:
Public Interface IMyClass
End Interface
Public mustinherit class MyBaseClass : implements IMyClass
End Class
public class MyClass : inherits MyBaseClass
End Class
public class MyModel(of t as IMyClass)
private Dim _parameter as t
Public Sub New(byval parameter As t)
_parameter As t
End Sub
End class
In my controller, I can do this with no problem:
Dim _myclass as IMyClass = new MyClass()
Can I do something similar with this:
Dim _myModel as MyModel(of IMyClass) = new MyModel(of MyClass)
???
My initial thought was wrong, as I thought the conversion could be done automatically, but it appears it is not done. Any way to achieve the same thing within .NET?
EDIT
I updated the MyModel class to show more of what I was doing. I want to constrain the instance I create, but then do what would be a narrowing conversion with traditional, non-generics code. Basically, my partial Razor views would require the explicit model, and those views end up rendering another view that will take that model and display it. Because the models all implement or inherit a class that implements IMyClass, all the methods should exist on all of the instances and should be callable but the types are not interchangable.
Let’s modify MyModel slightly, shall we?
Public Class MyModel(Of T As IMyClass)
Private _parameter As T
Public Sub Something(parameter As T)
_parameter = parameter
End Sub
End class
Public Class MyClassA : Inherits MyBaseClass
End Class
Public Class MyClassB : Inherits MyBaseClass
End Class
Dim _myModel As MyModel(Of IMyClass) = New MyModel(Of MyClassA)()
_myModel.Something(New MyClassB()) ' Boom!
If the assignment were allowed the last line would pose a problem: MyMode(Of MyClassA)._parameter has type MyClassA but the last line would assign an object of the (unrelated) type MyClassB. This is illegal and so VB forbids it.
Do you need multiple varieties of MyModel, or are you just attempting to require that the stored object be constrained to IMyClass?
Simplest approach (that might not do everything you need):
Public Interface IMyClass
Sub DoIt()
End Interface
Public Class MyModel
Private ReadOnly _parameter As IMyClass
Public Sub New(parameter As IMyClass)
_parameter = parameter
End Sub
Public Sub DoItToIt()
_parameter.DoIt()
End Sub
End Class
Public Class MyClassA
Implements IMyClass
Public Sub DoIt() Implements IMyClass.DoIt
End Sub
End Class
Public Class Tests
Public Sub Main()
Dim model1 As MyModel = New MyModel(New MyClassA)
model1.DoItToIt()
End Sub
End Class
Next step up in complexity is to define an interface IHasMyClass for classes that contain an IMyClass. This supports manipulations based on the allowed type, and the actual type, of the contained object:
Public Interface IMyClass
Sub DoIt()
End Interface
Public Interface IHasMyClass
Function GetIt() As IMyClass
Function GetItsType() As Type
Function GetAllowedType() As Type
End Interface
Public Class MyModel(Of T As IMyClass)
Implements IHasMyClass
Private ReadOnly _parameter As IMyClass
Public Sub New(parameter As IMyClass)
_parameter = parameter
End Sub
Public Sub DoItToIt()
_parameter.DoIt()
End Sub
Public Function GetItAsT() As T
Return _parameter
End Function
Public Function GetIt() As IMyClass Implements IHasMyClass.GetIt
Return _parameter
End Function
Public Function GetItsType() As Type Implements IHasMyClass.GetItsType
Return _parameter.GetType()
End Function
Public Function GetAllowedType() As Type Implements IHasMyClass.GetAllowedType
Return GetType(T)
End Function
End Class
Public Class MyClassA
Implements IMyClass
Public Sub DoIt() Implements IMyClass.DoIt
End Sub
End Class
Public Class Tests
Public Sub Main()
' Allow any IMyClass
Dim model1 As MyModel(Of IMyClass) = New MyModel(Of IMyClass)(New MyClassA)
model1.DoItToIt()
Dim it As IMyClass = model1.GetIt()
Dim allowedT As Type = model1.GetAllowedType()
' Restrict to MyClassA
Dim modelA As MyModel(Of MyClassA) = New MyModel(Of MyClassA)(New MyClassA)
modelA.DoItToIt()
Dim itA1 As IMyClass = modelA.GetIt()
Dim itA2 As MyClassA = modelA.GetItAsT()
Dim allowedTA As Type = modelA.GetAllowedType()
End Sub
End Class
In Tests(), notice that we now need to declare whether we are creating a MyModel that accepts ANY IMyClass MyModel(Of IMyClass), or one that requires a specific sub-class MyModel(Of MyClassA).
If we want to manipulate MyModels, that may be either of the above types, we use the common interface:
Dim model As IHasMyClass
model = model1
...
model = modelA
Or in your case, to support all the functionality of MyModel, rename IHasMyClass as IMyModel, and add the various MyModel functions, but instead of T, use IMyClass:
Public Interface IMyModel
Function GetIt() As IMyClass
Function GetItsType() As Type
Function GetAllowedType() As Type
Sub DoItToIt()
Function CompareIt(other As IMyClass) As Integer
End Interface
And make appropriate changes/additions to IMyClass and MyModel.
Then it becomes possible to do:
Dim model As IMyModel = modelA
If model.CompareIt(model1.GetIt()) > 0 ...

Can I inherit from a generic class without specifying a type?

I have the following sample code in a VB.NET console application. It compiles and works, but feels like a hack. Is there a way to define EmptyChild so that it inherits from Intermediate(Of T As Class) without using the dummy EmptyClass?
Module Module1
Sub Main()
Dim Child1 = New RealChild()
Child1.Content = New RealClass()
Dim Child2 = New EmptyChild()
Console.WriteLine("RealChild says: " & Child1.Test)
Console.WriteLine("EmptyChild says: " & Child2.Test)
Console.ReadLine()
End Sub
Public Class EmptyClass
End Class
Public Class RealClass
Public Overrides Function ToString() As String
Return "This is the RealClass"
End Function
End Class
Public MustInherit Class Base(Of T As Class)
Private _content As T = Nothing
Public Property Content() As T
Get
Return _content
End Get
Set(ByVal value As T)
_content = value
End Set
End Property
Public Overridable Function Test() As String
If Me._content IsNot Nothing Then
Return Me._content.ToString
Else
Return "Content not initialized."
End If
End Function
End Class
Public MustInherit Class Intermediate(Of T As Class)
Inherits Base(Of T)
'some methods/properties here needed by Child classes
End Class
Public Class RealChild
Inherits Intermediate(Of RealClass)
'This class needs all functionality from Intermediate.
End Class
Public Class EmptyChild
Inherits Intermediate(Of EmptyClass)
'This class needs some functionality from Intermediate,
' but not the Content as T property.
Public Overrides Function Test() As String
Return "We don't care about Content property or Type T here."
End Function
End Class
End Module
The other way to do this would be to move the generic code out of the Base class and then create 2 Intermediate classes like this:
Public MustInherit Class Intermediate
Inherits Base
'some methods/properties here needed by Child classes
End Class
Public MustInherit Class Intermediate(Of T As Class)
Inherits Intermediate
'implement generic Content property here
End Class
Then RealChild would inherit from the generic Intermediate and EmptyChild would inherit from the non-generic Intermediate. My problem with that solution is that the Base class is in a separate assembly and I need to keep the code that handles the generic type in that assembly. And there is functionality in the Intermediate class that does not belong in the assembly with the Base class.
Yes, you need to specify a type parameter when you inherit, or your EmptyChild must be generic as well. But, you don't have to dummy up a EmptyClass - just use Object as your type parameter:
Public Class EmptyClass
Inherits Intermediate(Of Object)
End Class