473,320 Members | 1,744 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.

Generics


Hi all,

I need some help in understanding how to use Generics. I have a class
based on a user control that can be put on any Container at runtime, I
want to be able to call a method on the parent class without knowing the
type of the parent class, can this be done with C#?

I thought this was what Generics was supposed to be all about but I have
spent the whole day Googling and I can't find a way to do this in C#.


An example would be

public class MyClass : UserControl
{

public void SomethingChanged()
{
// this method is bound to an event somewhere

// call the parent class method, I don't care what type the parent
class is just that is has the MyCustomMethod() method.

if (this.Parent.GetType().GetMember("MyCustomMethod") .GetLength(0) > 0)
this.parent.MyCustomMethod(this)
}

}
thanks for all the help
Nov 28 '05 #1
7 3435
Gene,
I thought this was what Generics was supposed to be all about but I have
spent the whole day Googling and I can't find a way to do this in C#.


No I don't think generics will help you here. If you author the
controls you can make them implement an interface where the
MyCustomMethod is defined (or derive from a common base class).
Otherwise you need late binding with Reflection.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 28 '05 #2
"Gene Vital" <no********@msnew.com> a écrit dans le message de news:
%2****************@TK2MSFTNGP09.phx.gbl...

| I need some help in understanding how to use Generics. I have a class
| based on a user control that can be put on any Container at runtime, I
| want to be able to call a method on the parent class without knowing the
| type of the parent class, can this be done with C#?
|
| I thought this was what Generics was supposed to be all about but I have
| spent the whole day Googling and I can't find a way to do this in C#.

What you describe is not generics, it is polymorphism. Generics allows you
to write strictly typed code that applies to many different types, but that
can be written once only. Each time you use a generic type, you are not
deriving from a base class, you are actually using classes that are siblings
to each other. You cannot assign a parameterised instance of a generic class
to a differently parameterised instance. You must usually know the full type
of a generic instance in order to manipulate it, unless you have
methods/properties declared in a non-generic base class and that are
overridden in the generic derivative.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Nov 28 '05 #3
I'm afraid generics won't help you much here, but still you're not far from
the solution.
You can use reflection, which you've used in your example, to do this.

MethodInfo myCustomMethod=this.Parent.GetType().GetMethod("My CustomMethod",new
Type[] { this.GetType() });
if (myCustomMethod!=null)
{
myCustomMethod.Invoke(this.Parent,new object[] { this });
}

Regards,
Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 28 '05 #4
Hi Gene,

No, this is not what Generics are used for. A Generic class is a class that
takes one or more type parameters. When a Generic class is compiled, it uses
the type(s) passed to it, and is strongly typed.

What you're talking about is reflection, which is the discovery of
characteristics of a type. From what I gather from your description, the
only thing known about the container Control for your Control is that it is
a Control, which isn't going to tell you anything about whether or not it
has a member defined that is a Method of the description you're looking for.
Therefore, you need to use reflection to find out.

The code you posted is close to the solution, but makes several fatal
assumptions. First, the return value of Type.GetMember() is an array of
System.Reflection.MemberInfo, but you are treating it as if it is a single
value. All you probably need to know is whether or not the Member Method
exists, and what type to cast the container to in order to properly access
the Member. So, what you need to do is first determine the type:

System.Type t = this.Parent.GetType();

You need to store this in order to use it for casting if the Member exists.
Otherwise, you have to call GetType() twice.

Next, check to see whether the Member exists:

