Question

I'm relatively new to programming and I've just gotten into classes. The problem is, my compiler is returning

"Error 1 'OlioohjelmoinninPerusteet.Blogimerkinta' does not contain a constructor that takes 1 arguments"

I have no idea how to fix this, here's my code.

using System;
namespace OlioohjelmoinninPerusteet
{
    class Blogimerkinta
    {
        string teksti;
        string[] avainsanat;

        public string Teksti { get; set; }
        public string[] Avainsanat { get; set; }
    }

    class Ohjelma
    {
        static void Main()
        {
            string otsikko = "Otsikko";
            Blogimerkinta merkinta = new Blogimerkinta(otsikko);
            merkinta.Teksti = "Teksti";
            merkinta.Avainsanat = new string[] { "Eka", "Toka" };
        }
    }
}
Was it helpful?

Solution

By default, if you do not specify a constructor it is the same as below

If you do not provide a constructor for your object, C# will create one by default that instantiates the object and sets member variables to the default values as listed in Default Values Table (C# Reference).

Using Constructors (C# Programming Guide)

A constructor that takes no parameters is called a default constructor. Default constructors are invoked whenever an object is instantiated by using the new operator and no arguments are provided to new.

Unless the class is static, classes without constructors are given a public default constructor by the C# compiler in order to enable class instantiation.

class Blogimerkinta
{
    string teksti;
    string[] avainsanat;

    public string Teksti { get; set; }
    public string[] Avainsanat { get; set; }
    public Blogimerkinta()
    {
    }
}

You are calling a constructor that was not specified, namely

    public Blogimerkinta(string TADA)
    {
    }

OTHER TIPS

You are newing up an object and passing in a parameter for the constructor:

Blogimerkinta merkinta = new Blogimerkinta(otsikko);

this object does not have a constructor that takes any arguments, by default you can use the default constructor all c# objects get with no parameters:

Blogimerkinta merkinta = new Blogimerkinta();

or create a constructor that takes a string argument:

public Blogimerkinta(string arg) 
{
    //use argument for something
}

You need to add a constructor for your class like this:

class Blogimerkinta
{
    string teksti;
    string[] avainsanat;

    public string Teksti { get; set; }
    public string[] Avainsanat { get; set; }

    public Blogimerkinta(string otsikko)
    {
        this.teksti = otsikko;
    }
}

C# creates a default constructor if you don't define one (which you didn't), and the default constructor has no arguments and doesn't do anything. It's equivalent to this:

public Blogimerkinta()
{
}

In order to use a constructor which does take an argument (and I'm assuming internally set a property on the object to the value of that argument) you'll need to define one. Something like this:

public Blogimerkinta(string teksti)
{
    this.Teksti = teksti;
}

(Or setting whatever value you want to set internally. Currently your class has only one property of type string but the naming makes it very unclear if that's what you're trying to set.)

Well, you are trying to create an instance of Blogimerkinta and passing a string parameter to the constructor.But, you don't have any constructor on your class that takes a string parameter.Either create a constructor or use default parameterless constructor to create your instance:

Blogimerkinta merkinta = new Blogimerkinta();

Your Blogimerkinta does not have a constructor defined for it. During compilation there will be a default one generated that takes no arguments. If you want your class to take an argument during instantiation, make a contructor that takes an argument.

 class Blogimerkinta
    {
        public Blogimerkinta(string foo){
         //do something with foo
        }
        string teksti;
        string[] avainsanat;

        public string Teksti { get; set; }
        public string[] Avainsanat { get; set; }
    }

This line calls constructor with one parameter.

Blogimerkinta merkinta = new Blogimerkinta(otsikko);

To fix this you have to create one.

class Blogimerkinta
{
    string teksti;
    string[] avainsanat;

    public Blogimerkinta(){} // This is parameterless constructor
    public Blogimerkinta(string input)
    {
        if(input != null)
            teksti = input; //You initialize class fields here or do other stuff
    }
    public string Teksti { get; set; }
    public string[] Avainsanat { get; set; }
}

As you can see in Main(), you are passing a string value, so that constructor must take a string value, to initialize its field/fields with that value (possibly more values)

Check the msdn, since they explain it quite good.

http://msdn.microsoft.com/en-us/library/ace5hbzh.aspx

A class has 1 or more constructors. If you do not provide one, the compiler will create an empty one: A constructor is called when you say 'new Blogimerkinta()' You are calling a constructor that takes an argument: new Blogimerkinta(otsikko) but you did not define a constructor.

You need to define this constructor:

public Blogimerkinta(string s) {
    teksti = s; //I don't know which field you want to assign, just an example
}

(So it looks a method, but without a return type and it has the same name as the class)

If you do not define a constructor, you can only call 'new Blogimerkinta()'

Here is a generic fix:

class Blogimerkinta
    {
        string teksti;
        string[] avainsanat;
        string foo="";
        public Blogimerkinta(string bar)
        {
              foo=bar;
        }
        public string Teksti { get; set; }
        public string[] Avainsanat { get; set; }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top