Override variable from abstract class - vb.net

I need to define a base class that contains the common implementation of logging in my application and create two specific classes LogA and LogB that will use the methods from the base class with a specific variable determining the logger to use.
Now I have the following code:
Public MustInherit Class BaseLog
Private Shared _Log As ILog 'This must be overridden in specific classes
Shared Sub WriteDebug (value As String)
End Sub
End Class
Public Class LogA
Inherits BaseLog
Private Shared _Log As ILog = LogManager.GetLogger ("aaaa")
End Class
How can I do this?
PS: I don't know how to format code on stackexchange mobile app.

You cannot override a field or a Shared member of your base class. Only a method or property that you declared Overridable can be overridden. You can a declare a method MustInherit to force a derived class to provide an implementation, you'd consider a GetLogger() function.
In a case like this, were you absolutely want to be sure that the client code hands you a valid logger, the more obvious solution is to add a constructor to your base class:
Public MustInherit Class BaseLog
Public Sub New(logger As ILog)
If logger is Nothing Then
Throw New ArgumentNullException("You must provide a logger")
End If
_Log = logger
End Sub
'' etc..
End Class
The client code is now forced to provide you with a valid ILog implementation, the only way they can create an instance of the derived class.

Without knowing your entire spec and what you're trying to achieve; I wonder if the singleton pattern get done what you need to get done.
Instead of LogB.WriteDebug("Oh noes!") you would have to do LogB.GetInstance().WriteDebug("Oh noes!"). Its not quite as nice, but when you're talking about the base class accessing a member set by a child class it might be the only way to go. This assumes you could use different logs in the same instance of your application.
If you just want to inject different loggers at runtime/startup for the duration (i.e. if you're running in production, development or unit testing), then you may want to look at dependency injection (such as Ninject), or some other form of IOC (inversion of control).
Clearly, this is a more complex problem than it first appears to be! However, it may be that we're stuck looking at it from the wrong angle.

You could have an instance property which is implemented in the derived class and returns the shared log variable:
Public MustInherit Class BaseLog
Public MustOverride ReadOnly Property _Log As ILog
Shared Sub WriteDebug(value As String)
End Sub
End Class
Public Class LogA
Inherits BaseLog
Private Shared log As ILog = LogManager.GetLogger("aaaa")
Public Overrides ReadOnly Property _Log As ILog
Get
Return log
End Get
End Property
End Class

Related

How to get the class type in a inherited shared method

Folks;
Code looks like:
Public Class MasterA
Inherits Underling
End Class
Public Class MasterB
Inherits Underling
End Class
Public Mustinherit Class Underling
Sub DoSomething()
Me.GetType 'Using the instance, I can get the class.
end sub
Shared function() as ???? 'How can I define the return type based on the class that inherited me?
'Me.GetType 'Won't work as this is a shared function with no instance 'Me'
End Function
End class
OK. The question is: is there a way to get at the class type from within a shared function that was inherited by another class?
What I'm building is an XML serializer/desrializer as an inheritable class so that classes that inherit it can be serilized to an XML file, and back again. Rather than writing a serializer/deserializer for each type of class I want to do this with, I'd like to just inherit the functionality.
To do that, though, requires that I be able to ascertain the clas that inherited me in the shared function.
You could get the desired behavior with a generic base class, my VB is a little rusty so you might find stray parens or brackets. This would really be the only way to get a type reference to an inheriting class in a shared base class function.
Public Mustinherit Class Underling(Of T)
Sub DoSomething()
Me.GetType 'Using the instance, I can get the class.
end sub
Shared function() As T
' GetType(T) should get the type at this point
End Function
End class
Public Class MasterA
Inherits Underling(Of MasterA)
End Class
Public Class MasterB
Inherits Underling(Of MasterB)
End Class
As a side note it does seem like a rather weird solution to handle XmlSerialization rather than through your own serializer implementation or XmlSerializer

Error when passing friend class as type from public class to friend base class

Disclaimer: I am fairly new to working with generics so I am not entirely sure if what I am trying to do even makes sense or is possible.
I have a bunch of user controls in a project. All of these user controls share a similar property so I want to move it into a base class. The only difference is the return type of the property.
I have three classes interacting in this scenario. The first class is a base type, which inherits from CompositeControl and will be inherited by other classes in my project:
Friend Class MyBaseClass(Of T As {New})
Inherits CompositeControl
Private _someProperty As T = Nothing
Protected ReadOnly Property SomeProperty As T
Get
// dumbed down for the sake of example
If _someProperty Is Nothing Then
_someProperty = New T()
End If
Return _someProperty
End Get
End Property
End Class
Then I have this control class, which inherits from MyBaseClass:
Public Class MyControlClass
Inherits MyBaseClass(Of MyReturnTypeClass)
// snip...
End Class
And finally MyReturnTypeClass which is what the base's SomeProperty should return:
Friend Class MyReturnTypeClass
Public Property AutoProperty1 As Boolean = False
Public Property AutoProperty2 As String = String.Empty
// etc
End Class
When I attempt to build the project, I get this error from MyControlClass:
Inconsistent accessibility: type argument 'MyReturnTypeClass' is less accessible than Class 'MyControlClass'.
I need MyControlClass to be Public so it can be consumed by other projects, and I also want the MyBaseClass and MyReturnTypeClass to be Friend so they cannot be seen/used by consumers. Am I just missing some special keyword somewhere or is this not possible?
You cannot inherit from a base class that is less accessible than the derived class. So for instance, this won't work:
Friend Class MyBase
End Class
Public Class MyDerived
Inherits MyBase ' Won't compile because MyBase is less accessible
End Class
Therefore, since in your example, MyBaseClass(T) has is a friend type, but you are trying to inherit from it into a public MyControlClass type. Therefore, even if you took generics and MyReturnTypeClass out of the "equation", it still wouldn't work.
However, with generics, even if no member of the public interface of the class actually uses the generic type, the type must still be at least as accessible as the derived type. For instance:
Public Class MyBase(Of T)
' T not actually used at all
End Class
Friend Class MyOtherType
End Class
Public Class MyDerived
Inherits MyBase(MyOtherType) ' Won't compile because MyOtherType is less accessible
End Class
The base class must be at least as accessible as the derived class. This is a language restriction (see here).
If you intend to avoid MyBaseClass being instantiated by consumers, consider marking it Public MustInherit instead of Friend. Hope this helps.

Error using multiple Generic Classes in VB.Net

I tried to find a similar issue already posted but simply got confused with what i found.
I have a situation which involves the following objects:-
Reports contain parameters
Documents contain parameters
Report and Document Parameters are similar
Queries contain parameters
Reports, Documents and Queries all have similar Parameters
So I created the following class structure:-
Public MustInherit Class clsBaseCollection(Of TclsBaseChild As clsBase, TclsBaseParent As clsBase)
Public MustInherit Class clsParams(Of TclsParam As clsParam, TclsRootObject As clsRootObject)
Inherits clsBaseCollection(Of TclsParam, TclsRootObject)
Public MustInherit Class clsRepDocParams(Of TclsRepDocParam As clsRepDocParam, TclsReportDocument As clsReportDocument)
Inherits clsParams(Of TclsRepDocParam, TclsReportDocument)
Public Class clsReportParams
Inherits clsRepDocParams(Of clsReportParam, clsReport)
Public MustInherit Class clsReportDocument
MustOverride ReadOnly Property Parameters() As clsRepDocParams(Of clsRepDocParam,clsReportDocument)
Public Class clsReport
Inherits clsReportDocument
Private _Params As clsReportParams
Public Overrides ReadOnly Property Parameters() As clsReportParams
Get
If _Params Is Nothing Then
BeginUpdate()
_Params = New clsReportParams(Me)
EndUpdate()
End If
Return _Params
End Get
End Property
The last property produces the following error:-
‘Public Overrides ReadOnly Property Parameters As clsReportParams' cannot override 'Public MustOverride ReadOnly Property Parameters As clsRepDocParams(Of clsRepDocParam, clsReportDocument)' because they differ by their return types.
I cant see how this is so because I believe I have my classing levels correct!?
Apologies for the lack of tab formatting... im new to the stack overflow site and hopefully i will improve ;)
Cheers
Jeff
The MustOverride property is declared as
As clsRepDocParams(Of clsRepDocParam,clsReportDocument)
Your Override is simply declared as
As clsReportParams
It does not specify the base collection so they do not match. You should probably declare the override as
As clsRepDocParams(Of clsRepDocParam,clsReportDocument)
to match the base class.
It is worth noting that with every additional level of inheritance that you create, you need to be that much more careful to ensure that you design is done very carefully! If I was reviewing that code I would need to be convinced that the complexity was actually going to pay off over the lifetime of the project.

