变量范围c#

时间:2014-01-15 23:31:05

标签: c# xml methods parameters

我对c#相当新,并且收到错误“对象引用未设置为对象的实例。”我正在创建一个XML数据包并将其发送到外部设备进行控制。如果我在单击事件中将以下代码放在表单上,​​它可以很好地工作。 在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和cmbbox中获取数据并将它们分配给各自的参数..

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。但是当我调用Method时,我必须声明testInfo = null或者SetTestInformation的参数无效(“当前上下文中不存在”)。我需要传递什么以及如何在btn点击表单上作为被调用的方法工作?我需要这样做,因为我有很多反序列化函数编写,以捕获响应xml中的错误消息(这些都工作正常)和它在click事件上的太多信息。 (我需要学习)。

由于

3 个答案:

答案 0 :(得分:1)

您的示例都不应该起作用(无论您将它们放在何处)。这完全是错误的:

TestInformation testInfo = null;
// ...
stringRequestXML = XMLCommunicationPackets.SetTestInformation (testInfo, 
                                               testInfo.TestID, ...);
//                                                     ^^ BANG!

您的testInfo对象为null。当您尝试访问null对象上的任何时,会抛出NullReferenceException。您需要先将其初始化。你试图在你的PopulateTestDataXml方法中做到这一点..它返回你追求的对象。所以将代码更改为:

TestInformation testInfo = PopulateTestDataXml(); // assign it

答案 1 :(得分:0)

这是你的问题..

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;

答案 2 :(得分:0)

我没有看到你使用空对象,所以我想知道你是否稍后设置它们,你也说错误发生在

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())调用方法, 你的班级是这样的:

class MyClass{
    public static Form1 staticFormPointer = null;
    //your code
    . 
    .
    .
// and in your methods you call it like this txtBox1.Text -> staticFormPointer.txtBox1.Text
}
相关问题