473,396 Members | 2,129 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,396 software developers and data experts.

Generic class

Hi all,
how can create a generic class tha has a "value" property that can be int or
string?

Thanks a lot.
--
Luigi

Nov 3 '08 #1
8 1929
Luigi wrote:
Hi all,
how can create a generic class tha has a "value" property that can be int or
string?

Thanks a lot.

public class test
{
public object a_param
{
get{;}
set{;}
}
}
Nov 3 '08 #2
"Steph" wrote:
public class test
{
public object a_param
{
get{;}
set{;}
}
}
Is the same writing?

public class Test<T>
{
public T value;
}
Nov 3 '08 #3
Luigi wrote:
"Steph" wrote:
>public class test
{
public object a_param
{
get{;}
set{;}
}
}

Is the same writing?

public class Test<T>
{
public T value;
}
why not ! all depend of your use !
your can use one of this two ways.

the code class goto define the good way
Nov 3 '08 #4
That doesn't use generics; while it will work (typos aside), it isn't
exactly going to help much.
Nov 3 '08 #5
You can't do much to restrict the available types to int or string, but
if you are happy to allow any type:

public class Foo<T{
public T Value {get;set;}
}

or in C# 2.0:

public class Foo<T{
private T value;
public T Value {
get {return this.value;}
set {this.value = value;}
}

The only way to disallow other types would be to use a type inintializer
- but it probably isn't worth it. There are such things as generic
constraints, but none that would fit here.

Marc
Nov 3 '08 #6
"Marc Gravell" wrote:
You can't do much to restrict the available types to int or string, but
if you are happy to allow any type:

public class Foo<T{
public T Value {get;set;}
}

or in C# 2.0:

public class Foo<T{
private T value;
public T Value {
get {return this.value;}
set {this.value = value;}
}

The only way to disallow other types would be to use a type inintializer
- but it probably isn't worth it. There are such things as generic
constraints, but none that would fit here.
Thank you Marc, this sounds fine.

Luigi
Nov 3 '08 #7
Luigi, please see your previous post about this. It is not an implementation
using generics but should fit your requirements.
--
Stanimir Stoyanov
http://stoyanoff.info

"Luigi" <ci****************@inwind.itwrote in message
news:59**********************************@microsof t.com...
Hi all,
how can create a generic class tha has a "value" property that can be int
or
string?

Thanks a lot.
--
Luigi
Nov 3 '08 #8
"Marc Gravell" <ma**********@gmail.comwrote in message
news:u9**************@TK2MSFTNGP03.phx.gbl...
You can't do much to restrict the available types to int or string, but if
you are happy to allow any type:

public class Foo<T{
public T Value {get;set;}
}

or in C# 2.0:

public class Foo<T{
private T value;
public T Value {
get {return this.value;}
set {this.value = value;}
}

The only way to disallow other types would be to use a type inintializer -
but it probably isn't worth it. There are such things as generic
constraints, but none that would fit here.
I've used IConvertible as a constraint. Not as specific as int and string
but it does narrow things down considerably without excluding either of
those two.

--
Anthony Jones - MVP ASP/ASP.NET

Nov 4 '08 #9

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

Similar topics

8
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; ...
1
by: Arthur Dent | last post by:
Hi all... Heres what im looking to do.... I have a Generic class i wrote. Now, on another class, i want to add a method which can take in an object of my generic class... but the catch is, i want...
25
by: Lars | last post by:
Hi, I have a base class holding a generic list that needs to be accessed by both the base class and its subclasses. What is the best solution to this? I am fairly new to generics, but I am...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
0
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting...
4
by: Andrew Ducker | last post by:
I have a collection of classes descending from a single root class (let's call it RootClass). They all currently have a property of Logical, of type Logical. However they actually return a...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
3
by: Seth Gecko | last post by:
Hi I am working with generic lists of various objects and a control dealing with these lists. For instance: A parent form holds: dim Walls as List(Of wall) dim Segments as List(Of segment) ...
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...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.