Inheritance Question

G

Guest

Hello All:

I work at an insurance company and we are changing the design of an ASP.NET
application (for those have followed my postings, we're going to redesign
some parts of the application - thank you Kevin and Bruce!). The application
processes claims. We have two types of claims, PClaim and WClaim (property
claims and worker's comp claims). PClaim and WClaim share some common traits
so I can have them inherit from an abstract Claim class and not have to worry
about implementing these in the PClaim and WClaim classes.

PClaim and WClaim also have some differing traits. So I can define in each
of their classes the properties and methods that are particular to each type
of claim. So far, so good.

Implementing this, I find:

Dim myClaim As New PClaim
myClaim.whatever() //shows properties and methods for Claim and
PClaim

Dim myClaim As New Claim
myClaim.whatever() //shows properties and methods for Claim only

But when I do this, I don't see any of the properties or methods defined in
PClaim or WClaim.

Can someone help me to understand why this is and how I could restructure
this to get what I want.

I would like to design this so that a PClaim or a WClaim can be generically
treated as a Claim and have the claim carry it's claim type (i.e.
myClaim.ClaimType = ClaimType.Property or myClaim.ClaimType =
ClaimType.WorkersComp) so that the business layer can retrieve the claim's
data from the appropriate source (i.e. the business layer class irequests
the claim type from the claim and, depending on the type of claim, retrieves
the claim's info from either a third-party system or a home-grown database).
This way I can avoid having to maintain two seperate calls in the busines
layer to retrieve claim data. In other words I would like to code this:

Function RetrieveClaimInfo(c as Claim) as Claim
'code checks claim type and retrieves data from appropriate source
'then retrurns Claim with either the appropriate PClaim properties and
methods
'or Claim with either the appropriate WClaim properties and methods
End Function

as opposed to this:

Function RetrievePropertyClaimInfo(c as PClaim) as PClaim
'code retrieves data from appropriate source and returns a PClaim
End Function

Function RetrieveWorkersCompClaimInfo(c as WClaim) as WClaim
'code retrieves data from appropriate source and returns a WClaim
End Function

I would also like to avoid having several places in the UI project where I
would have to code:

If (PropertyClaim) Then
Dim myClaim As New PClaim
Else
Dim myClaim As New WClaim
End If

I would appreciate any help with this.

TiA,
 
S

S. Justin Gengo

Joe,

Since PClaim and WClaim are inheriting Claim, Claim will never be able to
show their properties only its own.

A simple solution to what you want to do would be to give Claim an
enumerated list of claim types:

Enum ClaimTypes
PClaim
WClaim
End Enum

and a public property for setting the claim type:

Private _ClaimType As ClaimTypes

Public Property ClaimType as ClaimTypes
Get
Return _ClaimType
End Get
Set (ByVal Value As ClaimTypes)
_ClaimType = Value
End Set
End Property

Then in PClaim and WClaim use the new constructor to set the claim type:

Public Sub New()
Me.ClaimType = ClaimTypes.PClaim
End Sub

Then when you reference either as the base Claim you may check the property
to see which type it is:

If Claim.ClaimType = ClaimTypes.PClaim Then
'---Do what you want for a PClaim here

Else
'---Do what you want for a WCalime here

End If

If more claim types are involved use a select case instead of the if/then.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
M

Marina

How did you define the 'whatever' method?

Define it as overridable. Both PClaim and WClaim override 'whatever' and
have their own implementation. Then when you have a Claim object, and you
call 'whatever' on it, the correct version will be called.
 
G

Guest

Thanks. So far this is what I have. Now, how do I instantiate PClaim or
WClaim with this constructor? If I write:

Dim c as Claim = new PClaim(ClaimType.PC)

then I get a Claim object without any of the properties or methods
specifically associated with a PClaim, just the properties or methods
associated with Claim. This wouldn't give me the properties that I would
want to populate for a PClaim.

On the other hand, if I code:

Dim c as PClaim = new PClaim(ClaimType.PC)

then I get the properties and methods of PClaim (which include the
properties and methods of the Claim). This would give me the type of claim
that I want, but will not allow me to create a single Business layer method
to encapsulate the data retrieveal:

'in Business layer
Public Function RetrieveClaimInfo(c as Claim) as Claim
If c.ClaimType = ClaimTypes.PClaim Then
'populate PClaim properties
Return PClaim

Else
''populate WClaim properties (many of which are different that
PClaim props)
Return WClaim
End If

End Function

I had hoped for one Business layer function that returns a claim, whether
the claim is a PClaim or a WClaim. I just don't know if this is possible.

It appears that I need two Business layer methods; one that would accept and
return each type of claim. If I had three types of claims, I would need
three Business layer methods, etc.

Have I missed something or do you agree?

Thanks,

--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation


S. Justin Gengo said:
Joe,

Since PClaim and WClaim are inheriting Claim, Claim will never be able to
show their properties only its own.

A simple solution to what you want to do would be to give Claim an
enumerated list of claim types:

Enum ClaimTypes
PClaim
WClaim
End Enum

and a public property for setting the claim type:

Private _ClaimType As ClaimTypes

Public Property ClaimType as ClaimTypes
Get
Return _ClaimType
End Get
Set (ByVal Value As ClaimTypes)
_ClaimType = Value
End Set
End Property

Then in PClaim and WClaim use the new constructor to set the claim type:

Public Sub New()
Me.ClaimType = ClaimTypes.PClaim
End Sub

Then when you reference either as the base Claim you may check the property
to see which type it is:

If Claim.ClaimType = ClaimTypes.PClaim Then
'---Do what you want for a PClaim here

Else
'---Do what you want for a WCalime here

End If

If more claim types are involved use a select case instead of the if/then.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
B

Bruce Barker

the easiest in the UI layer have a methods

void ProcessClaim(WClaim claim)
{
// do wc claim
}
void ProcessClaim(PClaim)
{
// do p cliam
}
void ProcessClaimCommon(Claim claim)
{
// do common stuff
}

the in the UI

Claim claim = BI.NewClaim(); // returns a PCLaim or WClaim
ProcessClaimCommon(claim); // do common
ProcessClaim(claim); // do specific

-- bruce (sqlwork.com)
 
G

Guest

Hi Bruce,

How does Claim claim = BI.NewClaim() return a PClaim or a WClaim? It
returns a Claim. Since, PClaim inherits from Claim and WClaim inherits from
Claim, if I return a Claim, I'll only get the propertiers implemented in
Claim but will loose the specific properties implemented in PClaim or WClaim.
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation


Bruce Barker said:
the easiest in the UI layer have a methods

void ProcessClaim(WClaim claim)
{
// do wc claim
}
void ProcessClaim(PClaim)
{
// do p cliam
}
void ProcessClaimCommon(Claim claim)
{
// do common stuff
}

the in the UI

Claim claim = BI.NewClaim(); // returns a PCLaim or WClaim
ProcessClaimCommon(claim); // do common
ProcessClaim(claim); // do specific

-- bruce (sqlwork.com)
 
S

S. Justin Gengo

Joe,

You want to get back a claim, but then test that claim object via it's claim
type property and cast it as the claim you want to work with:

Dim Claim as Claim = MyClaim
Dim PClaim As PClaim
Dim WClaim As WClaim

If Claim.ClaimType = ClaimType.PClaim Then
PClaim = CType(Claim, PClaim)
Else
WClaim = CType Claim, WClaim)
End If

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
Joe said:
Thanks. So far this is what I have. Now, how do I instantiate PClaim or
WClaim with this constructor? If I write:

