我正在尝试创建一个函数,用于将接口对象分配给我在结构中轻松创建的动态对象。这是我的结构:
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
方法上分配它吗?
答案 0 :(得分:0)
我找到了一个解决方案,只需将结构类型更改为class即可。
我更改了这些:
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);
它成功进行了:)希望这种经验对某人有帮助。祝你有美好的一天...