Raise Base Class Event VB.NET Entity Framework Partial Class - vb.net

I have a partial class to add some functionality to an Entitiy Framework class.
I want to RaiseEvent PropertyChanged when I change any properties that are only in the partial part of the class.
I'm getting:
Derived classes cannot raise base class events
Partial Class Person
Sub NotifyPropertyChanged(ByVal name As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
End Class
Any ideas on how to achieve this?

The problem has nothing to do with partial classes. The problem is that Person derives from another class, and you're trying to raise an event declared in the base class from within the Person class.
The solution is to create a protected method in the base class that raises the event, and call it from within the person class.

Related

Asking help for understanding OOP and inheritance in VB

I am making a WinForms aplication where I have a graphic interface with textboxes and comboboxes where the user can input information about vehicles such as brand or color. It has two radiobuttons to choose if the vehicle is either a car or a motorcycle. It only has two buttons, one called "create vehicle" and another one "show info" which displays in a textbox all the data the user has input.
Then I have the classes Form1, Vehicle, Car, and Motorcicle (both inherit from Vehicle).
In classes vehicle, car, and motorcicle, I have all the attributes, an empty constructor and one with parameters, and also a method that should show the info provided for each vehicle.
The thing is, I don't know where and how I should instance Vehicle or the other classes, and if I need to assign the attributes from each class a value from the form. How and where do I call the method?
I imagine all of this should be done in Form1, but again, do I call it from the sub that handles the create button?
You should probably mark your vehicle class as MustInherit (abstract in c#) - meaning a class that can only be used as a base class for other classes, and can't be directly instantiated (except from it's derived classes).
In the Create vehicle button click event handler you should create either an instance of the Car or the Motorcycle class, but in the Show info button click event handler you should reference the vehicle class - assuming, of course, it has a MustInherit method called GetInfo.
This method should return a string, and be overrided in the derived classes.
Then, once you get the info in the event handler, you should display in on the form.
Basically, something like this should get you started:
Public MustInherit Class Vehicle
' this method should only return the info,
' it's the calling method that should decide what to do with that info
Public MustOverride Function GetInfo() As String
End Class
Public Class Car
Inherits Vehicle
' return the car's specific info
Public Overrides Function GetInfo() As String
End Function
End Class
Public Class Motorcycle
Inherits Vehicle
' return the motorcycle's specific info
Public Overrides Function GetInfo() As String
End Function
End Class

Why base classes must have default new?

I got this error
https://learn.microsoft.com/en-us/dotnet/visual-basic/misc/bc30387
And I wonder why.
What about if I never want to call parameterless new in both base and derived classes?
I can do that if I don't use inheritance. Why using inheritance means I can no longer do so?
To repeat the issue
What's the explanation? So NOT every classes need parameterless new but classes with inheritance must? That doesn't make sense.
What about if I never call derived class with parameter less constructor. I don't intent for the class to ever be constructed without parameter.
For example, say, I want to create a class without parameterless constructor. I can do that right.
But say I want to split the class into two. Parent and child class. I want BOTH not to ever have parameterless constructor.
It seems that I can't do that can I? If such is the case, can anyone please confirm it.
The link says that if you have an explicit constructor in your base class with parameters and no parameterless one than you cannot leave your derived class without constructor. Because VB.NET cannot create an implicit constructor for derived class.
If you don't write any constructors for both, it is perfectly valid.
public class Base
End Class
Public Class Derived
Inherits Base
End Class
However you cannot declare a derived class without an explicit constructor like below. Because VB.NET cannot determine how to initialize base class.
public class Base
Public sub New(ByVal Item As Integer)
End Sub
End Class
Public Class Derived
Inherits Base
End Class
To overcome this issue you can declare a default constructor on derived class which calls base class constructor with a default value.
public class Base
Public sub New(ByVal Item As Integer)
End Sub
End Class
Public Class Derived
Inherits Base
Public Sub New()
MyBase.New(5)
End Sub
End Class

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

How to add an inner (nested) class to a library class?

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

How do I code these generic functions and classes inheritances correctly? (VB .NET)

I have a function which I need to call for three different types, with the underlying logic remaining the same for all the different types, so I figured it would be best to write this function using generics.
Here is the basic outline of the classes and functions involved:
'PO Base class'
Public MustInherit Class ProductionOrder
Public MustInherit Class Collection(Of T)
Inherits System.Collections.Generic.Dictionary(Of Long, T)
End Class
'....'
End Class
Public Class ProfileProductionOrder
Inherits ProductionOrder
Public Class Collection
Inherits ProductionOrder.Collection(Of ProfileProductionOrder)
End Class
'....'
End Class
Public Class UnitProductionOrder
Inherits ProductionOrder
Public Class Collection
Inherits ProductionOrder.Collection(Of UnitProductionOrder)
End Class
'....'
End Class
Public Class CrateProductionOrder
Inherits ProductionOrder
Public Class Collection
Inherits ProductionOrder.Collection(Of CrateProductionOrder)
End Class
'....'
End Class
'Generic function, intended to work on profile, unit, and crate production orders.'
'This method resides in the base class of the GUI.'
Protected Sub FillPOCells(Of T As ProductionOrder.Collection(Of ProductionOrder)) _
(ByVal dgv As DataGridView, ByVal ProductionOrders As T)
'...do some stuff'
End Sub
'This function resides in the Profile child GUI class.'
Protected Sub LoadDataGridViewPOs()
Dim dgv As DataGridView
Dim ProductionOrders As ProfileProductionOrder.Collection
'....'
'Fill PO Cells'
FillPOCells(Of ProfileProductionOrder.Collection)(dgv, ProductionOrders)
'....'
End Sub
The ProductionOrder base and child classes compile, as does the FillPOCells function. But when I call FillPOCells inside LoadDataGridViewPOs the compiler complains that "Type argument 'ProfileProductionOrder.Collection' does not inherit from or implement the constraint type 'ProductionOrder.Collection(Of ProductionOrder)'.
Also, here is some explanation about why things are set up this way. My predecessor set up the convention of putting the collection of an object as a subclass within it, so it's easy to refer to it as Obj.Collection. Next, the reason we need three different types of production orders is because they are treated differently and stored in different tables and such on the back end. Lastly, I realize I could implement this fairly easily without getting this particular generic function to work, but I'm looking at this as a learning experience to improve my understanding of generics and OO design.
So the question is, why am I getting that compiler error and how should I change my class and generics design to accomplish what I have in mind?
If you need any further explanation about what I'm trying to do or how I have things set up let me know. The main idea is to have a function that can take a collection who's elements belong to one of the ProductionOrder child classes, and run operations on these elements that only use the functionality held in their ProductionOrder base class (hence why either of the child types is okay to operate on in the function).
'PO Base class'
Public MustInherit Class ProductionOrder(Of T)
Public MustInherit Class Collection
Inherits System.Collections.Generic.Dictionary(Of Long, T)
End Class
'....'
End Class
Public Class ProfileProductionOrder
Inherits ProductionOrder(Of ProfileProductionOrder)
'....'
End Class
Public Class UnitProductionOrder
Inherits ProductionOrder(Of UnitProductionOrder)
'....'
End Class
Public Class CrateProductionOrder
Inherits ProductionOrder(Of CrateProductionOrder)
'....'
End Class
That is a lot simpler according to me.
But I higly doubt you need the CollectionClass.