【问题标题】:How can i assign an interface object to dynamic object in a method?如何在方法中将接口对象分配给动态对象?
【发布时间】:2018-12-26 10:29:27
【问题描述】:

我正在尝试创建一个函数,用于将接口对象分配给我在结构中轻松创建的动态对象。这是我的结构:

public struct PSU
{
    public CheckBox CallHostessButton0, CallHostessButton1,
        ReadLightButton0, ReadLightButton1, ReadLightButton2, ReadLightButton3;
    public PictureBox BeltLight, SmokeLight;
}

这是我的功能:

public void AssignObjectsToPSU(UniversalVariables.PSU psu, CheckBox 
CallHostessBtn0, EventHandler HBtn0CheckedChanged, CheckBox 
CallHostessBtn1, EventHandler HBtn1CheckedChanged)
{
// and i'm trying to do this in my function:
...
psu.CallHostessButton0 = CallHostessBtn0;
psu.CallHostessButton0.CheckedChanged += new EventHandler(HBtn0CheckedChanged); 
...
}

程序运行顺利,但是当我尝试在 CheckedChanged 事件中执行此操作时:

if (TestPSU.CallHostessButton0.Checked)
{
    TestPSU.CallHostessButton0.BackgroundImage = Properties.Resources.lamp_on;
}
else
{
    TestPSU.CallHostessButton0.BackgroundImage = Properties.Resources.lamp_off;
}

程序给出一个错误:

Exception thrown: 'System.NullReferenceException' in IOS_Interface.exe
TestPSU.**CallHostessButton0** was null.

它怎么可能是空的?我在我的 AssignObjectsToPSU 方法上分配这个?

【问题讨论】:

    标签: c# object methods dynamic


    【解决方案1】:

    我找到了一个解决方案,只需将结构类型更改为类就可以了。

    我改变了这些:

    public struct PSU
    {
         public CheckBox CallHostessButton0, CallHostessButton1,
         ReadLightButton0, ReadLightButton1, ReadLightButton2, ReadLightButton3;
         public PictureBox BeltLight, SmokeLight;
     }
    
     public void AssignObjectsToPSU(UniversalVariables.PSU psu, CheckBox 
     CallHostessBtn0, EventHandler HBtn0CheckedChanged, CheckBox 
     CallHostessBtn1, EventHandler HBtn1CheckedChanged)
     {
     // and i'm trying to do this in my function:
     ...
     psu.CallHostessButton0 = CallHostessBtn0;
     psu.CallHostessButton0.CheckedChanged += new EventHandler(HBtn0CheckedChanged); 
     ...
     }
    

    到这里:

    //UniversalVariables.cs
    public class PSU
    {
        public CheckBox CallHostessButton0, CallHostessButton1,
            ReadLightButton0, ReadLightButton1, ReadLightButton2, ReadLightButton3;
        public PictureBox BeltLight, SmokeLight;
    
        public PSU(CheckBox CallHostBtn0, CheckBox CallHostBtn1, CheckBox ReadLightBtn0, CheckBox ReadLightBtn1,
            PictureBox BeltLght, PictureBox SmokeLght, [Optional] CheckBox ReadLightBtn2, [Optional] CheckBox ReadLightBtn3)
        {
            Log("[Info] Assigning objects to PSU unit...");
            CallHostessButton0 = CallHostBtn0;
            CallHostessButton1 = CallHostBtn1;
            ReadLightButton0 = ReadLightBtn0;
            ReadLightButton1 = ReadLightBtn1;
    
            if (CallHostessButton0 == null || CallHostessButton1 == null || ReadLightButton1 == null || ReadLightButton0 == null)
            {
                LogReport("[Failed]");
            }
    
            else { LogReport("[OK]"); }
    
            if (ReadLightBtn2 != null)
            {
                Log(String.Format("[Info] Extra {0} detected...", nameof(ReadLightBtn2)));
                this.ReadLightButton2 = ReadLightBtn2;
                LogReport("[OK]");
    
                if(ReadLightBtn3 != null)
                {
                    Log(String.Format("[Info] Extra {0} detected...", nameof(ReadLightBtn3)));
                    this.ReadLightButton3 = ReadLightBtn3;
                    LogReport("[OK]");
                }
            }
    
            this.BeltLight = BeltLght; this.SmokeLight = SmokeLght;
        }
    

    当你想定义新的 PSU 时,你只需要在你的表单代码中这样做:

     // Interface.cs
     UniversalVariables.PSU[] TestPSU = new UniversalVariables.PSU()cb_CallHost0, cb_CallHost1, cb_ReadLight0, cb_ReadLight1, pb_BeltLghtTest, pb_SmkLghtTest);
    

    或者您需要一组 PSU(或者您的项目需要类似的东西):

     //Interface.cs
     UniversalVariables.PSU[] TestPSU = new UniversalVariables.PSU[12];
     TestPSU[<indexOfYourPSU>] = new UniversalVariables.PSU(cb_CallHost0, cb_CallHost1, cb_ReadLight0, cb_ReadLight1, pb_BeltLghtTest, pb_SmkLghtTest);
    

    它成功了 :) 我希望这次经历可以帮助到某人。祝你有美好的一天...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 2015-08-20
      • 1970-01-01
      相关资源
      最近更新 更多