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
Related
So to start this off; I'm a beginner in VisualBasic.Net and my classes require me to learn it. The current subject is object constructors and constructor methods. The current exercise (it's not graded or an exam) is requiring us to make a parent class with a constructor method, and a child class with a new() that calls said function. It looks a bit like this;
Protected MustInherit Class Vehicle()
Protected ReadOnly Property Serial_No As Integer
Protected Property Mileage As Integer
Protected Property Color As String
Protected Function CreateVehicle() As Object
End Function
End Class
Public Class Car
Inherits Vehicle
Public ReadOnly Property Car_Type As String
Public Sub New()
End Sub
End Class
The thing I'm having issues with is that I'm not sure how to go about it? Can't ReadOnly properties ONLY be edited in the constructor itself, and doesn't the object need to be initialized in the constructor? Is there something particular I need to add in the CreateVehicle function?
I did ask the teacher but his answer was 'just give up on it and go do something else', which is ultimately pretty unhelpful.
Edit: (added the inheritance to the child class)
So, after being asked for clarification on what I'm trying to do; the exercise itself is not entirely about doing this, but it is the thing in the exercise that I'm struggling with. The goal is to create a Car object utilizing the constructor (New()), but the constructor must call a secondary function located inside the parent class, Vehicle.
My issue is the following : I'm not sure how to go about implementing the function inside the constructor. I know how to call methods/subs/functions and how to get returns from them, but I'm not sure on how I would go about returning a ReadOnly property's values from a secondary function. Don't readonly properties become uneditable outside of the constructor?
I could always return each value separately instead of as an object, and then set the Car object's values to be equal to the return of the function, individually. But then what's the point of calling a separate function instead of just passing everything as a parameter and doing it directly in the constructor?
This is probably what your teacher is looking for:
Public MustInherit Class Vehicle
Protected ReadOnly Property Serial_No As Integer
Protected Sub New(serialNumber As Integer)
Me.Serial_No = serialNumber
End Sub
End Class
Public Class Car
Inherits Vehicle
Public ReadOnly Property Car_Type As String
Public Sub New(serialNumber As Integer, carType As String)
MyBase.New(serialNumber)
Me.Car_Type = carType
End Sub
End Class
Both constructors take in parameters so the ReadOnly properties can be set.
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.
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
I'm trying to create a control (Parent Class) that uses a custom grid (Child Class). The grid has a series of constructors and methods for populating itself based on property values in the [parent] control.
The only way I found to make these property values available to the grid is by making them Shared but that's causing me all kinds of issues.
REQUIREMENTS
Properties in the control (parent) must be accessible to the grid (child).
Properties in the control must be visible in the design-time properties explorer.
The grid class must only be instantiable by the parent class.
As a side note: please indicate if your answer will allow me to share properties/methods back and forth between child and parent. That would be nice, but just a bonus.
Thanks ;)
EDIT - A VERY simple example based on my situation:
Partial Public Class catContent
Inherits System.Web.UI.UserControl
Protected Sub Page_Load(sender, e) Handles Me.Load
Page.Controls.Add(New CategoryResultGrid(category))
End Sub
Private Shared _product As String = String.Empty
Shared Property Product() As String
Get
Return _product
End Get
Set(ByVal value As String)
_product = value.Trim()
End Set
End Property
Private Class CategoryResultGrid
Inherits GridView
Sub New(ByVal category As String)
'How do I access "Product" here without sharing it?
End Sub
End Class
End Class
Do NOT use shared, it will break your app as soon as you put more than one of you custom control in your app.
If you only want the Grid to exist in the context of the Parent control, then consider exposing it similar to how the ListView control exposes its Items collection.
If you want your Grid to access fields in the (parent) Control there are several ways to do that. You could pass an instance of the Parent to the Grid, you can let the Grid use the standard Control methods to get its parent reference, or you can implement the Grid as an inner class of the Parent.
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.