Click here to Skip to main content
15,908,274 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# Question with Generics Pin
Christian Graus21-Feb-07 16:02
protectorChristian Graus21-Feb-07 16:02 
GeneralRe: C# Question with Generics Pin
mike montagne26-Feb-07 14:53
mike montagne26-Feb-07 14:53 
GeneralRe: C# Question with Generics Pin
malharone27-Feb-07 11:13
malharone27-Feb-07 11:13 
GeneralRe: C# Question with Generics Pin
mike montagne27-Feb-07 13:36
mike montagne27-Feb-07 13:36 
GeneralRe: C# Question with Generics Pin
malharone27-Feb-07 13:48
malharone27-Feb-07 13:48 
GeneralRe: C# Question with Generics Pin
mike montagne27-Feb-07 13:47
mike montagne27-Feb-07 13:47 
GeneralRe: C# Question with Generics Pin
malharone27-Feb-07 14:01
malharone27-Feb-07 14:01 
GeneralRe: C# Question with Generics Pin
mike montagne27-Feb-07 19:24
mike montagne27-Feb-07 19:24 
malharone wrote:
I couldnt agree with you more on this point. But there are cases when from you non-generic class, you want to call a generic method and all you want do is to pass the "current type" and not any other type. In most cases, you will hard-code the class name, but if you're working in the base class, you would not know the type of the deriving class.


I think there's a conceptual problem here. First of all, you can overload your method if you need to process a handful of known classes, or you can simply let descendants override your method (if they need to), and whatever calls the method ends up firing, being as they belong to the instance you (or whoever) calls them on, this ends up firing the method implementation of the closest descendant to the class it's called on (which may be the instance class itself). As your class design is a matter of writing these methods *for each class* (if necessary to override them), this *is* the right method (whether overloads or overrides are necessary or not -- they may very well not be).

Now, if you need to identify the class otherwise (which isn't evident from your example), you can also pass your own identifier argument. It doesn't even have to be a real class -- it could be an enumerated type... anything, even an integer. C++ guys do this all the time to eliminate dynamic casting, and it's perfectly sound if you cover your bases... and faster than greased things unmentionable in C# forums.

Reflection will also work to determine the class (although I have to admit I'm a bit raw with this with C#, as it's something I'll be getting to in the near future). I know for instance I have substantial notes and collected material on the Activator class, which I intend to use to create instances of unknown classes, which certainly is an intersecting area.

-----

Now, I also have another thought. You have a mixture of generics and non-generics interacting. Something you wrote earlier also tells me you are flexible -- you could write this as all non-generic then?

If you can, that's a good way to work out the problem. Get it working without generics. This will tell you exactly how to build generics into it, if that is a good thing to do.

It probably is a good thing to do if you have to do any casting, because it can eliminate casting altogether, and provide wholy type safe operation with minimal code.

I really can't see what the problem is. I look at your question, and you say you have to pass the type, and I ask myself, is he trying to call a static method without an instance (which is a place where the method doesn't know what type it belongs to, because there is no inherent this or instance pointer). Otherwise, a method always knows what type it belongs to, and doesn't need to reference the type. So unless a method has to process a further type which hasn't been cast for processing, I have yet to see in your question why you can't get the job done one of these ways.

Actually, you say:

malharone wrote:
The only workaround for this limiation, is to make the method from your base class abstract and let the deriving classes implement the call.


So we are in complete agreement. Why then can't you let that method call preciptate to the descendant, which, if it had needed to, has revised the method to handle itself however necessary? That's just general practice.

Sigh | :sigh:
GeneralRe: C# Question with Generics Pin
mike montagne28-Feb-07 6:25
mike montagne28-Feb-07 6:25 
GeneralRe: C# Question with Generics Pin
mike montagne28-Feb-07 7:06
mike montagne28-Feb-07 7:06 
GeneralRe: C# Question with Generics Pin
mike montagne28-Feb-07 7:09
mike montagne28-Feb-07 7:09 
GeneralRe: C# Question with Generics Pin
mike montagne28-Feb-07 8:14
mike montagne28-Feb-07 8:14 
AnswerRe: C# Question with Generics Pin
Colin Angus Mackay21-Feb-07 22:09
Colin Angus Mackay21-Feb-07 22:09 
GeneralRe: C# Question with Generics Pin
malharone22-Feb-07 7:03
malharone22-Feb-07 7:03 
AnswerRe: C# Question with Generics Pin
darkelv22-Feb-07 0:42
darkelv22-Feb-07 0:42 
GeneralRe: C# Question with Generics Pin
malharone22-Feb-07 7:49
malharone22-Feb-07 7:49 
GeneralRe: C# Question with Generics Pin
malharone27-Feb-07 11:13
malharone27-Feb-07 11:13 
QuestionStart reading at the end of a file? Pin
TheJudeDude21-Feb-07 14:05
TheJudeDude21-Feb-07 14:05 
AnswerRe: Start reading at the end of a file? Pin
Shajeel21-Feb-07 18:54
Shajeel21-Feb-07 18:54 
GeneralRe: Start reading at the end of a file? Pin
TheJudeDude22-Feb-07 3:31
TheJudeDude22-Feb-07 3:31 
GeneralRe: Start reading at the end of a file? Pin
Luc Pattyn22-Feb-07 8:08
sitebuilderLuc Pattyn22-Feb-07 8:08 
QuestionISerializable.GetObjectData Method Example Pin
Gywox21-Feb-07 11:53
Gywox21-Feb-07 11:53 
AnswerThats a lot of code Pin
Ennis Ray Lynch, Jr.21-Feb-07 12:48
Ennis Ray Lynch, Jr.21-Feb-07 12:48 
GeneralRe: Thats a lot of code Pin
Gywox21-Feb-07 23:23
Gywox21-Feb-07 23:23 
GeneralRe: Thats a lot of code Pin
Ennis Ray Lynch, Jr.22-Feb-07 2:45
Ennis Ray Lynch, Jr.22-Feb-07 2:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.