locked
Generics typecasting RRS feed

  • Question

  • User-1800438376 posted

    Hi experts,

    i have 2 classes with the same name as A but on a different namespace as given below.

    namespace1.A and namespace2.A
    
    The above 2 classes are referred in B as given below.
    
    namespace XYZ = namespace1.A
    namespace PQR = namespace2.A
    
    class B
    {
       method1(XYZ)
       method2(PQR)
    }
    
    is it possible to have a single method inside class B as given below using generics.
    
    class B<T>
    {
      method(T obj){
        if(obj is XYZ) { //do xyz} else { //PQR}
    } }



    Monday, August 22, 2016 5:16 PM

Answers

  • User36583972 posted

    Hi luckyforu2006,

    I modified your option 2, you can refer the code below

    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                Master<namespace1.Organization> name1 = new Master<namespace1.Organization>();
                namespace1.Organization na1org = new namespace1.Organization();
                na1org.Employees = new namespace1.Employee[] { new namespace1.Employee() { Name="001" }, new namespace1.Employee() { Name = "002" } };
                name1.GetEmployees(na1org);
                Console.ReadLine();
            }
        }
    
    
        public  class Master<T>
        {
            public  void GetEmployees(object obj )
            {
                System.Type objtype = obj.GetType();
                //how to write a code by combining both GetEmployees method of option 1 here
                //can we do it live as given below. which i feel is not elegant
                if (objtype == typeof(namespace1.Organization))
                {
                    namespace1.Organization temp = (namespace1.Organization)obj;
    
                    foreach (namespace1.Employee employee in temp.Employees)
                    {
                        //Code
                    }
                }
                else if (objtype == typeof(namespace2.Organization))
                {
                    namespace2.Organization temp = (namespace2.Organization)obj;
    
                    foreach (namespace2.Employee employee in temp.Employees)
                    {
                        //Code
                    }
                }
            }
        }
    }

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 28, 2016 7:28 AM

All replies

  • User753101303 posted

    Hi,

    And what happens when  you try? It seems to me this principle should work but it would defeat the purpose of using (at least only) generics. How does differ what you want to do? Maybe both classes could implement and interface and require that the generic type implements this interface?

    As often understanding the final goal rather than  just how yu think to achieve your unknown goal could help to suggest alternate and possibly better approaches...

    To be on the safe side the fact that both types are named the same way doesn't change anything (from the compiler point of view it is just two unrelated types and aliases likely doesn't play any role here).

    Edit: my current understanding is that you rather want to use overloading; http://csharp.net-tutorials.com/classes/method-overloading/

    You would have two methods named the same way and the compiler would call the correct one depending on the argument type (which is basically what your code does explicitely ie calling some code fragment based on the argument type).

    Monday, August 22, 2016 6:25 PM
  • User36583972 posted

    Hi Imran Khan,

    I have made a sample on my side. The following code for your reference.

    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                B<namespace2.A> bb = new B<namespace2.A>();
                namespace2.A aaaa = new namespace2.A();
                Console.WriteLine(bb.Login(aaaa));
                Console.ReadLine();
            }
        }
    
        public class B<T>
        {
            public string Login(T obj)
            {  
                if (obj is namespace1.A)
                {
                    return "namespace1.A";
                }    
                else if (obj is namespace2.A)
                {
                    return "namespace2.A";
                }
                else
                {
                    return "null";
                }
            }
        }
    
    }
    
    namespace namespace1 
    {
        public class A
        {
            public  string Name { get; set; }
        }
    }
    namespace namespace2
    {
        public class A
        {
            public string Name { get; set; }
        }
    }

    Best Regards,

    Yohann Lu

    Tuesday, August 23, 2016 4:10 AM
  • User753101303 posted

    Or maybe :

       public static class B
        {
            public static string Login(namespace1.A obj)
            {
                return "namespace1.A";
            }
            public static string Login(namespace2.A obj)
            {
                return "namespace2.A";
            }
        }

    depending on what you know about this generic type. If the only thing you are doing is testing the type you could just create a method with the same name with each type and the compiler will just call the right code for you (see the earlier link about "overloading" for details).

    Edit: And it is called using:

     Console.WriteLine(B.Login(new namespace1.A()));
     Console.WriteLine(B.Login(new namespace2.A()));

    Tuesday, August 23, 2016 9:29 AM
  • User-1800438376 posted

    Thanks Yohannu Lu,

    can you please help in fixing below code snippets.

    //structure 
    namespace namespace1 
    {
        public class Organization
        {
            public Employee[] Employees { get; set; }
        }
        public class Employee
        {
            public string Name { get; set; }
        }
    }
    namespace namespace2
    {
        public class Organization
        {
            public Employee[] Employees { get; set; }
        }
        public class Employee
        {
            public string Name { get; set; }
        }
    }
    
    
    //option 1 : without using T
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Master.GetEmployees(namespace1.Organization);
                Master.GetEmployees(namespace2.Organization);
                Console.ReadLine();
            }
        }
    
        internal static class Master 
        {
            internal static void GetEmployees(namespace1.Organization obj)
            {  
                foreach(namespace1.Employee in obj.Employees)
                {
                    //
                }
            }
            internal static void GetEmployees(namespace2.Organization obj)
            {  
                foreach(namespace2.Employee in obj.Employees)
                {
                    //
                }
            }
        }
    
    }
    
    //option 2 : with using T, if its possible...
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                Master<namespace1.Organization>.GetEmployees(namespace1.Organization);
                Master<namespace2.Organization>.GetEmployees(namespace2.Organization);
                Console.ReadLine();
            }
        }
    
        internal static class Master<T>
        {
            internal static void GetEmployees(T obj)
            {
              //how to write a code by combining both GetEmployees method of option 1 here
              
              //can we do it live as given below. which i feel is not elegant
              if(obj is namespace1.Organization)
              {
                foreach(namespace1.Employee employee in namespace1.Organization.Employees)
                {
                  //Code
                }
              }
              else if(obj is namespace2.Organization)
              {
                foreach(namespace2.Employee employee in namespace2.Organization.Employees)
                {
                  //Code
                }
              }
    
              ---OR---
              //would love if below is possible
              foreach(namespace1.Employee employee in obj.Employees)
              {
                 //Code
              }
            }
        }
    }

    Wednesday, August 24, 2016 4:40 PM
  • User36583972 posted

    Hi luckyforu2006,

    I modified your option 2, you can refer the code below

    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                Master<namespace1.Organization> name1 = new Master<namespace1.Organization>();
                namespace1.Organization na1org = new namespace1.Organization();
                na1org.Employees = new namespace1.Employee[] { new namespace1.Employee() { Name="001" }, new namespace1.Employee() { Name = "002" } };
                name1.GetEmployees(na1org);
                Console.ReadLine();
            }
        }
    
    
        public  class Master<T>
        {
            public  void GetEmployees(object obj )
            {
                System.Type objtype = obj.GetType();
                //how to write a code by combining both GetEmployees method of option 1 here
                //can we do it live as given below. which i feel is not elegant
                if (objtype == typeof(namespace1.Organization))
                {
                    namespace1.Organization temp = (namespace1.Organization)obj;
    
                    foreach (namespace1.Employee employee in temp.Employees)
                    {
                        //Code
                    }
                }
                else if (objtype == typeof(namespace2.Organization))
                {
                    namespace2.Organization temp = (namespace2.Organization)obj;
    
                    foreach (namespace2.Employee employee in temp.Employees)
                    {
                        //Code
                    }
                }
            }
        }
    }

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 28, 2016 7:28 AM