Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

I am getting a error while overloading the a class constructor.

"cannot convert from 'string' to 'System.Collections.Generic.List<string>" //Removed the "<" bcos string is between <> is not shown

code looks like this:
C#
Class Test
{
   Test(string arg)
   {
      //Code
   }

   Test(List<string> args) //Removed the "<" bcos string is between <> is not shown
   {
      //Code
   }
}

public static void main()
{
   Test obj = new Test("ABC");
}



Let me know if you have any solution

Thanks in advance!

Regards
Govardhan
Posted
Updated 2-Dec-13 3:20am
v4
Comments
Karthik_Mahalingam 2-Dec-13 9:11am    
i tried your code, it is working fine..

send me your exact code, what u have tried in ur visual studio..
Karthik_Mahalingam 2-Dec-13 9:48am    
r u getting any error, this is not a question at all ...
Sergey Alexandrovich Kryukov 2-Dec-13 10:17am    
It's not nice to show code which is not your real code. Was that so difficult to use copy/paste? What you show would not even compile. Do you remember C# is case-sensitive? :-) And your constructors are private, so nothing outside class could use them.
—SA

There is no solution. The code you posted, what little there is of it, is correct. The problem comes in somewhere inside the constructor code, NOT with the header itself. The exception message would have told you that, but you didn't read the entire thing. It will tell you the line of code the exception occurs on.

The message is telling you that you're trying to convert a String to a List, which, obviously, won't work.
 
Share this answer
 
In addition Solution 1 and Solution 2.

This is the practical advice. Don't create a constructor with the signature Test(List<string> args)</string>, because it is not flexible enough. You could make it more flexible without any additional effort with one or two slightly different signatures.

Variant #1:
C#
using System.Collections.Generic;

internal Test {
    List list<string> = new List<string>();
    internal Test(params string[] values)  { list.AddRange(values);  }
    //...
}

//...
//uses:

Test myTest1 = new Test("one parameter");
Test myTest2 = new Text("first parameter", "second parameter"); // any number of them

System.Collections.Generic.List<string> list = //...
Test myTestFromList = new Test(list.ToArray());

string[] array = //...
Test myTestFromArray = new Test(array); // only one parameter


Variant #2:
C#
internal Test {
    IList<string> list = new List<string>();
    //can be initialized with any array or collection implementing IList:
    internal Test(System.Collections.Generic.IList<string> list)  { this.list =  list; }
    //...
}

//...
//uses:

Text myTestFromArray = new Test(array); // array declaration is shown in the code sample above
Test myTestFromList = new Test(list); // no need to convert to array, list declaration is shown in the code sample above


You can combine two constructors in one class, of course, but it can create ambiguity where the constructor calls are identical in first and second sample.

—SA
 
Share this answer
 
v3
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConstructorOverloading
{
    class constructorOverloadingExample
    {
        string member1;
        List<string> member2;
        public constructorOverloadingExample(string arg1)
        {
            member1 = arg1;
        }
        public constructorOverloadingExample(List<string> arg1)
        {
            member2 = arg1;
        }
        public void displayData()
        {
            
            //Displaying data updated by any of the constructorOverloadingExample constructor

            Console.WriteLine("member1 = " + member1);


        }


    }


    public class testConstructorOverloading
    {


        public static void Main(string[] args)
        {


            constructorOverloadingExample instance1 = new constructorOverloadingExample("ABC");


            Console.WriteLine("Member values of instance1:");


            instance1.displayData();

            List<string> abc = new List<string>();
            abc.Add("abc");
            abc.Add("xyz");
            constructorOverloadingExample instance2 = new constructorOverloadingExample(abc[0]);


            Console.WriteLine("Member values of instance2:");


            instance2.displayData();



            Console.Read();


        }


    }
}


Try like this.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 2-Dec-13 10:35am    
Not really so bad to get two votes of 1 (I can see right now); I voted 4.
I can see some problems: you have given an excessive access modifier "public" where "internal" will do. Public is for access from a different assemblies, which you don't use. This is a usual mistake. (And OP have shown the code which won't even compile!)
Besides, the example is not compact enough, too many insignificant detail distract from the idea. Naming of members is unacceptable. Never use "member1", "member2"...

One problem never mentioned is inflexible design of constructors. Please see my suggestions in Solution 4.

—SA
Gitanjali Singh 2-Dec-13 14:08pm    
Really appreciate the way of guidance .Will take care of mentioned points .Thanks -SA
There was a issue with generic list conversion.

thanks
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Dec-13 10:12am    
No, there is no such issue. Don't post such content as "solution", it is not, and is considered as the abuse. Use comments or "Improve question".
—SA

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