[RESOLVED] property and method sharing using inheritance
Hi Friends,
i have written simple application to perform insert , update and delete in my small application . and in that application i have written person class . i just want to understand the inheritance . so that i should implement in any of my application inheritance sharing of properties and method in other userdefined class . how it can be possible . and i have used mypersons dictionary to fill all the records in the datagridview. let me know please .how should i use great feature inheritance . any help would be highly appreciated .
Code:
Public Class Person
Private m_Key As String
Private m_FirstName As String
Private m_LastName As String
Private m_Street As String
Private m_City As String
Public Property Key() As String
Get
Key = m_Key
End Get
Set(ByVal Value As String)
m_Key = Value
End Set
End Property
Public Property FirstName() As String
Get
FirstName = m_FirstName
End Get
Set(ByVal Value As String)
m_FirstName = Value
End Set
End Property
Public Property LastName() As String
Get
LastName = m_LastName
End Get
Set(ByVal Value As String)
m_LastName = Value
End Set
End Property
Public Property City() As String
Get
City = m_City
End Get
Set(ByVal Value As String)
m_City = Value
End Set
End Property
Public Property Street() As String
Get
Street = m_Street
End Get
Set(ByVal Value As String)
m_Street = Value
End Set
End Property
End Class
What exactly do you think is going to inherit what? Inheritance works when one type is a more specific version of another type, e.g. a cat is an animal but more specific so a Cat class could inherit an Animal class. Do you intend to implement some other type that is a person but more specific? Inheritance isn't some magic bullet. You use it where it's appropriate and not where it isn't.
Public Class Employee
Inherits Person
Public Property EmployeeNumber as string
Public Property HireDate as DateTime
Public Property TerminateDate as DateTime
End Class
Code:
Public Class Manager
Inherits Employee
Public Property Subordinates as List(of Employee)
End Class
So you can see (hopefully) how Employee inherits from Person, because it (Employee) is a more specific type of Person ... meanwhile Manager is a more specific type of employee, which is why it inherits from EMployee and not Person. At each point, new properties are added in that apply to that level of object...
i have written now employee class as follows .my question is how it is behavior sharing .so that no need person properties and methods in employee class as well . property in employee .can you just demostrate . any help would be highly appreciated .
Code:
Public Class Employee
Inherits Person
Private m_EmployeeNumber As String
Private m_Hiredate As Date
Private m_Terminationdate As Date
Public Property employeeNumber() As String
Get
employeeNumber = m_EmployeeNumber
End Get
Set(ByVal value As String)
m_EmployeeNumber = value
End Set
End Property
Public Property Hiredate() As Date
Get
Hiredate = m_Hiredate
End Get
Set(ByVal value As Date)
m_Hiredate = value
End Set
End Property
Public Property terminationdate() As Date
Get
terminationdate = m_Terminationdate
End Get
Set(ByVal value As Date)
m_Terminationdate = value
End Set
End Property
End Class
Last edited by firoz.raj; Mar 8th, 2016 at 12:38 AM.
WEll, a person has a Name right? So would an employee... but you don't re-implement the name property do you? No, you implement it ONCE in the Person class... since an Employee inherits form Person, that includes all functionality that the PErson class has, including the name property as well.
Sooo....
Code:
Dim customer as New Person
Dim barista as New Employee
customer.FirstName = "TechGnome" ' this works because .FirstNAme is a property of Person
bnarista.FirstName = "John" 'This also works because it's a property of Person, which the Employee class inherited.
Dim storeOccupants as New List(Of Person)
storeOccupants.Add(customer)
storeOccupants.Add(barista) ' Again, since Employee inherits from person, we can do this....
Dim storeEmployees as New List(Of Employee)
storeEmployee.Add(barista) ' barista is an employee...
storeEmployee.Add(customer) ' Will result in an error because while employee is a person, a person is not an employee...
You can see that it's less code right off the bat but you also have the advantage of being able to pass an Employee object anywhere that a Person object is required. For instance, if you wanted to display someone's name then, without inheritance, you'd have to have two methods: one for a Person and one for an Employee. With inheritance, you just need one method for a Person and you can pass an Employee to that because an Employee is a Person.
that is ok .#6 is the best example to show the ability of code sharing using inheritance . if we have one class . then it is easier to work with generic collection like the following, List(of Employee) , dictinary col . because simple holding all the properties and methods on the one collection.and using it for reporting like placing on the crystal report , excel report . how both can be used in the following case .i just want to make more practical inheritance in my all the database application .
Code:
Public Class AllPersons
Implements System.Collections.IEnumerable
Dim x As Long
Dim mCol As New Dictionary(Of String, Person)
Public Function Add(ByRef objnewmember As Person) As Person
x = x + 1
objnewmember.Key = "K" & CStr(x)
Call mCol.Add(objnewmember.Key, objnewmember)
'return the object created
Add = objnewmember
objnewmember = Nothing
End Function
Default Public ReadOnly Property Item(ByVal sKey As String) As Person
Get
If mCol.ContainsKey(sKey) Then
Item = mCol.Item(sKey)
Else
Item = Nothing
End If
End Get
End Property
Public ReadOnly Property Count() As Integer
Get
Count = mCol.Count()
End Get
End Property
Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
'Dim kvp As KeyValuePair(Of String, Person)
'For Each kvp In mCol
' Return kvp.Value
'Next
GetEnumerator = mCol.GetEnumerator()
End Function
Public Sub Remove(ByRef SKey As String)
mCol.Remove(SKey)
End Sub
End Class
and following way i am using all the data of class .i am just want to see how many class can be used in this case to get correct data from the database .
Code:
adapter.Fill(tab)
'Filling into the self created CollectionClass AllPersons
'AllPersons internally contains a Dictionary
For Each row In tab.Rows
person = New Person
person.FirstName = row.Item(1)
person.LastName = row.Item(2)
person.Street = row.Item(3)
person.City = row.Item(4)
MyPersons.Add(person)
Next
' Noe reading Access
' The retirnType of a Dictionary Enumaration is a KeyValuePair so we do
' we declair a varuable called 'pair' as this KeyValuePair
Dim pair As KeyValuePair(Of String, Person)
' now we can use For each Loop
For Each pair In MyPersons
'Pair.Value is a Person Class
person = pair.Value
FirstName = person.FirstName
Next
Last edited by firoz.raj; Mar 8th, 2016 at 04:22 AM.
An obscure body in the SK system. The inhabitants call it Earth
Posts
7,957
Re: property and method sharing using inheritance
The first snippet (AllPersons) would just work. Because an Employee Is a person you could put it in an AllPersons collection. The AllPersons collection would not even be aware that it's an employee.
The second snippet is more complex, or not, depending on what you're asking.
Assuming you just want to load up rows from your dataset and convert them to Persons in your MyPersons then, again the code you have will just work because an Employee Is a person.
However, a Person is not necessarily an Employee. Your code treats everything as a Person so nothing going in there is typed as an Employee, even if they are one. That may or may not matter depending on how you're going to use this collection later. If, when you come to use it, you only care that a Person is a Person then your code is fine. But if you want to know that a particular person in that collection is an Employee then you would either need to have declared it as an employee or you would need to write some sort of check at the point of consumption.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
ok that i have seen employee is type of person . so employee will be inherit from the person class.
and i need to create the instance of employee . and that way . when we will type emp.property .it will show the list of property existing in employee class as well as those property which existing in person class too while coding. that is great .i did not work on transaction on vb.net . can anyone tell me . how should i use transcation to store in employee class too .following are my code what i did .
Code:
Private Sub BtSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtSave.Click
Dim _con As New OleDb.OleDbConnection
Dim _cmd As New OleDb.OleDbCommand
Try
If Not checkinput() Then Exit Sub
_con = DataStore.GetConnection
_cmd.Connection = _con
'add in to emp class
_emp.FirstName = TxtFirstName.Text
_emp.LastName = TxtLastName.Text
_emp.employeeNumber = TxtEmpNo.Text
_emp.Hiredate = dtpJoiningdate.ToString(" yyyy-MM-dd")
_emp.terminationdate = TxtTerminationDate.Text
_cmd.CommandText = "INSERT INTO PERSON(FirstName, LastName) values (@FirstName, @LastName)"
_cmd.Parameters.AddWithValue("@FirstName", _emp.FirstName)
_cmd.Parameters.AddWithValue("@LastName", _emp.LastName)
_cmd.ExecuteNonQuery()
MessageBox.Show("Employee information stored")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Last edited by firoz.raj; Mar 20th, 2016 at 07:09 AM.
Reason: please delete this post
ok that i have seen employee is type of person . so employee will be inherit from the person class.
and i need to create the instance of employee . and that way . when we will type emp.property .it will show the list of property existing in employee class as well as those property which xisting in person class too while coding. that is great .i did not work on transaction on vb.net . can anyone tell me . it says the following .while i debug . any suggestion please .ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.
Code:
Private Sub BtSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtSave.Click
Dim _con As New OleDb.OleDbConnection
Dim _cmd1 As New OleDb.OleDbCommand
Dim _cmd2 As New OleDb.OleDbCommand
Dim tr As OleDb.OleDbTransaction
Try
If Not checkinput() Then Exit Sub
_con = DataStore.GetConnection
tr = _con.BeginTransaction()
_cmd1.Connection = _con
'add in to emp class
_emp.FirstName = TxtFirstName.Text
_emp.LastName = TxtLastName.Text
_emp.employeeNumber = TxtEmpNo.Text
_emp.Hiredate = dtpJoiningdate.Value.Date
' _emp.terminationdate = TxtTerminationDate.Text
_cmd1.CommandText = "INSERT INTO PERSON(FirstName, LastName) values (@FirstName, @LastName)"
_cmd1.Parameters.AddWithValue("@FirstName", _emp.FirstName)
_cmd1.Parameters.AddWithValue("@LastName", _emp.LastName)
_cmd1.ExecuteNonQuery()
_cmd2.CommandText = "INSERT INTO PERSON(EmployeeID,hiredate) VALUES(@EmployeeID,@hiredate)"
_cmd2.Parameters.AddWithValue("@EmployeeID", _emp.employeeNumber)
_cmd2.Parameters.AddWithValue("@hiredate", _emp.Hiredate)
_cmd2.ExecuteNonQuery()
tr.Commit()
MessageBox.Show("Employee information stored")
Catch ex As Exception
tr.Rollback()
MessageBox.Show(ex.Message)
End Try
End Sub
Last edited by firoz.raj; Mar 21st, 2016 at 12:29 AM.
There's no need for a transaction... now I think you're just using things for the sake of using them... and that's not the right way to go about it... learning to know WHEN to use something or not is part of learning things... in this case there's no reason for that... you're inserting into the PERSON table twice for no reason. You're also creating a n _emp class for no reason... to quote a great commercial "that's not how this works. That's not how any of this works."
You create the class to transport data, you're not transporting anything here. Also, the save typically would get packaged up into the class, so that it can save itself.
I'm having trouble wrapping my head around your code because it is....... so... different from the way I'd do it, I can't tell if that's because of personal style reasons, or if it's because it's contrived example for learning... I try to cut it some slack figuring it's more the latter than the former...
You create the class to transport data, you're not transporting anything here. Also, the save typically would get packaged up into the class, so that it can save itself.
HI techgnome,
i am simple moving data from text box to class . and finally into a person table . that is ok .i am able to store the data into person table . joining date and termination date is the part of employee table . in person table we have personid and in employee table we have employee id which is foreign key for other fields by which we want to store joining date and termination date . and i just want how we can store in the emp table too .because joining date and terminationdate is not available in person table and that is all about .
That's not what I meant... you're not adding the class to a list. you're not doing anything with it, it's simply being released at the bottom of the procedure... by transporting it, I don't mean doing this:
Code:
B = a
.parameter.addwithValue(b)
'done
Why bother? Just set the parameter to a and be done with it. B served no purpose ...
Now, this would be more appropriate:
Code:
Private Sub SaveEmployee()
dim empl as New Employee
empl.FirstName = TxtFirstName.Text
empl.LastName = TxtLastName.Text
empl.employeeNumber = TxtEmpNo.Text
empl.Hiredate = dtpJoiningdate.Value.Date
empl.terminationdate = TxtTerminationDate.Text
dim da as New DataAccessClass
da.EmployeeSave(empl)
End sub
THAT's what I mean by transporting it... I create an employee object, I fill it's properties with the data... I then figuratively throw it over the wall to another class that knows what to do with it - in the mean time I don't care what is done with it... it could be saved to a database, or a text file, or the registry, or beamed out into space for all I know.
Note, I also created a LOCAL instance of the class... because that's all I need it for... it doesn't (and shouldn't) be scoped any higher than that. The object really should only live for as long as it is needed. Since it is only needed long enough to be passed to the actual save routine, that's where it is scoped at.
That's not what I meant... you're not adding the class to a list. you're not doing anything with it, it's simply being released at the bottom of the procedure... by transporting it, I don't mean doing this:
are you talking about list collection here .
Code:
Dim _con As New OleDb.OleDbConnection
Dim _cmd1 As New OleDb.OleDbCommand
Dim _cmd2 As New OleDb.OleDbCommand
Dim dataaccess As New List(Of Employee)
' Dim tr As OleDb.OleDbTransaction
Try
' If Not checkinput() Then Exit Sub
_con = DataStore.GetConnection
_cmd1.Connection = _con
_emp.FirstName = TxtFirstName.Text
_emp.LastName = TxtLastName.Text
_emp.employeeNumber = TxtEmpNo.Text
_emp.Hiredate = dtpJoiningdate.Value.Date
dataaccess.Add(_emp)
' _emp.terminationdate = TxtTerminationDate.Text
_cmd1.CommandText = "INSERT INTO PERSON(FirstName, LastName) values (@FirstName, @LastName)"
_cmd1.Parameters.AddWithValue("@FirstName", _emp.FirstName)
_cmd1.Parameters.AddWithValue("@LastName", _emp.LastName)
_cmd1.ExecuteNonQuery()
Ok... 1) I didn't appreciate the PM... as I mentioned, I subscribe to all of the threads I reply to (until I get too annoyed with it) ... so as soon as I check in, I'll see it... 2) I am really under no obligation to help, especially through a PM... if you have a question, post it in the thread... as mention in #1 - I subscribe to all of the threads I reply to... so as soon as I check in, I'll see it ... 3) Now you're just adding crap to justify... where'd this list suddenly come from? it wasn't there before. And once again it's local to the sub... so as soon as the sub exits, it's gone too... so what's the point? I know you're trying to learn... but you're just slinging code around for... I don't know why... you took a small piece of info, and are trying to shoe-horn it into a trite, trivial place where it doesn't belong in an effort to justify it. Just based off the code as it is, the list is of no value either... but all we're seeing is just a very small piece of things... so maybe it does make sense... I suspect that it probably doesn't... I mentioned the the class isn't being added to a list so you've whipped up a list to jsutify the use of the class ... but I couldn't tell you if the list makes sense... in the case of the code as it is though, it doesn't. I even showed and example of how to use the class, based directly off the code snip you posted...
Again... there's more to learning how to build and use classes... you also need to have a grasp on when to use them (or not). This is where a lot of peopel get into a trap... they try to use their previous code as an example and what you get is the (in my opinion) crap code like you have above. It's junk and isn't useful - it needs to be refactored...
I don't know why... you took a small piece of info, and are trying to shoe-horn it into a trite, trivial place where it doesn't belong in an effort to justify it. Just based off the code as it is, the list is of no value either... but all we're seeing is just a very small piece of things... so maybe it does make sense... I suspect that it probably doesn't... I mentioned the the class isn't being
hi i just have asked what are a=b and why you are adding this b in paramerter .this you have mentioned . nothing else . and instead of explaining simple you got angry at me . sorry this is my mistake i have asked to you for help . the list does not make sense i know it .
Code:
B = a
.parameter.addwithValue(b)
'done
Last edited by firoz.raj; Mar 22nd, 2016 at 11:32 PM.