John Smith
John Smith

Reputation: 495

C# related on Constructor and Property

I am building some tiny lib, and I have run into a problem. I want to provide a two-way solution, for example:

How can I accomplish this? I am getting exception thrown, because it expects something... Any example that will do is welcomed :) Thanks!

EDIT: I am executing something, initially my code is similar to this one:

 System.IO.DriveInfo d = new System.IO.DriveInfo("C:"); 

I want to achieve with my class the following:

Driver d = new Driver(); 
d.DriverLetter = "C:"; 

And still get the same results, I use ManagementObjectSearch, ManagementObjectCollection and some other System.Management classes.

Upvotes: 2

Views: 207

Answers (4)

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137527

You need to provide both constructors:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Country { get; set; }

    // Paramterless constructor  -  for   new Person();
    // All properties get their default values (string="" and int=0)
    public Person () { }

    // Parameterized constructor -  for   new Person("Joe", 16, "USA");
    public Person (string name, int age, string country)
    {
        Name = name;
        Age = age;
        Country = country;
    }
}

If you define a parameterized constructor, the default parameterless constructor is not included for you. Therefore you need to include it yourself.

From MSDN 10.10.4 Default constructors:

If a class contains no instance constructor declarations, a default instance constructor is automatically provided.

Upvotes: 12

J0e3gan
J0e3gan

Reputation: 8938

Try this:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Country { get; set; }

    public Person()
    {
    }

    public Person(string name, int age, string country)
    {
        Name = name;
        Age = age;
        Country = country;
    }
}

class Test
{
    static void Main(string[] args)
    {
        var person1 = new Person();
        person1.Name = "Joe";
        person1.Age = 2;
        person1.Country = "USA";

        var person2 = new Person("John", 4, "USA");
    }
}

The .NET Framework will implicitly provide a default/parameterless constructor if you don't define a constructor. If you define a parameterized constructor, though, you need to explicitly define a default constructor too.

Upvotes: 1

Soner Gönül
Soner Gönül

Reputation: 98858

You probably missing your Age property type as int or string.

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Country { get; set; }
    public Person()
    {

    }
    public Person(string name, int age, string country)
    {
        this.Name = name;
        this.Age = age;
        this.Country = country;
    }
}
class Program
{
    static void Main(string[] args)
    {
        Person p1 = new Person("Erik", 16, "United States");

        Person p2 = new Person();
        p2.Name = "Erik";
        p2.Age = 16;
        p2.Country = "United States"; 
    }
}

EDIT: Also you need parameterless constructor for also.

Upvotes: 0

John Willemse
John Willemse

Reputation: 6698

You have to define a constructor that takes those three arguments:

public class Person
{
    public Person(string name, string age, string country)
    {
        this.Name = name;
        this.Age = age;
        this.Country = country;
    }
 }

This way, you can assign the values to the properties when the class is constructed. You can have more than one constructor for a class taking different parameters and you can have one constructor call another constructor with : this() syntax:

public class Person
{
    public Person()
        : this(string.Empty, string.Empty, string.Empty)
    {

    }

    public Person(string name, string age, string country)
    {
        this.Name = name;
        this.Age = age;
        this.Country = country;
    }
 }

Here the "empty" constructor will call the other constructor and set all properties to empty strings.

Upvotes: 2

Related Questions