-2
interface IA<T>
{T Value;}

Class A<T> : IA
{
  T Value;
  public A<T> saveClassA;
}

var A1= new A(123);
var A2 = new A("hihi");
A1.saveClassA = A2; 

ERROR:Unable to cast object of type 'Developer.Test.DProperties1[System.String]' to type 'Developer.Test.DProperties1[System.Int32]'.

I need to store A2 in A1. So in future A1 changed, A2 can be notified. I got a mismatch error. How to do this.....

3
  • Class<int>, cast to Class<string>? do you know how to cast the class? the problem is class<T> and <T>can be anything, <string><bool><>..... Commented Mar 26, 2017 at 16:33
  • Even I changed (public A<T> saveClassA) to (public object saveClassA), I cannot Add (object)A2 to A1. Commented Mar 26, 2017 at 16:35
  • Possible duplicate of Converting string to int using C# Commented Mar 27, 2017 at 6:29

1 Answer 1

0

You may define your classes in the following way:

Class B<T> : IA<T>
{
  T Value;
}

Class A<T1, T2> : IA<T1>
{
  T1 Value;
  public B<T2> saveClassA;
}

var A1= new A<int, string>(123);
var B2 = new B<string>("hihi");
A1.saveClassA = B2;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I will try!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.