none
Beginner question: ToString() and constructors

    Question

  • Hi again. Back with another newbie question.

    I'm still working through my book tutorial. I've written out the following code:

        

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace inheritance
    {
        class Member
        {


            protected int annualFee;
            private string name;
            private int memberID;
            private int memberSince;

            public override string ToString()
            {
                return "\nName: " + name + "\nMember ID: " + memberID + "Member since: " + memberSince + "\nAnnual fee: " + annualFee;
            }

            public Member()
            {
                Console.WriteLine("Parent constructor with no parameter");

            }

            public Member(string pName, int pMemberID, int pMemberSince)
            {
                Console.WriteLine("Parent constructor with 3 parameters");
                name = pName;
                memberID = pMemberID;
                memberSince = pMemberSince;


            }





            static void Main(string[] args)
            {
                Member obj = new Member();
                obj.ToString();

                Console.Read();
            }
        }

    }

    The console reads "Parent constructor with no parameter" when I run this code. I'm not sure why, though. Can anyone shed some light? I assumed calling "obj.Member()" would display that message.

    Also, I'm wondering why using "obj.Member();" in main doesn't work. Can I not call a constructor in main?

    I don't need anyone to change the code to make it work. I'm a beginner and likely won't understand your changes. I'm just trying to grasp the above concepts.

    Thanks in advance. :)

    Monday, April 10, 2017 11:42 PM

Answers

  • Hello,

     Many Class object will have different constructors. Some you can

    call using the default constructor or others can take parameters with

    the call.

     Your code calls, Member obj = new Member() , uses modified default constructor

     Try this code, Member obj = Member( "Clydey2Times", 1, 2017 );

     As you will see the outputs are different because of the constructor

    you called.

      MSDN Ref:

     https://msdn.microsoft.com/en-us/library/ms173115.aspx?f=255&MSPPError=-2147217396

    Hope this helps :)

     

    • Marked as answer by Clydey2Times Tuesday, April 11, 2017 12:09 AM
    Tuesday, April 11, 2017 12:02 AM
  • That line isn't trying to call a constructor, it's trying to call a method called ToString. But there is no method by that name which takes three arguments, so you get an error.

    Constructors are invoked using the "new" keyword, and as the name suggests, they construct (or create) a new instance of the class. If you want to see what happens when you call the constructor with three arguments, try this.

    Member obj2 = new Member("John", 322, 1920);

    If you want to use the ToString method, try this.

    Console.WriteLine(obj2.ToString()); // You must have created obj2 as above for this to work.

    • Marked as answer by Clydey2Times Tuesday, April 11, 2017 12:09 AM
    Tuesday, April 11, 2017 12:04 AM
  • The method calls must match the method signature.  The method signature for Member.ToString takes no arguments and returns a string.  The method call obj.ToString("John", 322, 1920); is trying to call a ToString method with a different signature.  The matching signature would be string ToString(string, int, int).
    • Marked as answer by Clydey2Times Tuesday, April 11, 2017 12:09 AM
    Tuesday, April 11, 2017 12:06 AM

All replies

  • This line...

    Member obj = new Member();

    ... calls the constructor. There is nothing in the parenthesis (round brackets) so the constructor in question is the one with no parameters. That is, this one.

            public Member()
            {
                Console.WriteLine("Parent constructor with no parameter");

            }

    Monday, April 10, 2017 11:52 PM
  • Thanks for the response.

    I also tried obj.ToString("John", 322, 1920);

    ...in main. I get an error when I try to use the constructor with 3 parameters, though.

    "No overload for method 'ToString' takes 3 arguments".

    Any ideas?

    Never mind, I got it. I had to put the parameter values in the brackets when I called the constructor.

    Thanks for helping me work it out.

    Monday, April 10, 2017 11:57 PM
  • Hello,

     Many Class object will have different constructors. Some you can

    call using the default constructor or others can take parameters with

    the call.

     Your code calls, Member obj = new Member() , uses modified default constructor

     Try this code, Member obj = Member( "Clydey2Times", 1, 2017 );

     As you will see the outputs are different because of the constructor

    you called.

      MSDN Ref:

     https://msdn.microsoft.com/en-us/library/ms173115.aspx?f=255&MSPPError=-2147217396

    Hope this helps :)

     

    • Marked as answer by Clydey2Times Tuesday, April 11, 2017 12:09 AM
    Tuesday, April 11, 2017 12:02 AM
  • That line isn't trying to call a constructor, it's trying to call a method called ToString. But there is no method by that name which takes three arguments, so you get an error.

    Constructors are invoked using the "new" keyword, and as the name suggests, they construct (or create) a new instance of the class. If you want to see what happens when you call the constructor with three arguments, try this.

    Member obj2 = new Member("John", 322, 1920);

    If you want to use the ToString method, try this.

    Console.WriteLine(obj2.ToString()); // You must have created obj2 as above for this to work.

    • Marked as answer by Clydey2Times Tuesday, April 11, 2017 12:09 AM
    Tuesday, April 11, 2017 12:04 AM
  • The method calls must match the method signature.  The method signature for Member.ToString takes no arguments and returns a string.  The method call obj.ToString("John", 322, 1920); is trying to call a ToString method with a different signature.  The matching signature would be string ToString(string, int, int).
    • Marked as answer by Clydey2Times Tuesday, April 11, 2017 12:09 AM
    Tuesday, April 11, 2017 12:06 AM
  • Hello,

     Your syntax of ToString is incorrect. That method does

    not take parameters.  It returns the object type back as 

    a String type.

     Thanks :)

    Tuesday, April 11, 2017 12:06 AM
  • Thanks for all the replies. I understand things a bit better now. :)
    Tuesday, April 11, 2017 12:08 AM