1

When attempt to fill an array in C# with my custom type class, the array populates with the class's location in the namespace. However, I need the array to populate with the values I specified with that type.

I have tried defining my array using each of these implementations with the same results:

List<FieldName> fieldNames = new List<FieldName>() { "event_ref", "user_id", "sys_time" };

FieldName[] fieldNames = { "event_ref", "user_id", "sys_time" };

In both cases the values that get implemented in the lower function calls (nothing fancy) is ElectronicSJP.DatabaseIF.FieldName for all three of the fields.

Upon setting a breakpoint, I can see that fieldNames is populated as so:

enter image description here

The values that I specified are at the next level down, if that table is expanded. My class for FieldName is defined as such:

class FieldName
{
    private string name = "";
    public static implicit operator string(FieldName fn)
    {
        return (fn == null) ? null : fn.name;
    }
    public static implicit operator FieldName(string fn)
    {
        return new FieldName { name = fn };
    }
}

I suspect that I may need to add another implicit operator, or two, so I could use some help with that or any other ideas you think could fix this problem. Thanks.

7
  • 1
    I'm sort of lost - you want it to populate the array with strings, not FieldNames? Commented Jun 1, 2016 at 16:21
  • 2
    implement toString in your object with the string you'd like to see in your intellisense debugger. Commented Jun 1, 2016 at 16:26
  • 1
    Is this question in English or my English is that bad? Commented Jun 1, 2016 at 16:28
  • @Jeremy - alternately, you can use the DebuggerDisplay attribute. Commented Jun 1, 2016 at 16:28
  • @AdamV thanks! learn something new everyday! Commented Jun 1, 2016 at 16:34

1 Answer 1

2

When the debugger shows an object, or list of objects, it will show the type rather than a particular field.

In order to see a "user friendly value", one can either override the toString method that exists on the Object class, or you can decorate the property you want shown with the DebuggerDisplay Attribute (credit to Adam V up above in the comments!)

example:

 public override string ToString()
    {
        return name;
    }

Link to how to override a method. https://msdn.microsoft.com/en-us/library/ms173154.aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.