4
Answers

Problem in Assigning one object to other object

Smart    Lucky

Smart Lucky

13y
1.6k
1
Hi
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);
        }
    }

}
Answers (4)
0
Vulpes

Vulpes

NA 96k 2.6m 13y
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
Mamta M

Mamta M

180 10.7k 2.8m 13y
Vulpes,

You are right. I had assumed they were related, when in fact they weren't. Thanks for pointing that out.

-Mamta
0
Mamta M

Mamta M

180 10.7k 2.8m 13y
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
Ish Bandhu

Ish Bandhu

NA 1.4k 725.2k 13y
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);
  }
}