0
Mamta,
A cast won't work either as there is no relationship between Test and Test2.
However, if Test2 were to inherit from Test, then it would work as it stands:
using System;
namespace DMLOperationOnArray
{
class Program
{
static void Main(string[] args)
{
Test t = new Test();
t.Testmethod();
Console.Write("\n\n\n");
Test2 t2 = new Test2();
t2.Testmethod2();
t = t2; // error no longer occurs here
Console.ReadKey();
}
}
class Test
{
public void Testmethod()
{
int value = 20;
Console.WriteLine(value);
}
}
class Test2 : Test // now inherits from Test
{
public void Testmethod2()
{
int value = 40;
Console.WriteLine(value);
}
}
}
Accepted 0
Vulpes,
You are right. I had assumed they were related, when in fact they weren't. Thanks for pointing that out.
-Mamta
0
Both are objects of two different types. Test is one type and Test2 is another type. You cannot directly assign objects of Test to Test2. You need to use a cast as follows:
t= (Test) t2;
0
class Program
{
static void Main(string[] args)
{
xyz t = new Test();
t.Testmethod();
Console.Write("\n\n\n");
xyz t2 = new Test2();
t2.Testmethod();
t = t2; // error occured here
Console.ReadKey();
}
}
//create a base class with virtual method
class xyz {
public virtual void Testmethod()
{
int value = 20; Console.WriteLine(value);
}
}
//inherit base class in both class
class Test:xyz
{
public override void Testmethod()
{
int value = 20; Console.WriteLine(value);
}
}
class Test2:xyz
{
public override void Testmethod()
{
int value = 40; Console.WriteLine(value);
}
}