return generic object rather strongly-typed business object?

  • Thread starter Thread starter hazz
  • Start date Start date
H

hazz

I would like to call a generic method that will pass back a generic object

private object getBO(int ID)
{return specific_bo;}

rather than returning a specific business object

private specific_BO getBO(int objID)
{return specific_bo;}

so I can do this;

switch(ID) {
case(int)genericID.CodeID1:
BO1 obo = getBO(ID);
break;
case(int)genericID.CodeID2:
BO2 obo = getBO(ID);
break;
case(int)genericID.CodeID3
BO3 obo = getBO(ID);
break;

Does this make sense to use the switch and cast each BO separately at the
caller to so I don't have to have a separate method for each specific
business object?
Thanks, -hazz
 
Hazz,

If you are using .NET 2.0, then I would use generics, like this:

T GetBusinessObject<T>(int id)
{
// Get the object and cast to the return value.
return (T) getBO(id);
}

Hope this helps.
 
Thank you Nicholas. I am not using NET 2.0 but I appreciate your matching up
my requirement with that .NET 2.0 feature. Now I understand the practical
application to generics and their usefulness!
-hazz

Nicholas Paldino said:
Hazz,

If you are using .NET 2.0, then I would use generics, like this:

T GetBusinessObject<T>(int id)
{
// Get the object and cast to the return value.
return (T) getBO(id);
}

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

hazz said:
I would like to call a generic method that will pass back a generic
object

private object getBO(int ID)
{return specific_bo;}

rather than returning a specific business object

private specific_BO getBO(int objID)
{return specific_bo;}

so I can do this;

switch(ID) {
case(int)genericID.CodeID1:
BO1 obo = getBO(ID);
break;
case(int)genericID.CodeID2:
BO2 obo = getBO(ID);
break;
case(int)genericID.CodeID3
BO3 obo = getBO(ID);
break;

Does this make sense to use the switch and cast each BO separately at the
caller to so I don't have to have a separate method for each specific
business object?
Thanks, -hazz
 
I'm very puzzled about what you want to do. Given that code fragment,
the only possible implementation for getBO(int ID) I can imagine is:

private object getBO(int ID)
{
switch(ID) {
case(int)genericID.CodeID1:
return new BO1(ID);
break;
case(int)genericID.CodeID2:
return new BO2(ID);
break;
case(int)genericID.CodeID3
return new BO3(ID);
break;
}

I think you can see how silly it would be to have your code (below) call
that. Further, your code won't even work (you can't define obo as three
different types in the same scope--- and that scope will end (and the
objects be garbage collected) at the end of the switch statement.

If you give us a more realistic example of what you are trying to do, we can
give you a more practical method of doing it.
 
James,
I will admit I was working on a pretty half-baked idea here. I ended up
completely redesigning what I was attempting to do.
I apologize for not having thought through this more and for not having
provided a more complete example.
 

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

Back
Top