Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I am having a class with parameterized constructor e.g
C#
public class Company
{
  private string _name;
  public Company(string name)
  {
    this._name = name;
  }
} 

now i am calling this Company Class on my page
C#
public string CompanyDetails(Company objcompany)
{
  //Some Logic
}

But when i pass Company class to CompanyDetails method its throwing exception "no parameter less constructor defined for this object", which is obvious i believe this is because i have parameterized constructor .
How to solve this prob? am i missing something or its not the right way to pass class?

Thanks for guiding :)
Posted
Updated 30-Jul-15 1:15am
v4
Comments
stibee 30-Jul-15 6:35am    
Where dou you create the Company instance? Pls. show code
CPallini 30-Jul-15 6:42am    
Without seeing how you actually create the objcompany object how could we help?

It seems there is some synctax error
Your class name is Company where as you mentioned constructor saying CompanyName

Refer the article An Intro to Constructors in C#[^]

Replace the code with the below
C#
public class Company
{
  private string _name;
  public Company()
  {

  }
  public Company(string name)
  {
    this._name = name;
  }
} 
 
Share this answer
 
v2
Comments
Schatak 30-Jul-15 7:15am    
Yeah that is typo mistake, i corrected into question.
i created a constructor without parameter( as in ur solution) and it worked for me.

Thanks
Depends on how you "pass Company class to CompanyDetails method".

Because you have declared a constructor with a parameter, the system no longer generates a parameterless constructor for you automatically.
So if you try to create a new instance of your Company class without passing it a string it looks for a parameterless version and reports an error.

There are a couple of ways round this:
1) If the name is compulsory, then leave it as it is, and provide a new when you create the new instance.
2) Convert your string to an optional parameter:
C#
public CompanyName(string name = "Anonymous")
{
  this._name = name;
}

3) Write a parameterless constructor that sets a default string value.
 
Share this answer
 
v2
Comments
Schatak 30-Jul-15 7:20am    
1. It did not work for me bad luck.
2. Optional parameter didn't work for me either , again bad luck :(
3. constructor without parameter and it worked for me.. :)

Thanks for your help:)
I tried your logic its just work fine...

C#
namespace ConsoleApplication3
{


    public class Company
    {
    private string _name;
   
//BEFORE
// public CompanyName(string name)
// {
//  this._name = name;
// }
    public Company(string name)
    {
    this._name = name;
    }
} 


    class Program
    {
        public string CompanyDetails(Company objcompany)
          {
            //Some Logic
         return "hello";
         }
        static void Main(string[] args)
        {
            Company ob = new Company("sasank");
            Program obj = new Program();
            //Passing Company objects to CompanyDetails method
            Console.WriteLine(obj.CompanyDetails(ob));
          // o/p  hello
        }
    }
}
 
Share this answer
 
Comments
Schatak 30-Jul-15 7:21am    
Thanks for your efforts :)

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