I want to allow the user, when searching for a Platypus, to enter either the Platypus’ ID or Name (or portion of a name).
The database is down right now, so I can’t test this code, but the fact that I’m assigning to kvp twice doesn’t seem right. Anyway, first my code:
String SearchVal = textBoxPlatypusNumOrPartialName.Text.Trim();
int PlatypusID;
int.TryParse(SearchVal, out PlatypusID);
Dictionary<int, string> CandidatePlatypusDict = PlatypusID > 0 ? PlatypusSetupData.GetPlatypusById(PlatypusID) : PlatypusSetupData.GetCandidatePlatypusFromName(SearchVal);
KeyValuePair<int, String> kvp = new KeyValuePair<int, string>();
using (var getPlatypusIDAndNameDlg = new GetPlatypusForm(CandidatePlatypusDict)) {
DialogResult dr = getPlatypusIDAndNameDlg.ShowDialog();
if (dr == DialogResult.OK) {
kvp = getPlatypusIDAndNameDlg.PlatypusKVP;
}
}
…Now, if I try this instead:
KeyValuePair<int, String> kvp; // = new KeyValuePair<int, string>();
I get: “Use of unassigned local variable ‘kvp'”
And I can’t assign null as KeyValuePair, as it is a non-nullable type.
Is this instantiation of kvp followed by assignment to it okay, and if not, how can I work around it?
Well it’s okay – but you might instead want:
That way it’s clearly only assigned once, but is always assigned. Next you need to work out what value you want it to have in the situation where
drisn’tDialogResult.OK.