-4

I have a method that returns an object. If I return an object then it will give a fully qualified name of the class. But I want to return the data members of the object.

public GetDetails GetInfo()
{
    GetDetails detail = new GetDetails("john", 47);
    return detail;
}

public override string ToString()
{
    return this.Name + "|" + this.Age;
}

I override the ToString() method to get the data member of detail object. But it is not working. How can I achieve that?

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
Code Break
  • 137
  • 1
  • 2
  • 7
  • You need to be more specific. Returning an object returns just that, the object. `ToString` just changes the *string representation* of that object. – BradleyDotNET Mar 05 '15 at 01:48
  • `if i return a object then it will gives a fully qualified name of the class` it either returns `object` or `string`. Generally the full type name means that there is nothing meaningful in `ToString()` – Ňɏssa Pøngjǣrdenlarp Mar 05 '15 at 01:50
  • Are you asking that in your `ToString()` override you would like to access the members (properties?) of the `detail` variable which you have locally declared in `GetInfo()`? – Quality Catalyst Mar 05 '15 at 02:16
  • Yes @Quality Catalyst – Code Break Mar 05 '15 at 03:44
  • "if i return a object then it will gives a fully qualified name of the class" No it doesn't, it just looks that way when you (perhaps implicitly) invoke the object's ToString() method. But overriding ToString() is not a good way to get access to things inside an object. The object should have public properties or public methods that you invoke by using "objectReference dot property" or "objectReference dot method". You need to learn more about how to program with objects in C#. – RenniePet Mar 05 '15 at 03:44

2 Answers2

1

What you ask for doesn't work because detail is a private variable and in the scope of the GetInfo() method. Therefore it is not accessible from outside this method.

It's hard to guess what the context of these two methods is; however, I assume you should keep state in your class to allow detail to be rendered in the ToString() method.

This example might not be a perfect solution, but it will fix your issue:

class MySpecialDetails
{
    // declare as private variable in scope of class
    // hence it can be accessed by all methods in this class
    private GetDetails _details; // don't name your type "Get..." ;-)

    public GetDetails GetInfo()
    {
        // save result into local variable
        return (_details = new GetDetails("john", 47));
    }

    public override string ToString()
    {
        // read local variable
        return _details != null ? _details.Name + "|" + _details.Age : base.ToString();
    }
}
Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
0

You can create a String Extension Method.

 public static string StringExtension(this GetDetails input)
 {
     return input.Name + "|" + input.Age;
 }

This static method is typically in a static class. Then you would call it like this

public string GetInfo() 
{
    GetDetails detail = new GetDetails("john", 47);
    return detail.ToString();
}
Eddie
  • 466
  • 5
  • 8