I have a class called Part that has 2 fields, PartNo & Filename. I make a list of "Parts" and then set the list as a datasource for a list box control. Then I set the displaymember to "PartNo" and ValueMember to "Filename". So when a user selects an item in the list box I want to use the "Filename" value. I couldn't figure out how to 开发者_如何学Goaccess it, but intellisense showed me this expression:
((Part)(drawingList.SelectedValue)).Filename
which works fine, but I'm wondering if there was a different way to access this value cause that looks a bit too convoluted and I imagine there is a better way to reference it. If anything just so I can understand the language better.
Thanks!
The only thing I'd change is to remove the unnecessary parentheses:
((Part)drawingList.SelectedValue).Filename
Since List controls are not generically typed, the cast is required in this case, and casts in the C-based programming languages tend to require an unfortunate number of parentheses. If it's hard to look at, you could certainly split it into a couple of lines, but most C# developers have no problem understanding the code as it stands.
var selectedPart = (Part)drawingList.SelectedValue;
var selectedFilename = selectedPart.Filename;
精彩评论