I have a rather untypical situation:
When I build an Application, I created a class witch contained several nested classes. At the beginning, this was manageable, but now so the file is so big, that I want to move each nested class to its own file.
Is this possible in VB.NET, or do I need to move each nested class out of the "mother" class?
I have Resharper installed, if this helps..
Structure:
Public Class A
Public Class NestedA
End Class
Public Class NestedB
End Class
End Class
Use Partial class:
Partial Public Class ParcialTest
Public Class NestedA
End Class
End Class
And in another file:
Partial Public Class ParcialTest
Public Class NestedB
End Class
End Class
Compiler will merge all these Partial class parts into one for you.
Related
I am trying to implement a mySubClass.vb file as a nested subclass of another main class. It seems like the Partial Class idea is what I need but the implementation isn't working when I try to pull mySubClass.vb in as a nested subclass of another main class.
My original implementation of this code used mySubClass directly so I know the functionality works. I just want to use mySubClass as a data structure within clsMain.
Main Class
Public Class clsMain
Public Property myIntProp as Integer
Public property myStrProp as String
'other properties
Partial Public Class MySubClass
'I want this functionality to be accessible via clsMain.MySubClass
'Just like any other property or function of clsMain
'Partial would keep things organized nicely
End Class
End Class
Sub Class
The class.vb that I want to use as clsMain.MySubClass.
File: MySubClass.vb
Partial Public Class MySubClass
Inherits BaseCollection
Private Class MySubSubClass '(Used for custom properties and functions)
'More properties and Functions
End Class
End Sub
Public Class BaseCollection 'functionality of MySubClass
Public Function MyFunction1()
'Return Data
End Function
End Class
How Main Class is Used
Public Class UsageClass
Private myMainDataStructure as new clsMain
Private Sub GetSubClassList()
dim MyData as ArrayList = myMainDataStructure.MySubClass.MyFunction1()
'^^^ error on this line: MyFunction1() is not a member of project.clsMain.MySubClass^^^
End Sub
End Class
Instead of trying to make this a Partial Class, you should just make a Property containing an instance of that class.
Nested classes must be created and have instances, just like top level classes. By making a property within your main class, you can automatically create that instance in your main class constructor, and your code will work as expected.
Public Class clsMain
Public Property myIntProp as Integer
Public property myStrProp as String
'other properties
Public Property OtherFunctionality as MyOtherClass = New MyOtherClass()
Then just define the class in a separate file:
Public Class MyOtherClass
Public Sub MyFunction1()
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
I'm trying to add a custom event generator inner class to reuse through several Forms. I was at first just going to put it the relevant code inside a #Region and copy-paste it into the code, but I realized a better way would maybe be to do it in a nested class. So I want to basically do
Partial Class Form
Public Class VerifyGenerator
...
End Class
End Class
Public Class MyForm
Inherits Form
Public Class MyVerifyGenerator
...
End Class
End Class
If there's a better / more logical solution to this, I'm all ears. In the Form there's logic to find all the TextBoxes with VerifyHandlers and subscribe them to the event when the Form generates it, custom logic to disable (most) other Controls
Could you just create a base class that exposed that functionality and inherit from it?
Public MustInherit Class VerifyerForm
Inherits Form
' your stuff here
End Class
Public Class MyForm
Inherits VerifyerForm
End Class
Say some function is defined in one file and another function is defined in another file
In C++ you can do that right
You have to create partial class. That way you may splits the definition of class into two or more files.
You can use Partial classes msdn link.
This means you can do something like this:
Partial Public Class partialClass
Public Sub method1()
End Sub
End Class
Partial Public Class partialClass
Public Sub method2()
End Sub
End Class
BTW, this is not the same you can do in C++. In C++, you can separate function declaration from the implementation. In VB, you can separate methods and properties, but you can't declare a function in one partial class and implement it in the another partial 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.