I am reading a file and information is provided line by line (this I can’t change). I want to create an object if the line has x value and if the line has y value, assign some values to the object. this proves to be very challenging. obviously I am doing something wrong.
if (line_split[i].Contains("LabelId"))
{
try
{
gen.m_LabelId_pos.Add(multicast_ports[3], i);
multicast my_multicast = new multicast();
}
catch
{
}
}
else if (line_split[i].Contains("TotalFrameSentCount_PerSecond"))
{
try
{
gen.m_TotalFrameSentCount_PerSecond_pos.Add(multicast_ports[3], i);
// want to assign y value to the object here. but cant
}
catch
{
}
}
You can declare the object outside of the
ifstatement, instantiate it in theifblock and set it’s value in theelseblock after you have checked that it isn’tnull. Something like this:As an aside note, you should avoid eating your
Exceptions, they are meant to help you if something goes wrong, this way catch block will hide them and you won’t have a clue what went wrong. Use