0

If I fill a ListBoxView with objects, the text that will display for each one will be the text produced by the ToString() function. Let's say I want to use ToString() in some cases, and GetSpecialString() in other cases. What is the best way to switch between the two?

Should I have two different objects with the same base class with different ToString() methods, or is there a way I can have both the ToString() and GetSpecialString() methods in the same class?

1
  • There's ListBox or ListView, no ListBoxView. ListBox always uses ToString() without an option to change that unless you use custom draw. Whatever logic you use should be in the classes whose objects you add. In their ToString override. Commented Aug 30, 2011 at 16:21

2 Answers 2

2

You could make a class-wrapper for your view model, which returns different strings by condition:

class ViewModelListItem {

  public ViewModelListItem(MyObject item) {
    this.Item = item;
  }

  public MyObject Item {
    get;
    private set;
  }

  public override ToString() {
    // to do: add your logic here
    if (...)
      return "case A";
    else
      return "Case B";
  }
}

Then just fill your ListView with such items.

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

Comments

0

If the choice between ToString() and GetSpecialString() is esclusive, just override ToString() in class you want and done.

2 Comments

I believe this is bad practice to unite the back-end with a view model
I'm talking about overiding to string on data model, so the call ListBox's Ite.ToString() will call appropriate override by producing desired string. OOP elegant solution, by me.

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.