Answer the question
In order to leave comments, you need to log in
NullReferenceException when returning a struct from a function?
Function to create structure
private FormData CreateFormData()
{
int speed = 0;
try
{
speed = Convert.ToInt32(cbSpeed.SelectedItem);
} catch (Exception e) { if (ExceptionEvent != null) ExceptionEvent(e); }
//Создание структуры с передачей пареметров string, int, string.
return new FormData(cbCOM.SelectedItem.ToString(), speed, tbSendText.Text); //.NullReferenceException
}
public struct FormData
{
public string Port;
public int Baud;
public string Messenge;
public FormData(string port, int baud, string mess) { Port = port; Baud = baud; Messenge = mess; }
}
Answer the question
In order to leave comments, you need to log in
it's most likely the cbCOM.SelectedItem you're trying to call ToString() on.
see structure constructor parameters, string is an object and can be null.
return new FormData(cbCOM?.SelectedItem?.ToString(), speed, tbSendText?.Text);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question