473,320 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Converting type from base class to inherited class

I have created an abstract base class and several classes that inherit it.
i.e.

Public MustInherit Class Person
Public Property Name
......
End Property

Public MustOverride Sub Add()
End Class

Public Class Employee
Inherits Person

Public Property Salary
......
End Property

Public Overrides Sub Add()
....
End Sub
End Class

Public Class Customer
Inherits Person

Public Property LastOrder
.......
End Property

Public Overrides Sub Add()
....
End Sub
End Class

I am trying to create a new object and set it's properties in an asp.net
page

Dim bloke as Person

Select Case PersonType
Case 1
bloke = New Employee
bloke.Salary = intSalary
Case 2
bloke = New Customer
bloke.LastOrder = intOrderNumber
End Select

bloke.Name = strName
bloke.Add

When I try to compile this it fails, telling me that Salary is not a member
of Person even though at runtime the behaviour of bloke would be as an
Employee. Is there a way to get to the Salary property of the Employee
class?

Thanks,
Nick
Nov 20 '05 #1
5 1468
Try casting the object to the desired type:
CType(bloke, Employee).Salary = intSalary
--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan

"Nick Foster" <ni***********************@attenda.net> schreef in bericht
news:uv*************@tk2msftngp13.phx.gbl...
I have created an abstract base class and several classes that inherit it.
i.e.

Public MustInherit Class Person
Public Property Name
......
End Property

Public MustOverride Sub Add()
End Class

Public Class Employee
Inherits Person

Public Property Salary
......
End Property

Public Overrides Sub Add()
....
End Sub
End Class

Public Class Customer
Inherits Person

Public Property LastOrder
.......
End Property

Public Overrides Sub Add()
....
End Sub
End Class

I am trying to create a new object and set it's properties in an asp.net
page

Dim bloke as Person

Select Case PersonType
Case 1
bloke = New Employee
bloke.Salary = intSalary
Case 2
bloke = New Customer
bloke.LastOrder = intOrderNumber
End Select

bloke.Name = strName
bloke.Add

When I try to compile this it fails, telling me that Salary is not a member of Person even though at runtime the behaviour of bloke would be as an
Employee. Is there a way to get to the Salary property of the Employee
class?

Thanks,
Nick

Nov 20 '05 #2
Thank you! I had tried

bloke = CType(bloke,Employee)
bloke.Salary = intSalary

and that hadn't worked so I had been looking for another way.

Thanks again,
Nick

"Jan Tielens" <ja*@no.spam.please.leadit.be> wrote in message
news:ex**************@TK2MSFTNGP11.phx.gbl...
Try casting the object to the desired type:
CType(bloke, Employee).Salary = intSalary
--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan

"Nick Foster" <ni***********************@attenda.net> schreef in bericht
news:uv*************@tk2msftngp13.phx.gbl...
I have created an abstract base class and several classes that inherit it. i.e.

Public MustInherit Class Person
Public Property Name
......
End Property

Public MustOverride Sub Add()
End Class

Public Class Employee
Inherits Person

Public Property Salary
......
End Property

Public Overrides Sub Add()
....
End Sub
End Class

Public Class Customer
Inherits Person

Public Property LastOrder
.......
End Property

Public Overrides Sub Add()
....
End Sub
End Class

I am trying to create a new object and set it's properties in an asp.net
page

Dim bloke as Person

Select Case PersonType
Case 1
bloke = New Employee
bloke.Salary = intSalary
Case 2
bloke = New Customer
bloke.LastOrder = intOrderNumber
End Select

bloke.Name = strName
bloke.Add

When I try to compile this it fails, telling me that Salary is not a

member
of Person even though at runtime the behaviour of bloke would be as an
Employee. Is there a way to get to the Salary property of the Employee
class?

Thanks,
Nick


Nov 20 '05 #3
Agreed that you need to do the cast, since you DIM'ed
bloke as the generic person and the compiler can't know
that it's the more specific employee.

Other options are to dim these two separately (although
it looks like you are not doing that on purpose for some
reason), or to create Salary as overridable up in person
and then just ignore it for customer types.

Finally, you have to decide whether the employee and
customer animals are all that similar anyway--it's
possible you're going to jump through hoops to get them
to work this way, when maybe they're too different to
bother, and what you really want is rather than customer
and employee sharing a lineage that they instead share a
common structure for the basics like name/address, etc.

hth,

Bill Borg
-----Original Message-----
Try casting the object to the desired type:
CType(bloke, Employee).Salary = intSalary
--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan

"Nick Foster" <ni***********************@attenda.net> schreef in berichtnews:uv*************@tk2msftngp13.phx.gbl...
I have created an abstract base class and several classes that inherit it. i.e.

Public MustInherit Class Person
Public Property Name
......
End Property

Public MustOverride Sub Add()
End Class

Public Class Employee
Inherits Person

Public Property Salary
......
End Property

Public Overrides Sub Add()
....
End Sub
End Class

Public Class Customer
Inherits Person

Public Property LastOrder
.......
End Property

