473,372 Members | 856 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,372 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 1447
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 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 cant use generics... Im trying to make a generic collection myCollection<vartype> and...
7
by: Gene Vital | last post by:
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...
2
by: Daniel Billingsley | last post by:
Let's say I have: public class RootClass<T> { public virtual T GetOne() { return default(T); } }
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...
2
by: vcquestions | last post by:
What's the best approch to having a common method that takes a collection ( List<), goes through its elements and deletes them. public ref struct Struct1 { public: Struct1( ); ~Struct1( ); };
1
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... I'm a little perplexed by some of the generics collection stuff. Dictionary<Key, Valuebeing the rough equivalent of Hashtable, I was surprised to find there was no direct way to get to the...
3
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I have some problems with Crystal Reports (Version 10.2, Runtime 2.0). In Section3 I have added a OLE Object (Bitmap). Now when I open the report in my code I would like to set this...
3
by: Anders Borum | last post by:
Hello, I've worked on an API for quite some time and have (on several occasions) tried to introduce generics at the core abstract level of business objects (especially a hierarchical node). The...
2
by: nicholas08 | last post by:
I am new to programming and trying to work on a simple console app. I am making a simple Library Management system where the user/admin can manage members and item. Attaching the menu so it's clear...
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.