如何使用C#在class2的void方法中使用class1的void方法中声明的字符串值?

时间:2014-01-27 08:32:12

标签: c#

我在两个不同的测试文件中有class1和class2。应该在class1方法中使用void返回类型声明一个字符串。然后在class2方法中使用带有void返回类型的字符串值。我想要的样本脚本如下。

Testfile1

public class Class1
{ 
    public string test1{get; set;}

    public void method1()
    {
       test1 = "xyz"; 
    }
}

Testfile2:

using test1;

public class Class2
{
    Class1 Obj = new Class1();

    public string test2;

    public void method2()
    {
        test2 = Obj.test1;
    } 
}

我编码的ui项目中的实际脚本文件,我需要从'prodSearch_ND'方法中提取'test'的值。

TestFile1:

  /// <summary>
/// Summary description for CodedUITest
/// </summary>
[CodedUITest]
public class CodedUITest:GenericFunctionsLib
{
    public string WinTitle { get; set; }
    public string EntityId { get; set; }

    ProdSearchValues_CO Obj_CO = new ProdSearchValues_CO();
    CreateWellValues_ND Obj_ND = new CreateWellValues_ND(); 



    [TestMethod]
    public void prodSearch_ND()
    {
        EntityId = Obj_ND.EntityId_ND;

        WinTitle = "CATS Energy - NorthDakota";

        this.ProdSearch1.RecordedMethod1();


        //this.ProdSearch1.AssertMainForm();
        //this.ProdSearch1.AssertProdTree();
        //this.ProdSearch1.ClickProdSearch();
        //this.ProdSearch1.AssertEntityTextBox();
        //this.ProdSearch1.EnterEntityId();
        ////this.ProdSearch1.AssertEntityIdExists();
        //this.ProdSearch1.AssertEntityTab();


    }

TestFile2:

 [GeneratedCode("Coded UITest Builder", "10.0.30319.1")]
public partial class ProdSearch1
{
    CodedUITest Obj = new CodedUITest();
    public string test;


    /// <summary>
    /// RecordedMethod1 - Use 'RecordedMethod1Params' to pass parameters into this method.
    /// </summary>
    public void RecordedMethod1()
    {
        #region Variable Declarations
        WinTreeItem uIProductionSearchTreeItem = this.UICATSEnergyNorthDakotWindow.UIItemWindow.UIItemTree.UIProductionSearchTreeItem;
        WinEdit uITxtboxEntityIdentifiEdit = this.UICATSEnergyNorthDakotWindow.UIFactoryProdSearchWindow.UITxtboxEntityIdentifiWindow.UITxtboxEntityIdentifiEdit;
        WinButton uISearchButton = this.UICATSEnergyNorthDakotWindow.UIFactoryProdSearchWindow.UIToolStrip1ToolBar.UISearchButton;
        WinCell uIItem133001011821Cell = this.UICATSEnergyNorthDakotWindow.UIFactoryProdSearchWindow.UIListofEntitiesFoundwWindow.UIListofEntitiesFoundwTable.UIPdenSelectionrow1Row.UIItem133001011821Cell;
        #endregion

        Obj.prodSearch_ND();
        test = Obj.WinTitle;

        // Click 'Production Search' tree item
        Mouse.Click(uIProductionSearchTreeItem, new Point(74, 5));

        // Type '133001011821' in 'txtboxEntityIdentifier' text box
        //uITxtboxEntityIdentifiEdit.Text = this.RecordedMethod1Params.UITxtboxEntityIdentifiEditText;
        uITxtboxEntityIdentifiEdit.Text = test;

        // Click 'Search' button
        Mouse.Click(uISearchButton, new Point(25, 13));

        // Double-Click '133001011821' cell
        Mouse.DoubleClick(uIItem133001011821Cell, new Point(53, 4));
    }

2 个答案:

答案 0 :(得分:0)

您没有在method1实例上执行Class1,因此test1属性具有默认值,即null。要更改Class1实例的状态,您应该调用method1

public class Class2
{    
    Class1 Obj = new Class1();

    public string test2;

    public void method2()
    {
        // at this point Obj has default value of test1 property
        Obj.method1(); // change state of Obj
        test2 = Obj.test1; // now test1 has value "xyz" assigned
    }    
}

注意:在C#中,我们将PascalCase用于公共成员和类型名称。我们还将camelCase用于私有成员和本地变量名称。

如果您希望Class1默认情况下将“xyz”分配给test1,则应在Class1的构造函数中指定此值。

答案 1 :(得分:0)

我不明白你为什么要这样,但你可以像这样改变方法2

public void method2()
{
  Obj.method1();
  test2 = Obj.test1;
}

<强>更新

你写了

RecordedMethod1()正在调用Obj.prodSearch_ND();并且inturn prodSearch_ND()尝试调用RecordedMethod1()。这就是抛出上述错误。

如果我理解正确,则意味着问题与此项目具有相同的名称 - 堆栈溢出。 prodSearch_ND()尝试调用RecordedMethod1()并在RecordedMethod1()内部再次调用prodSearch_ND()。它是无限递归导致堆栈ovwerflow。