Public Overrides Sub Add()
....
End Sub
End Class

I am trying to create a new object and set it's properties in an asp.net page

Dim bloke as Person

Select Case PersonType
Case 1
bloke = New Employee
bloke.Salary = intSalary
Case 2
bloke = New Customer
bloke.LastOrder = intOrderNumber
End Select

bloke.Name = strName
bloke.Add

When I try to compile this it fails, telling me that Salary is not a
member
of Person even though at runtime the behaviour of

bloke would be as an Employee. Is there a way to get to the Salary property of the Employee class?

Thanks,
Nick

.

Nov 20 '05 #4
Bill,

The example I gave is a very simplified version of the actual code. The
abstract base has about 20 or so properties that the 8 slightly different
implementation classes share and they are only different by a couple of
properties and methods. The problem is I dont want Customer to expose the
Salary property or Employee to expose LastOrder and I can't see a way in
vb.net to create a kind of optional-to-inherit property in the abstract
class.

Cheers,
Nick
"Bill Borg" <an*******@discussions.microsoft.com> wrote in message
news:02****************************@phx.gbl...
Agreed that you need to do the cast, since you DIM'ed
bloke as the generic person and the compiler can't know
that it's the more specific employee.

Other options are to dim these two separately (although
it looks like you are not doing that on purpose for some
reason), or to create Salary as overridable up in person
and then just ignore it for customer types.

Finally, you have to decide whether the employee and
customer animals are all that similar anyway--it's
possible you're going to jump through hoops to get them
to work this way, when maybe they're too different to
bother, and what you really want is rather than customer
and employee sharing a lineage that they instead share a
common structure for the basics like name/address, etc.

hth,

Bill Borg

Nov 20 '05 #5
Nick,
In addition to the others comments.

Rather then setting properties on the bloke object, have you considered
passing them as parameters to the constructor? In this case it simplifies
your code immensely!
Dim bloke as Person

Select Case PersonType
Case 1
bloke = New Employee(strName, intSalary)
Case 2
bloke = New Customer(strName, intOrderNumber)
End Select

bloke.Add Public MustInherit Class Person
' this is protected as only derived classes can call it,
' as the class is abstract (MustInherit)
Protected Sub New(name As String)
Me.Name = name
End Sub
Public Property Name
......
End Property Public Class Employee
Inherits Person
Public Sub New(name As String, salary As Integer)
MyBase.New(name)
me.Salary = salary
End Sub
Public Property Salary
......
End Property

Hope this helps
Jay

"Nick Foster" <ni***********************@attenda.net> wrote in message
news:uv*************@tk2msftngp13.phx.gbl... I have created an abstract base class and several classes that inherit it.
i.e.

Public MustInherit Class Person
Public Property Name
......
End Property

Public MustOverride Sub Add()
End Class

Public Class Employee
Inherits Person

Public Property Salary
......
End Property

Public Overrides Sub Add()
....
End Sub
End Class

Public Class Customer
Inherits Person

Public Property LastOrder
.......
End Property

Public Overrides Sub Add()
....
End Sub
End Class

I am trying to create a new object and set it's properties in an asp.net
page

Dim bloke as Person

Select Case PersonType
Case 1
bloke = New Employee
bloke.Salary = intSalary
Case 2
bloke = New Customer
bloke.LastOrder = intOrderNumber
End Select

bloke.Name = strName
bloke.Add

When I try to compile this it fails, telling me that Salary is not a member of Person even though at runtime the behaviour of bloke would be as an
Employee. Is there a way to get to the Salary property of the Employee
class?

Thanks,
Nick

Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Josh Mcfarlane | last post by:
I'm doing recomposition of objects from binary streams, and the best way for me to write them out is to write base class data first, forward to inherited classes, pointer class values, etc. Now,...
8
by: TS | last post by:
I am trying to get set a property of a control on the inherited class from base class. I imagine i have to use reflection, so could someone give me the code to do it? something like this?...
7
by: Santi | last post by:
I have two classes: Product and Fruit which inherits from Product. If I try to narrow the reference to the base type by a cast, I always get a reference to the inherited type. For example: ...
5
by: Robert | last post by:
I have a series of web applications (configured as separate applications) on a server. There is a main application at the root and then several virtual directories that are independant...
3
by: Jeff User | last post by:
Hello I am using C#, .net1.1 Vis Studio 2003 I am using homeBase.aspx.cs page as a base for several other aspx/aspx.cs web pages. The base page handles some operations that are common to all...
6
by: roland.bali | last post by:
Hi, Here is the basic setup, my base class is Shoe which has a child class called Sandal. I would like to create objects by calling Sandal.Load. But without overloading Load in Sandal and...
3
by: Magnus | last post by:
i have a class which inherits from a base class class base1 { } class child1 : base1 { } how can I determine if child1 is of base1? gettype returns type of child1, but I need to know the...
5
by: Eliseu Rodrigues | last post by:
Hi I would like to have a static method on a base class that executes some action (for example retrieves the row count) on a table whose name is the same of the inherited class name. For...
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.