OT: Generics... Declaring a Variable

OT: Generics... Declaring a Variable

Old forum URL: forums.lhotka.net/forums/t/2119.aspx


ward0093 posted on Thursday, January 11, 2007

O.K... just starting to understand Generics

Here is what I want to do...

I have two methods in a NON-GENERIC class that have the following def.

Public Class MyClassObj

private mActiveItem as ??? = Nothing

private mActiveReadOnlyItem as ??? = nothing

Public Sub MyFunction(Of T As CSLA.BusinessBase(Of T))(ByVal activeObj As T)

mActiveItem = activeObj

End Sub

Public Sub MyFunction(Of C As CSLA.ReadOnlyBase(Of C))(ByVal activeObj As C)

mActiveReadOnlyItem = activeObj

End Sub

End Class

 

How do i finish the Module Level definitions (the variables with the ???)?  I want to set the "activeObj" coming in to a module level variable for use later.

I can not seem to figure it out... it appears I want to create a variable of type CSLA.BusinessBase(of CSLA.BusinessBase)... but that does not work.  I am i going about this the wrong way?

any help would be great

ward0093

 

skagen00 replied on Thursday, January 11, 2007

You would either store the variable as an interface (like IEditableBusinessObject) or Core.BusinessBase would probably work too (but really wouldn't be the proper approach).

To use an example I like to use - I may have a generic class called Profile<T>, of which I have Individual : Profile<Individual> and Organization : Profile<Organization>. I can't store something as simply a profile - it's a generic class template. So I put an interface on Profile<T> and expose members of Profile<T> in this interface - let's call it IProfile.

Now, I can store an IProfile - which Individual and Organization implement by nature of descending from Profile. And I can call any members declared on IProfile, such as Id, Name, Status, etc.

ward0093 replied on Thursday, January 11, 2007

but than can you CType the variable back into the Class Object it originally was? I mean say i pass in a "Customer" object (def: Public Class Customer - Inherits CSLA.BusinessBase(of Customer)) and this object is stored in the module variable mActiveItem (def: private mActiveItem as IMyLocalBusinessBase)...

How do I CType(mActiveObject, CLSA.BusinessBase(Of ???))?

ward0093

skagen00 replied on Thursday, January 11, 2007

If something isn't on the interface that I'm storing the variable for - let's say I have an individual stored in my IProfile variable and I want to use some functionality for individuals...

I can test the variable like this: if (myIProfile is Individual) (C#)

And then just:

Individual myIndividual =  (Individual)myIProfile;

myIndividual.Gender = Gender.Male;

I'm sure there's the VB equivalent. Is this what you were asking?

ward0093 replied on Thursday, January 11, 2007

maybe...

I want to CType the interface back into the complete original object

if typeof MyClassObj.activeItem is Library.Customer then

'Do Something

ElseIf typeof MyClassObj.activeReadOnlyItem is Library.CustomerInfo then

'Do Something

end

Brian Criswell replied on Thursday, January 11, 2007

if typeof MyClassObj.activeItem is Library.Customer then

    Dim customer as Library.Customer = DirectCast(MyClassObj.ActiveItem, Library.Customer)

    'Do Something to customer

ElseIf typeof MyClassObj.activeReadOnlyItem is Library.CustomerInfo then

    Dim customer as Library.CustomerInfo = DirectCast(MyClassObj.ActiveItem, Library.CustomerInfo)

    'Do Something to customer

end

ajj3085 replied on Thursday, January 11, 2007

You don't want to do that; something may be wrong with your design if you need to do that typically.

"Depend on high level abstractions, not concrete classes."

xal replied on Thursday, January 11, 2007

I'm still failing to see the point of what you're trying to accomplish with that code. Could you explain a little bit further?
If what you want is to store the "current" bo, the i don't think generics are the way to go, because you're going to be storing objects of different types. Generics won't change the current type once the instance was made, so you can't have a local variable that will change it's type as you store different object types.

So, while you can have generic methods in your class, the variables at the object level comply with what you define at the class level. ie:

Public Class MyGenericClassContainer(Of T)
    Private mField as T
End Class

Once you make an instance of, say, MyGenericClassContainer(Of Integer), all you can store in mField are integers.
I think generic methods are not meant to do what you're trying to do.

Andrés

Copyright (c) Marimer LLC