A
Andrew Ch. _
Guest
Gentlefolk,
I am trying to create a generic C# class in which I need to compare a stored value against a passed parameter. I am trying to do what seems intuitive to me, but am not having any success. I believe my problem is that I am thinking of C# generic classes as equivalent to C++ templates, but there are subtle differences. Here is my code:
namespace ConsoleApp
{
//public delegate void PubDelegate( object Thing) ;
class Program
{
static void Main(string[] args)
{
Thing<int> A = new Thing<int>( 23 ) ;
Thing<string> B = new Thing<string>( "Hello" ) ;
A.Func( 23 ) ;
B.Func( "Hello" ) ;
}
}
public class Thing<T>
{
public Thing( T Val )
{
Value = Val ;
}
public void Func( T CompVal )
{
if (CompVal == Value) Console.WriteLine("They are equal");
if (CompVal.CompareTo(Value) == 0) Console.WriteLine("They compare as equal");
}
T Value ;
}
}
In my Func method all I want to do is see if the passed value equates to the stored value. As you can see I have tried two ways to do it, but the compiler does not like either. Is there any way do something like this in C# and if so, please can someone tell me what my Func method should look like. If have tried converting the values to objects and not got what I wanted (the string version worked but the int one did not):
public void Func( T CompVal )
{
object ObjCompVal = (object)CompVal ;
object ObjValue = (object)Value ;
if (ObjCompVal == ObjValue) Console.WriteLine("Equal ");
else Console.WriteLine("Not equal");
}
Any help gladly accepted.
Andrew.
Continue reading...
I am trying to create a generic C# class in which I need to compare a stored value against a passed parameter. I am trying to do what seems intuitive to me, but am not having any success. I believe my problem is that I am thinking of C# generic classes as equivalent to C++ templates, but there are subtle differences. Here is my code:
namespace ConsoleApp
{
//public delegate void PubDelegate( object Thing) ;
class Program
{
static void Main(string[] args)
{
Thing<int> A = new Thing<int>( 23 ) ;
Thing<string> B = new Thing<string>( "Hello" ) ;
A.Func( 23 ) ;
B.Func( "Hello" ) ;
}
}
public class Thing<T>
{
public Thing( T Val )
{
Value = Val ;
}
public void Func( T CompVal )
{
if (CompVal == Value) Console.WriteLine("They are equal");
if (CompVal.CompareTo(Value) == 0) Console.WriteLine("They compare as equal");
}
T Value ;
}
}
In my Func method all I want to do is see if the passed value equates to the stored value. As you can see I have tried two ways to do it, but the compiler does not like either. Is there any way do something like this in C# and if so, please can someone tell me what my Func method should look like. If have tried converting the values to objects and not got what I wanted (the string version worked but the int one did not):
public void Func( T CompVal )
{
object ObjCompVal = (object)CompVal ;
object ObjValue = (object)Value ;
if (ObjCompVal == ObjValue) Console.WriteLine("Equal ");
else Console.WriteLine("Not equal");
}
Any help gladly accepted.
Andrew.
Continue reading...