471,348 Members | 1,250 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Generics and member access

Hello, Programmers.

How can I access a member of an object using generics?
I've got the following problem:I Have a class that uses generics like
below:

class ClassName<typename>
{
public void method(typename var)
{
var.Method2(); //I've got an error here.
}
}

How can I Access Method2 from the class typename ???

in VB.Net its easy:

Class ClassName(Of typename)
public sub method(byval var as typename)
Dim obj As Object = var
obj.Method2()
end sub
End Class

Thanks in advance.
Jun 27 '08 #1
4 1371
C# 3 enforces that the method must resolve at compile-time (C# 4 may
change this). Either you need a constraint that provides this (such as
"typename : ISomeInterface" where ISomeInterface defines Method2()),
or you need to use reflection, defeating the purpose of generics. You
could also use a base-class in place of the interface, but interfaces
are generally more versatile (due to single type-inheritance in .NET).

Marc
Jun 27 '08 #2
C# 3 enforces that the method must resolve at compile-time

(and all prior, of course...)
Jun 27 '08 #3
On Jun 10, 1:03 pm, Bruno Neves Pires Silva <brunonpsi...@gmail.com>
wrote:
How can I access a member of an object using generics?
Did you ask the same question recently? I'm sure someone did.
I've got the following problem:I Have a class that uses generics like
below:

class ClassName<typename>
{
public void method(typename var)
{
var.Method2(); //I've got an error here.
}

}
Given that var is a contextual keyword in C# 3, it's not a great
variable name to use. Likewise conventionally "typename" would be T.
Just a suggestion to make it easier for others to understand your
code.

Basically, you need to constrain the type parameter, e.g.

class ClassName<Twhere T : IDisposable
{
public void Method(T item)
{
item.Dispose(); // Fine, because T implements IDisposable
}
}
in VB.Net its easy:
Try doing it with Option Strict On, and I suspect you'll find it
doesn't work.

Jon
Jun 27 '08 #4

"Bruno Neves Pires Silva" <br**********@gmail.comwrote in message
news:d5**********************************@w7g2000h sa.googlegroups.com...
Hello, Programmers.

How can I access a member of an object using generics?
I've got the following problem:I Have a class that uses generics like
below:

class ClassName<typename>
{
public void method(typename var)
{
var.Method2(); //I've got an error here.
}
}

How can I Access Method2 from the class typename ???
One way to do it is by means of a constraint on typename:

class ClassName<typenamewhere typename: myInterface

where myInterface is an interface or base class that declares Method2.
Now the compiler knows that Method2 is a member of typename, and therefore
when you declare var of type typename, it knows that it can call Method2 on
var.
in VB.Net its easy:
[...]
It's easy, but not safe or efficient, only if you have Option Strict
Off. If you set Option Strict to On, it will complain the same way as C#,
and the remedy is the same (add a constraint).

Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by Bernardo Heynemann | last post: by
7 posts views Thread by Gene Vital | last post: by
2 posts views Thread by Daniel Billingsley | last post: by
13 posts views Thread by rkausch | last post: by
2 posts views Thread by vcquestions | last post: by
1 post views Thread by =?Utf-8?B?TWFyaw==?= | last post: by
3 posts views Thread by =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post: by
1 post views Thread by Ronak mishra | last post: by

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.