c#在if语句中分配对象值

时间:2021-10-02 20:14:59

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.

我正在读取文件,并且逐行提供信息(这是我无法更改的)。我想创建一个对象,如果该行具有x值,并且该行具有y值,则为该对象指定一些值。这被证明是非常具有挑战性的。显然我做错了什么。

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
    {
    }
}

1 个解决方案

#1


3  

You can declare the object outside of the if statement, instantiate it in the if block and set it's value in the else block after you have checked that it isn't null. Something like this:

您可以在if语句之外声明对象,在if块中实例化它,并在检查到它不为null后在else块中设置它的值。像这样的东西:

multicast my_multicast = null;
if (line_split[i].Contains("LabelId"))
{
   try
   {
       gen.m_LabelId_pos.Add(multicast_ports[3], i);
       my_multicast = new multicast();
   }
   catch
   {
   }
}
else if (line_split[i].Contains("TotalFrameSentCount_PerSecond"))
{
    try
    {                                    
        gen.m_TotalFrameSentCount_PerSecond_pos.Add(multicast_ports[3], i);
        if(my_multicast!=null)
        {
            //do something with my_multicast here
        }
    }
    catch
    {
    }
}

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

另外,你应该避免吃掉你的异常,它们是为了帮助你,如果出现问题,这种方式会阻止它们隐藏它们,你就不会知道出了什么问题。使用

catch(Exception err)
{
}

#1


3  

You can declare the object outside of the if statement, instantiate it in the if block and set it's value in the else block after you have checked that it isn't null. Something like this:

您可以在if语句之外声明对象,在if块中实例化它,并在检查到它不为null后在else块中设置它的值。像这样的东西:

multicast my_multicast = null;
if (line_split[i].Contains("LabelId"))
{
   try
   {
       gen.m_LabelId_pos.Add(multicast_ports[3], i);
       my_multicast = new multicast();
   }
   catch
   {
   }
}
else if (line_split[i].Contains("TotalFrameSentCount_PerSecond"))
{
    try
    {                                    
        gen.m_TotalFrameSentCount_PerSecond_pos.Add(multicast_ports[3], i);
        if(my_multicast!=null)
        {
            //do something with my_multicast here
        }
    }
    catch
    {
    }
}

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

另外,你应该避免吃掉你的异常,它们是为了帮助你,如果出现问题,这种方式会阻止它们隐藏它们,你就不会知道出了什么问题。使用

catch(Exception err)
{
}