Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
I want to call a method on two separate occasions. The two occasions pass two different types parameters, they differ by datatype. How do i achieve this without overloading.

This is my code

XML
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DifferentDatatypeOfMethods
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> intlist = new List<int>();
            intlist.Add(2);
            intlist.Add(3);
            intlist.Add(7);

            List<string> stringlist = new List<string>();
            stringlist.Add("A");
            stringlist.Add("B");
            stringlist.Add("C");

            Hit(intlist);
            Hit(stringlist);



            Console.Read();
        }

        public static void Hit(List<object> a) //doesn't work
        {
            Console.WriteLine(a[0]);
        }
    }
}
Posted

Read here about variable data type in C# (it's there since Visual Studio 2008/C# 3.0)...
http://msdn.microsoft.com/en-us/library/bb383973(v=vs.120).aspx[^]
 
Share this answer
 
Here's a start:
C#
public static void Hit(IList a)
{
    Console.WriteLine(a[0]);
}

Both of the types of Lists you're using implement the IList interface.
Without seeing what else you are doing I can't be sure, but using generics[^] looks like it may be a good solution.
 
Share this answer
 
The easy way:
C#
private void TestHit()
{
    var intlist = new List<int> {2, 3, 7};
    var stringlist = new List<string> {"A", "B", "C"};

    Hit(intlist);
    Hit(stringlist);
}

private void Hit<T1>(List<T1> theList)
{
    if (theList == null) throw new ArgumentNullException("theList");

    if (theList.Count == 0) throw new ArgumentOutOfRangeException("theList");

    Console.WriteLine(theList[0]);

}
The method 'Hit declares it uses one generic parameter by putting the place-holder name "T1" in angle-brackets after the method name. In the parameter list a generic List of whatever 'T1 will become at run-time, as the method is invoked, is defined. Using "T1" is a common convention, but you could use any non-reserved name for it; if you wanted to write:
C#
private void Hit<TypeToBeDetermined>(List<TypeToBeDetermined> theList);
That would be just fine, but ... verbose, indeed.
 
Share this answer
 
v2
Hi you can play like this...

C#
static void Main(string[] args)
        {
            List<int> intlist = new List<int>();
            intlist.Add(2);
            intlist.Add(3);
            intlist.Add(7);

            List<string> stringlist = new List<string>();
            stringlist.Add("A");
            stringlist.Add("B");
            stringlist.Add("C");

            Hit(intlist);
            Hit(stringlist);
            Console.Read();
        }

        public static void Hit(object a) //Its works!!
        {
            Type type = a.GetType();

            if (type == typeof(List<int>))
            {
                List<int> aa = a as List<int>;
                Console.WriteLine(aa[0]);
            }
            if (type == typeof(List<string>))
            {
                List<string> aa = a as List<string>;
                Console.WriteLine(aa[0]);
            }
        }
 
Share this answer
 
Comments
[no name] 20-Mar-14 9:05am    
Great!! Nice solution
Divakar Raj M 20-Mar-14 9:11am    
Works mate.. Awesome, thanks :)
Is there a way i can optimize this code so that I do the Console.WriteLine(aa[0]) only once ?
Tejas Vaishnav 20-Mar-14 9:22am    
I have tried, but can't get solution right now... I will tried it and if i got, i will update my answer...
Divakar Raj M 20-Mar-14 9:11am    
@Tejas_Vaishnav
I'm trying this.. However, am getting errors
Type type = a.GetType();
List<object> aa = null;

if (type == typeof(List [<]int[>]))
{
aa = a as List[<]int[>];
}
else if (type == typeof(List[<]string[>]))
{
aa = a as List[<]string[>];
}

foreach (var item in aa)
Console.WriteLine(aa[item]);
Solved it..

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DifferentDatatypeOfMethods
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> intlist = new List<int>();
            intlist.Add(2);
            intlist.Add(3);
            intlist.Add(7);

            List<string> stringlist = new List<string>();
            stringlist.Add("A");
            stringlist.Add("B");
            stringlist.Add("C");

            int user = Convert.ToInt32(Console.ReadLine());
            if(user > 5)
                Hit(intlist);
            else
                Hit(stringlist);
            
            Console.Read();
        }

        public static void Hit(object a)
        {

            List<object> intlist = new List<object>();

            List<int> int1 = null;
            List<string> string1 = null;
            int1 = a as List<int>;
            string1 = a as List<string>;

            if(int1 == null)
                foreach(var item in string1)
                Console.WriteLine(item);
            else
                foreach (var item in int1)
                    Console.WriteLine(item);
        }
       
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900