Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
I want to write a function whose arguments are generic (so that i can pass ny type like string or defined struct. I wore like below


C#
public struct Book
{
    public decimal price;
    public string title;
    public string author;
}


public int GenericFun(                                            Object i_objCondition,
                                            List<Object> io_objListOutValue)
{
// Do some operation
}

int main()
{
    List<string>    Obj1      = null;
            string          s1= ""s1;
            string          s2= "s2";

            Obj1.Add(strLoginName);
            Obj1.Add(strpassword);
            Obj1.Add(strdomain);

    string sCondition = "";
    GenericFun(sCondition, Obj1);

    List<Book> objBook = null;
    Book Book1;
    Book Book2;
    GenericFun(sCondition, objBook);
}


But I';m getting error "Argument 2: cannot convert from System.Collections.Generic.List<string> to System.Collections.Generic.List<object>
Posted
Updated 29-Apr-12 20:29pm
v2

A similar problem is discussed here
http://stackoverflow.com/questions/6557/in-c-why-cant-a-liststring-object-be-stored-in-a-listobject-variable[^]

To convert List<string> to List<object>, each element of List<string> is to be cast to object type as below
C#
List<string> Obj1 = new List<string>();
Obj1.Add(strLoginName);
Obj1.Add(strpassword);
Obj1.Add(strdomain);
List<object> Obj1AsObjects = Obj1.Cast<object>().ToList();
string scondition = "";
GenericFun(scondition, Obj1AsObjects);


But then in GenericFun what operation can be done on the parameters. Because, Object type has only the basic methods, which may not be much useful to perform some practical calculations or other manipulations. Hence, it is preferable create a heirarchy using classes and interfaces and then pass the parameter type as the base class of this heirarchy.
 
Share this answer
 
v2
Comments
vidiking 30-Apr-12 4:33am    
Thank you very much. Its working
VJ Reddy 30-Apr-12 4:54am    
You're welcome and thank you accepting the solution.
You can consider to vote if the solution is helpful.
C#
//In GenericFun() method call, use casting as below
GenericFun((Object)sCondition, (List<Object>)objBook);
</string>
 
Share this answer
 
v3
Comments
vidiking 30-Apr-12 2:42am    
GenericFun(sCondition, (List<Object>)objBook);
is giving below error.
error CS0030: Cannot convert type System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<object>'
summon_19 30-Apr-12 2:48am    
was your first problem with the first method call or the second method call?
vidiking 30-Apr-12 2:56am    
Both. :(
A penality in using these generics is boxing/unboxing. I guess, you have to convert List<string> to List<object> and then pass on to the method.
You can try using Obj1.CovertAll(...) method for doing this
 
Share this answer
 
Comments
vidiking 30-Apr-12 2:54am    
Thank u Lakamraju. I'm new to C#. Can u plz tell me how to do?

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