Dim c as Claim = new PClaim(ClaimType.PC)

then I get a Claim object without any of the properties or methods
specifically associated with a PClaim, just the properties or methods
associated with Claim. This wouldn't give me the properties that I would
want to populate for a PClaim.

On the other hand, if I code:

Dim c as PClaim = new PClaim(ClaimType.PC)

then I get the properties and methods of PClaim (which include the
properties and methods of the Claim). This would give me the type of
claim
that I want, but will not allow me to create a single Business layer
method
to encapsulate the data retrieveal:

'in Business layer
Public Function RetrieveClaimInfo(c as Claim) as Claim
If c.ClaimType = ClaimTypes.PClaim Then
'populate PClaim properties
Return PClaim

Else
''populate WClaim properties (many of which are different that
PClaim props)
Return WClaim
End If

End Function

I had hoped for one Business layer function that returns a claim, whether
the claim is a PClaim or a WClaim. I just don't know if this is possible.

It appears that I need two Business layer methods; one that would accept
and
return each type of claim. If I had three types of claims, I would need
three Business layer methods, etc.

Have I missed something or do you agree?

Thanks,
 
M

mszanto

The real challenge here is that you want a single business layer that
accepts any number of types of claims without creating a business layer
for each type of claim. To accomplish this, you need to understand the
concept of downcasting and upcasting.
(Page 56 - C# In a Nutshell - O'Reilly)

In short any base class can be downcast to a derived class and any
derived class can be upcast to its base class and maintain the data
specific to the derived class.
What this means is that you can create one business layer method that
accepts the base class Claim as it's argument then recasts it back to
its appropriate type in order to retrieve its properties.

For example, given that you have a base class Claim, and derived
classes WClaim and PClaim you can create a single business layer
function and you don't even need any overloaded versions to handle
different claim types...

public Claim DoMyBusiness(Claim myClaim) // notice that it accepts the
base (abstract) class as an argument
{
PClaim myPClaim;
WClaim myClaim;
// first determine what type myClaim is...
if (myClaim.GetType() == typeof(PClaim)) // if this is a PClaim
then...
myPClaim = (PClaim)myClaim; // ...cast myClaim to a PClaim
(downcast)
else if (myClaim.GetType() == typeof(WClaim)) // if this is a WClaim
then...
myWClaim = (WClaim)myClaim; // ...cast myClaim to a WClaim
(downcast)

// note: to compare types in vb use...
// If TypeOf myClaim is WClaim Then

// now that I've recast myClaim to its proper claim type I can
retrieve the properties specific to that claim type that were set in
the calling function.
if (PClaim != null)
{ //get a PClaim using properties specific to a PClaim}
else if (WClaim != null)
{ get a WClaim using... }

// next, do something thats generic to all claim types, I.e. my
general business layer stuff
// if this general business logic is common to all claim types and
uses only properties of the base class
// then you could either refer back to myClaim or upcast the
retrieved claim back to a Claim.
// that way you don't have to have a bunch of code to check what type
of claim it is.

// finally return either PClaim or WClaim
// Its important to note that even though DoMyBusiness returns an
object of type Claim,
// you can also return objects of type PClaim or WClaim because they
are derived from Claim
// Also, when that object is returned back to the calling function it
is automatically downcast
// which means that it will be returned as the proper claim type with
all properties specific to the claim type

if (PClaim != null)
{ return PClaim }
else if (WClaim != null)
{ return WClaim}
}
 
M

mszanto

I just realized that I have a couple of typos.
Please use the following corrections:

if (myPClaim != null)
{ //get a PClaim using properties specific to a PClaim}
else if (myWClaim != null)
{ get a WClaim using... }

if (myPClaim != null)
{ return myPClaim }
else if (myWClaim != null)
{ return myWClaim}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top