C# cant make an object of my class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • malman69
    New Member
    • Aug 2009
    • 1

    C# cant make an object of my class

    Code:
    class Product
    {
    public String Productname;
    public String Brand;
    public String Price;
    
    public Product(String productname, String brand, String price)
    { 
    Productname = productname;
    Brand = brand;
    Price = price;
    }
    }
    now i am only a beginner with c# but when i try making a new object of this class it gives me the error

    'WindowsFormsAp plication1.Prod uct' does not contain a constructor that takes '0' arguments

    its probably a stupid error i am making but any help would be appreciated :)
    Last edited by PRR; Aug 6 '09, 09:12 AM. Reason: Please post code in [code] [/code] tags.
  • flohack
    New Member
    • Aug 2009
    • 2

    #2
    It would be much easier to help if you could also post the line of code whre the error appears. Did you try something like Product A = new Produc(); ??

    floh

    Comment

    • artov
      New Member
      • Jul 2008
      • 40

      #3
      You did not tell us how you called the constructor, but based on the error message, I assume something like

      Product television = new Product();
      which means that you are calling the Product constructor with no parameters. However, you do not have parameterless constructor, as the error message tells. Either create one (you can have several constructors with different number or types of parameters) or call the constructor with right parameters, like

      Product television = new Product("LCD 25", "Tellie", "$2220");

      Comment

      • PRR
        Recognized Expert Contributor
        • Dec 2007
        • 750

        #4
        I had no problem in instantiating your Product class
        Code:
        Product p = new Product();
        
                    Product p1 = new Product("A", "B", "C");
        A better way to write the class would be
        Code:
        class Product
                {
                    //public String Productname;
                    //public String Brand;
                    //public String Price;
        
        
                    private String productName;
                    private String brand;
                    private String price;
        
                    public Product(String productname, String brand, String price)
                    {
                        Productname = productname;
                        Brand = brand;
                        Price = price;
                    }
        
                    public String Productname
                    {
                        get { return productName; }
        
                        set { productName = value; }
                    }
        
        
                    public String Brand
                    {
                        get { return brand; }
        
                        set { brand = value; }
                    }
                    public String Price
                    {
                        get { return price; }
        
                        set { price = value; }
         
                    }
                }
        P.S. Do not double post.

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          'WindowsFormsAp plication1.Prod uct' does not contain a constructor that takes '0' arguments
          Slim but possible...
          Is this a custom usercontrol that you are dragging from the toolbox to a form?
          If so, it has to have a constructor that takes no arguments. That's because you have no way to specify arguments when you drag from the toolbox.

          Comment

          • ThatThatGuy
            Recognized Expert Contributor
            • Jul 2009
            • 453

            #6
            considering you're new to c#....
            I'll provide the most simple way......

            Your class Product....
            has a constructor which has
            public Product(String productname, String brand, String price)
            3 arguments or parameters.....
            You're probably doing this......

            Product objProduct=new Product();

            while you should be doing this...

            Product objProduct=new Product("Loose motion syrup","Loose industries","5. 00");

            And why you're price parameter is String....i can't find a solution...

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Can't you override the method in C#? If so, just duplicate the method with 0 parameters.

              Comment

              • ThatThatGuy
                Recognized Expert Contributor
                • Jul 2009
                • 453

                #8
                Hmm.....You're absolutely correct.......M arkus
                That can be done......but i think malman69 is fairly pretty new to C#.....
                It takes a little time for a newbee to implement OOP concepts to real use..

                Comment

                • cloud255
                  Recognized Expert Contributor
                  • Jun 2008
                  • 427

                  #9
                  Originally posted by ThatThatGuy
                  Hmm.....You're absolutely correct.......M arkus
                  That can be done......but i think malman69 is fairly pretty new to C#.....
                  It takes a little time for a newbee to implement OOP concepts to real use..
                  Well, what's the point of half teaching someone how to program in an OO environment? Sure it takes a while to get used to it, but if we guide people on best practices from the start it saves them having to re-think how they code/design a couple of months down the line.

                  If the OP doesn't understand the concept of overloading there are many people here more than happy to explain it...

                  IMHO the only way to really know whats going on in any programming language is to dive into its guts and start ripping it apart, you're gonna fail several times but some perseverance is the only way to become a great developer. But thats just my opinion.

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    What was all this discussion about?
                    The OP had created a class without a default constructor, but was trying to instanciate with a default constructor.

                    Adding in some form of the following:
                    [code=c#]
                    public Product()
                    {//an empty constructor
                    Productname = "";
                    Brand = "";
                    Price = "";
                    }
                    [/code]
                    Should remidy the issue?

                    Comment

                    Working...