its not bug, Generics donot allow + Sign.
2009/12/28 Maddy <[email protected]>
> Dear folks,
>
> As i am working in C# Generics i found this little bit Strange
>
> My code goes like this
> Consider this as my Business Object
>
> Public Class *BO*<type1,type2,type3>
> {
> private type1 Variable1;
> private type2 Variable2;
> private type3 Variable3;
>
> public type1 val1
> {
> set { Variable1 = value; }
> get { return Variable1; }
> }
> public type2 val2
> {
> set { Variable2 = value; }
> get { return Variable2; }
> }
> public type3 val3
> {
> set { Variable3 = value; }
> get { return Variable3; }
> }
>
> }
>
> and Business Access Layer
>
> Public Class *BAL:BO*<Object,Object,Object>//Because i don't want to
> confine my self to work with particular Data type for this
> //Class so i declared it as Object where if i can pass string,datetime,
> int,etc and It ll do Boxing(data type to Object)
> {
>
> // take a look at this function as i am declaring a Function to Perform
> "+" operation(for string -> Concatenation and for integer -> Sum)
> public T getValue<T>()
> {
> T temp1 = (T)val1; // I am Type Casting my Object to this Type
> T temp2 = (T)val2; // I am Type Casting my Object to this Type
> T temp3 = temp1 + temp2; // I am Adding the values
> return (temp3); // And returning with the Same Data type "T"
> }
> }
>
> Declaring in aspx.cs as
>
> obj.val1 = TextBox1.Text;
> obj.val2 = TextBox2.Text;
>
> Label1.Text = obj.getValue<string>(); // Here i am passing the generic data
> type as String so the function will perform Concatenation
>
> *ERROR: Operator '+' cannot be applied to operands of type 'T' and 'T'*
>
> The Error shows (As of my Understanding) that T is Not a Valid Data type
> TO PERFORM "+"
>
> Clarifications required if possible:
> 1. Then Why the Data type T can be Type casted from an Object (T temp1 =
> (T)val1;)
> 2. *Label1.Text *should assign to string type, as i am passing (*Label1.Text
> = obj.getValue<int>();)* so passing type and the return type is recognized
> as String so didn't throw any error but why it cant able to perform
> Concatenation
>
> *Is that A BUG From Microsoft or From My side ????*
>
>
>
> --
> regards
> Sarvesh
>
--
Cheers,
Raj
http://rajmittal.blogspot.com
Sent from Chandigarh, PB, India