Home > front end >  Array is returning my Namespace and Type but not the Values that i put in
Array is returning my Namespace and Type but not the Values that i put in

Time:09-06

im making .NET desktop app, im running into a problem. here is my class that i created with name,phone and salary properties:

namespace Employee
{
    internal class myEmployee
    {
      private string _name;
      private string _phone;
      private double _salary;

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public string Phone
    {
        get
        {
            return _phone;
        }
        set
        {
            _phone = value;
        }
    }
    public double Salary
    {
        get
        {
            return _salary;
        }
        set
        {
            if (value >= 0)
            {
                _salary = value;
            }
            else
            {
                _salary = 0;
            }


        }


    }

    public double AnnualSal
    {
        get { return _salary * 12; }
    }

    public myEmployee(string n, string p, double s)
    {

        
        Name = n;
        Phone = p;
        Salary = s;
    }

    public void RaisePay()
    {

        Salary *= 0.1;
    }




}

}

after that i initialized my array as follows:

using Employee;

   myEmployee[] emp = { new myEmployee("Bran","111-111-1111",2500),
                 new myEmployee("Patrick","222-222-2222",1000),
                 new myEmployee("Spongebob","333-333-3333",1500),

};

                 Console.WriteLine(emp[0]);

and when it runs it just displays my class name and type, not the items that i put in:

                 Employee.myEmployee 

can someone help me in the right direction? I m thinking its cause of my naming convention but im not sure.

CodePudding user response:

By default, Console.Writeline(object) (or in your case Console.WriteLine(emp[0]) calls the ToString() method on Object or an overridden version of ToString. The default for Object.ToString is to output the type name (including namespace) as you've discovered.

While you can override ToString on your Employee type, that implementation would be used on all cases where you are trying to use ToString and doesn't allow customizations for different needs. Additionally, if you override ToString, the recommendation is to also override Equals and GetHashCode which doesn't make sense in your particular case.

As an alternative to overriding ToString, you might consider creating a specialized method to format the output:

public string PrintableString
{
   return $"Name={Name},Phone={Phone}";
}

or just format the value you want to display when you want to use it:

// Allocating a pointer here so that you don't index into the array multiple times.
var thisEmp = emp[0]; 
Console.WriteLine($"Name={thisEmp.Name},Phone={thisEmp.Phone}");

CodePudding user response:

myEmployee is a complex type (a class with many properties). By default, .ToString doesn't know what you want to show, so just outputs the name of the class.

If you want to show what properties are, you need to override ToString manually, and return the properties you are interested in (it doesn't have to be all of them).

class myEmployee
{
    public override string ToString()
       => $"Name={Name},Phone={Phone}";
}
  •  Tags:  
  • c#
  • Related