473,408 Members | 1,749 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,408 software developers and data experts.

Error in Generic method: 'T' does not contain a definition for...

When I call the Generic method ShowName in the following code, I get the
error message "'T' does not contain a definition for 'Name'." Does anyone
know why? Thanks in advance. Jay.

//------------------------------------
using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
class GenericsTest
{
static void Main(string[] args)
{
Person person = new Person();
person.Name = "John";
ShowName<Person>(person);
}

static void ShowName<T>(T someObject)
{
Console.WriteLine(someObject.Name);
}
}
public class Person
{
public string Name = "";
}
}
//-----------------------------------
May 17 '07 #1
2 13353
Generics in C# are not like C++, in that the type T is not resolved at
compile-time in order to determine whether or not a class has certain
members. Because of this, you will have to use reflection to call the
member "Name", or you can use a constraint, defining an interface or base
class which exposes the member, like so:

public interface IPerson
{
string Name {get; set;}
}

public class Person : IPerson
{}

static void ShowName<T>(T someObject) where T : IPerson
{
Console.WriteLine(someObject.Name);
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"J Miro" <jm***@yahoo.comwrote in message
news:46**********************@roadrunner.com...
When I call the Generic method ShowName in the following code, I get the
error message "'T' does not contain a definition for 'Name'." Does anyone
know why? Thanks in advance. Jay.

//------------------------------------
using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
class GenericsTest
{
static void Main(string[] args)
{
Person person = new Person();
person.Name = "John";
ShowName<Person>(person);
}

static void ShowName<T>(T someObject)
{
Console.WriteLine(someObject.Name);
}
}
public class Person
{
public string Name = "";
}
}
//-----------------------------------

May 17 '07 #2
Thanks so much Nicholas for your prompt response.

Jay.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:6B**********************************@microsof t.com...
Generics in C# are not like C++, in that the type T is not resolved at
compile-time in order to determine whether or not a class has certain
members. Because of this, you will have to use reflection to call the
member "Name", or you can use a constraint, defining an interface or base
class which exposes the member, like so:

public interface IPerson
{
string Name {get; set;}
}

public class Person : IPerson
{}

static void ShowName<T>(T someObject) where T : IPerson
{
Console.WriteLine(someObject.Name);
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"J Miro" <jm***@yahoo.comwrote in message
news:46**********************@roadrunner.com...
>When I call the Generic method ShowName in the following code, I get the
error message "'T' does not contain a definition for 'Name'." Does anyone
know why? Thanks in advance. Jay.

//------------------------------------
using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
class GenericsTest
{
static void Main(string[] args)
{
Person person = new Person();
person.Name = "John";
ShowName<Person>(person);
}

static void ShowName<T>(T someObject)
{
Console.WriteLine(someObject.Name);
}
}
public class Person
{
public string Name = "";
}
}
//-----------------------------------


May 17 '07 #3

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

Similar topics

4
by: Michael Kennedy [UB] | last post by:
Hi Everyone, I have this multithreaded C# windows forms application which does a lot of image processing. Occasionally, I get the following error: A generic error occurred in GDI+....
5
by: Metaman | last post by:
I'm trying to write a generic method to generate Hashcodes but am having some problems with (generic) collections. Here is the code of my method: public static int GetHashCode(object input) {...
33
by: Martin Jørgensen | last post by:
Hi, In continuation of the thread I made "perhaps a stack problem? Long calculations - strange error?", I think I now got a "stable" error, meaning that the error always seem to come here now...
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
1
by: rcoco | last post by:
Hi, I'm having this error, 'Bsc_Test1.src.controls.cont' does not contain a definition for 'ChartTitle' 'Bsc_Test1.src.controls.cont' does not contain a definition for 'gcolors'...
5
by: Ethan Strauss | last post by:
Hi, I have just started using Generic Collections for .Net 2.0 and so far they are working very nicely for me. But, I do have a couple of questions. If I have a Generic collection which has a type...
4
by: shapper | last post by:
Hello, I have 3 tables: Tags TagID, Name Files FileID, Name, Url FilesTags FileID, TagID FilesTags relates each file with one or more tags
0
by: asmx126453 | last post by:
Hey guys i have a question about those 2 funtions the error occures in 2 files my DataConnection.asmx.cs and Funtions.cs error 1 is this il also paste a part of the code were the error is ...
2
by: Andrus | last post by:
Winforms UI assembly has static FormManager.FormCreator method which creates forms taking entity as parameter. I need to pass this method to business objects in business assembly so that business...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.