[英]Variable scoping c#
我对C#相当陌生,并且收到错误消息“对象引用未设置为对象的实例”。 我正在创建XML数据包,并将其发送到外部设备进行控制。 如果我将以下代码放在click事件中的表单上,则效果很好。 在btn Click事件上,它看起来像这样:
SetTestInfoResponse testDataDs = null;
TestInformation testInfo = null;
this.PopulateTestDataXml();
string stringRequestXML = string.Empty;
string stringResponseXML = string.Empty;
//Creates Request packet
stringRequestXML = XMLCommunicationPackets.SetTestInformation (testInfo, testInfo.TestID, testInfo.TestUser, testInfo.TestSampleType, testInfo.TestSampleId, testInfo.TestMethodNumber, testInfo.TestTubeSn, testInfo.TestComments);
//Write set Test Info XML Packet and get response for ack or failure.
stringResponseXML = PluginContext.GetInstance().InstrumentDriverCurrent.GetInstrumentControl().SetCommonParameter(stringRequestXML);
但是,如果我将整个函数移出表单,并在单击按钮时尝试调用它,则会收到错误消息。
在窗体的方法中以.cs文件形式编写,其读取为:
public static SetTestInfoResponse SetTestData()
{
SetTestInfoResponse testDataDs = null;
TestInformation testInfo = null;
string stringRequestXML = string.Empty;
string stringResponseXML = string.Empty;
//Creates Request packet
stringRequestXML = XMLCommunicationPackets.SetTestInformation (testInfo, testInfo.TestID, testInfo.TestUser, testInfo.TestSampleType, testInfo.TestSampleId, testInfo.TestMethodNumber, testInfo.TestTubeSn, testInfo.TestComments);
//Write set Test Info XML Packet and get response for ack or failure.
stringResponseXML = PluginContext.GetInstance().InstrumentDriverCurrent.GetInstrumentControl().SetCommonParameter(stringRequestXML);
构建stringRequestXml时发生错误。
我的问题的一部分是PopulateTestData()是表单本身上的一种方法。 其目的是从txtboxes和cmbboxes中获取数据并将它们分配给各自的参数。
private TestInformation PopulateTestDataXml()
{
TestInformation UiTestData = new TestInformation();
UiTestData.TestID = txtTestId.Text;
UiTestData.TestUser = cmbUsers.SelectedItem.ToString();
UiTestData.TestSampleType = txtSampleType.Text;
UiTestData.TestSampleId = txtSampleId.Text;
UiTestData.TestMethodNumber = Convert.ToInt32(cmbMethod.SelectedItem);
UiTestData.TestTubeSn = txtTubeSerialNum.Text;
UiTestData.TestComments = txtComments.Text;
return UiTestData;
}
这是我收到错误的SetTestInformation()方法:
public static string SetTestInformation(TestInformation testInfo, string stringTestId, string stringUser, string stringSampleType, string stringSampleId, int intMethodNumber, string stringTubeSn, string stringComments)
{
try
{
string stringRequestXMLPacket = string.Empty;
string stringType = @"Request";
string stringCommand = @"Set";
string stringArgument = @"TestInformation";
CommunicationPacket requestXMLPacket = new CommunicationPacket(stringRootTag, stringXMLVersion, stringType, stringCommand);
requestXMLPacket.AddCommandArgument(stringArgument);
requestXMLPacket.AddArgumentItem(stringArgument, "sTestId", testInfo.TestID.ToString());
requestXMLPacket.AddArgumentItem(stringArgument, "sUser", testInfo.TestUser.ToString());
requestXMLPacket.AddArgumentItem(stringArgument, "sSampleType", testInfo.TestSampleType.ToString());
requestXMLPacket.AddArgumentItem(stringArgument, "sSampleId", testInfo.TestSampleId.ToString());
requestXMLPacket.AddArgumentItem(stringArgument, "nMethodNumber", testInfo.TestMethodNumber.ToString());
requestXMLPacket.AddArgumentItem(stringArgument, "sTubeSn", testInfo.TestTubeSn.ToString());
requestXMLPacket.AddArgumentItem(stringArgument, "sComments", testInfo.TestComments.ToString());
stringRequestXMLPacket = requestXMLPacket.CreateXMLPacket();
return stringRequestXMLPacket;
}
catch (Exception ex)
{
throw ex;
}
}
知道我在这里遇到变量范围问题。 在调用setTestData()方法之前,我仍然必须在窗体上使用方法PopulateTestDataXml。 但是,当我调用方法时,必须声明testInfo = null或SetTestInformation的参数无效(“在当前上下文中不存在”)。 我需要传递什么,以及如何使其作为btn click表单上的调用方法工作? 我需要这样做,因为我还编写了许多反序列化函数,以捕获响应xml中的错误消息(这些都工作正常),以及有关click事件的太多信息。 (我需要学习)。
谢谢
您的两个示例都不起作用(无论放置在何处)。 这是完全不正确的:
TestInformation testInfo = null;
// ...
stringRequestXML = XMLCommunicationPackets.SetTestInformation (testInfo,
testInfo.TestID, ...);
// ^^ BANG!
您的testInfo
对象为null
。 当您尝试访问null
对象上的任何内容时 ,都会引发NullReferenceException
。 您需要先对其进行初始化。 您正在尝试在PopulateTestDataXml
方法中执行此操作。该方法将返回您的对象。 因此,将您的代码更改为此:
TestInformation testInfo = PopulateTestDataXml(); // assign it
这是你的问题。
public static SetTestInfoResponse SetTestData()
{
SetTestInfoResponse testDataDs = null;
TestInformation testInfo = null;
string stringRequestXML = string.Empty;
string stringResponseXML = string.Empty;
//Creates Request packet
stringRequestXML = XMLCommunicationPackets.SetTestInformation (testInfo, testInfo.TestID, testInfo.TestUser, testInfo.TestSampleType, testInfo.TestSampleId, testInfo.TestMethodNumber, testInfo.TestTubeSn, testInfo.TestComments);
//Write set Test Info XML Packet and get response for ack or failure.
stringResponseXML = PluginContext.GetInstance().InstrumentDriverCurrent.GetInstrumentControl().SetCommonParameter(stringRequestXML);
您是否正在为这些对象分配值,我看到它们只是声明而从未分配。
SetTestInfoResponse testDataDs = null;
TestInformation testInfo = null;
我看不到您使用空对象,所以我想知道您是否稍后再设置它们,您还说错误发生在
private TestInformation PopulateTestDataXml()
{
TestInformation UiTestData = new TestInformation();
UiTestData.TestID = txtTestId.Text;
UiTestData.TestUser = cmbUsers.SelectedItem.ToString();
UiTestData.TestSampleType = txtSampleType.Text;
UiTestData.TestSampleId = txtSampleId.Text;
UiTestData.TestMethodNumber = Convert.ToInt32(cmbMethod.SelectedItem);
UiTestData.TestTubeSn = txtTubeSerialNum.Text;
UiTestData.TestComments = txtComments.Text;
return UiTestData;
}
在将其移出表单后,这意味着它的文本框引用可能已损坏……因此,您可以做的是存储一个指针,例如在program.cs中,调用表单以显示它,则可以创建一个形式的静态对象,然后将其放在您的类中,然后在program.cs文件中进行设置,例如:
Form1 f = new Form(); MyClass.staticFormPointer = f;
并在调用方法上用(f)替换(new Form()),您的my类如下所示:
class MyClass{
public static Form1 staticFormPointer = null;
//your code
.
.
.
// and in your methods you call it like this txtBox1.Text -> staticFormPointer.txtBox1.Text
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.