VB.NET inheritance - do all properties in the derived classes have to be declared in the base class?

I'm refactoring, and have run into a roadblock.
Background:
I have a base class and several inherited derived classes. The derived classes don't always need to have the same properties. If any properties are shared among the derived classes, those properties would live at the base class level ('Contents', for example).
Similarly, GoodDocument below has 'GoodThings' but would not want/need to have 'BadThings'.
I want to treat instances of both 'GoodDocument' and 'BadDocument' as type 'Document'
public mustinherit class Document
public property Contents as string
public sub new()...
end class
public class GoodDocument
inherits Document
public property GoodThings as string
public sub new()...
end class
public class BadDocument
inherits Document
public property BadThings as string
public sub new()...
end class
The 'DocumentWriter' class will also have several derived classes: ('GoodDocumentWriter' and 'BadDocumentWriter').
I need to pass around the DocumentWriter.Doc as a 'Document' to a number of other places in the code. Doc.GoodThings would only be called from within an instance of either 'GoodDocument' or 'GoodDocumentWriter'.
public mustinherit class DocumentWriter
public property Doc as Document
public sub new()...
end class
public class GoodDocumentWriter
inherits DocumentWriter
public sub new
mybase.Doc = new GoodDocument
end sub
end class
public class BadDocumentWriter
inherits DocumentWriter
public sub new
mybase.Doc = new BadDocument
end sub
end class
Question:
Is there a design pattern that allows for derived classes to have members that don't exist at the base class level?
Do all properties have to live at the base class level?
Revised
I was trying to be brief with my initial question and I made the mistake of over simplifying the situation. In short, I did realize that it should be possible to have different properties on each of the derived classes. (I typed that in a tongue-in-cheek manor and didn't mean to keep it in the final post).
I realize now that the problem that I was experiencing was really symptomatic of a larger issue which needed addressing.
It appears that I was encountering compiler complaints that could be corrected by further refactoring and looser coupling. While others answered the basic question that I posed, Ryan Gross' example really helped kick start some new ideas.
Thanks!
What you should do in this case is define the operations that can be performed on instances of Document in an interface. In your case maybe there is a WriteThings operation, so you would have:
public interface Writeable {
public sub WriteThings();
}
Then in your derived classes you would implement the method to utilize the internal data of the class. For example:
public mustinherit class Document implements Writeable
public property Contents as string
public sub new()...
public sub WriteThings();
end class
public class GoodDocument
inherits Document
public property GoodThings as string
public sub new()...
public sub WriteThings()
//Do something with GoodThings
end sub
end class
public class BadDocument
inherits Document
public property BadThings as string
public sub WriteThings()
//Do something with BadThings
end sub
public sub new()...
end class
Finally, client code that needs to call WriteThings accesses it through an interface:
public mustinherit class DocumentWriter
public property Doc as Writable
public sub new()...
public sub PerformWrite()
Doc.WriteThings();
end sub
end class
It is generally not a good idea to build several parallel class hierarchies. In this case, one DocumentWriter class should be able to write any class that implements Writeable by invoking its WriteThings method.
If all the properties live at the base class level, then I'm not sure what the point of a derived class would be. :) You'd be able to do everything with the base class.
So, yes. If something is applicable only to GoodDocument and not to Document, then it should be in GoodDocument.
To answer your question specifically:
Yes, you just create multiple layers in the inheritance hierarchy: You have a base class, and then many two “branches” (good and bad, to use your terminology). Any properties that are only relevant to either branch, you declare in the class that inherits from the base class. Those properties will only be visible to that class and any classes inheriting from it.
No, properties can be declared anywhere within your inheritance hierarchy.

In VB, How do you force an inherited class to use an attribute on the class?

I'm trying to force an inherited class to use a custom attribute. I'm creating a class library where the user who wants to create an item will do so, but be forced to add an attribute (or visual studio will automatically add the default attribute) to their inherited class. Here is what I'm hoping to achieve:
BaseClass.vb:
<CustomAttribute(10)> _
Public Class BaseClass
End Class
MyClass.vb:
<CustomAttribute(12)> _
Public Class MyClass
Inherits BaseClass
Public Sub New()
Mybase.New()
End Sub
End Class
So the thought is that much like when you mark a function as "MustOverride" and then the inherited class must override the function, I want the attribute to be "MustOverride" causing the inherited class to specify the value.
I've tried this, and it will work, but it would be much cleaner if I could use attributes:
BaseClass.vb:
Public MustInherit Class BaseClass
Public MustOverride ReadOnly Property CustomAttribute() As String
End Class
MyClass.vb:
Public Class MyClass
Inherits BaseClass
Public Sub New()
Mybase.New()
End Sub
Public Overrides ReadOnly Property CustomAttribute() As String
Get
Return "testing"
End Get
End Property
End Class
Thank you for any help you can provide.
Scott
Did you consider implementing an interface instead? I assume that you're using a base class as you want to provide some code in the base, if not then an Interface might be better depending on your other requirements:
Interface IBase
ReadOnly Property CustomAttribute() As String
End Interface
It's still very compact and when you type 'Implements IBase' in a new class Visual Studio will fill in the code for you.
There's no way in .NET to force a class to define an attribute at compile time. The best you'll be able to do is check at run-time whether the attribute was defined, and if not to throw an exception.