CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Lil' question about parsing

    I would go one step further and simply create your own class for this. The ComboBox will call ToString to display the items, so your class can just override that method for display purposes, and expose an int property for backend data collection:

    Code:
    class Whatever
    {
        public Whatever( int years )
        {
            Years = years;
        }
    
        public int Years { get; private set; }
    
        public override string ToString()
        {
            return String.Format( "{0} years", Years );
        }
    }
    Now just add instances of the "Whatever" class to the ComboBox:

    Code:
    comboBox.Items.Add( new Whatever( 2 ) );
    comboBox.Items.Add( new Whatever( 5 ) );
    // ...
    
    // and to get them back...
    var item = (Whatever)comboBox.SelectedItem;
    int years = item.Years;  // no parsing required
    Last edited by BigEd781; May 12th, 2011 at 10:32 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured