i am trying to assign one object to other object but it is assigning and showing me this error can any one help me...?
Error1Cannot implicitly convert type 'DMLOperationOnArray.Test2' to 'DMLOperationOnArray.Test'D:\c# Prectice\DMLOperationOnArray\DMLOperationOnArray\Program.cs7017DMLOperationOnArray
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 occured here
Console.ReadKey();
}
}
class Test
{
public void Testmethod()
{
int value = 20;
Console.WriteLine(value);
}
}
class Test2
{
public void Testmethod2()
{
int value = 40;
Console.WriteLine(value);
}
}
}


VulpesRank #55 · 29.2K reputationPosted Jun 17, 2011, 12:07 AM EDT
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:
Ish BandhuRank #1,415 · 1.2K reputationPosted Jun 16, 2011, 10:51 PM EDT
{
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);
}
}
Mamta MRank #268 · 9.4K reputationPosted Jun 16, 2011, 11:22 PM EDT
t= (Test) t2;
Mamta MRank #268 · 9.4K reputationPosted Jun 17, 2011, 12:47 AM EDT
You are right. I had assumed they were related, when in fact they weren't. Thanks for pointing that out.
-Mamta