if (t.GetMember("MyCustomMethod")) != null
{
t.InvokeMember(("MyCustomMethod", BindingFlags.InvokeMethod, null,
this.Parent, new Object[] {this});
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
If you push something hard enough,
it will fall over.
- Fudd's First Law of Opposition

"Gene Vital" <no********@msnew.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...

Hi all,

I need some help in understanding how to use Generics. I have a class
based on a user control that can be put on any Container at runtime, I
want to be able to call a method on the parent class without knowing the
type of the parent class, can this be done with C#?

I thought this was what Generics was supposed to be all about but I have
spent the whole day Googling and I can't find a way to do this in C#.


An example would be

public class MyClass : UserControl
{

public void SomethingChanged()
{
// this method is bound to an event somewhere

// call the parent class method, I don't care what type the parent class
is just that is has the MyCustomMethod() method.

if (this.Parent.GetType().GetMember("MyCustomMethod") .GetLength(0) > 0)
this.parent.MyCustomMethod(this)
}

}
thanks for all the help

Nov 28 '05 #5
I don't have vs2005 here to test this, but try this......
public class MyClass <TParent> : UserControl
{
protected SomeEvent(object sender, EventARgs e)
{
TParent parent = this.Parent as TParent;
parent.MyCustomMethod();
}
}

public class MyParent
{
private MyChild<MyParent> child;

public MyCustomMethod() {...}
}

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
"Gene Vital" <no********@msnew.com> wrote in message
news:#3**************@TK2MSFTNGP09.phx.gbl...

Hi all,

I need some help in understanding how to use Generics. I have a class
based on a user control that can be put on any Container at runtime, I
want to be able to call a method on the parent class without knowing the
type of the parent class, can this be done with C#?

I thought this was what Generics was supposed to be all about but I have
spent the whole day Googling and I can't find a way to do this in C#.


An example would be

public class MyClass : UserControl
{

public void SomethingChanged()
{
// this method is bound to an event somewhere

// call the parent class method, I don't care what type the parent
class is just that is has the MyCustomMethod() method.

if (this.Parent.GetType().GetMember("MyCustomMethod") .GetLength(0) > 0)
this.parent.MyCustomMethod(this)
}

}
thanks for all the help

Nov 29 '05 #6
Another solution - what about events? Especially in the UserControl world,
this would seem to be a fairly "natural" way for a child control to indicate
something to it's parent, without the child having to know anything about a:
the parent, or b: the implementation... so if your child had a simple public
event, the container can subscribe and provide it's own implementation
(based on the event args), and all the child has to do is call the event?

Marc

"Gene Vital" <no********@msnew.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...

Hi all,

I need some help in understanding how to use Generics. I have a class
based on a user control that can be put on any Container at runtime, I
want to be able to call a method on the parent class without knowing the
type of the parent class, can this be done with C#?

I thought this was what Generics was supposed to be all about but I have
spent the whole day Googling and I can't find a way to do this in C#.


An example would be

public class MyClass : UserControl
{

public void SomethingChanged()
{
// this method is bound to an event somewhere

// call the parent class method, I don't care what type the parent class
is just that is has the MyCustomMethod() method.

if (this.Parent.GetType().GetMember("MyCustomMethod") .GetLength(0) > 0)
this.parent.MyCustomMethod(this)
}

}
thanks for all the help

Nov 29 '05 #7
You bring up a good point, Marc. Objects should generally "mind their own
business." This means that a child object should generally not be calling
methods in a container. Raising an event is an excellent idea. that way, the
container can handle (or not) the event itself.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
If you push something hard enough,
it will fall over.
- Fudd's First Law of Opposition

"Marc Gravell" <mg******@rm.com> wrote in message
news:eK*************@TK2MSFTNGP15.phx.gbl...
Another solution - what about events? Especially in the UserControl world,
this would seem to be a fairly "natural" way for a child control to
indicate something to it's parent, without the child having to know
anything about a: the parent, or b: the implementation... so if your child
had a simple public event, the container can subscribe and provide it's
own implementation (based on the event args), and all the child has to do
is call the event?

Marc

"Gene Vital" <no********@msnew.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...

Hi all,

I need some help in understanding how to use Generics. I have a class
based on a user control that can be put on any Container at runtime, I
want to be able to call a method on the parent class without knowing the
type of the parent class, can this be done with C#?

I thought this was what Generics was supposed to be all about but I have
spent the whole day Googling and I can't find a way to do this in C#.


An example would be

public class MyClass : UserControl
{

public void SomethingChanged()
{
// this method is bound to an event somewhere

// call the parent class method, I don't care what type the parent class
is just that is has the MyCustomMethod() method.

if (this.Parent.GetType().GetMember("MyCustomMethod") .GetLength(0) > 0)
this.parent.MyCustomMethod(this)
}

}
thanks for all the help


Nov 29 '05 #8

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

Similar topics

27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
2
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
23
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
5
by: anders.forsgren | last post by:
This is a common problem with generics, but I hope someone has found the best way of solving it. I have these classes: "Fruit" which is a baseclass, and "Apple" which is derived. Further I have...
11
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
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...
1
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.