无法从另一个 class 访问方法
Can't access a method from another class
已经回答了许多类似的问题,但他们的解决方案并没有帮助我理解如何解决我的问题。这就是为什么我决定问。
我有一个 class GSMCallHistoryTest 方法:
public void DisplayCallHistory()
{
Console.WriteLine(theGSM.PrintCallHistory());
}
我还有一个 class GSM,带有构造函数、属性和 PrintCallHistory() 方法:
public string PrintCallHistory()
{
StringBuilder printCallhistory = new StringBuilder();
foreach (Call call in this.callHistory)
{
printCallhistory.Append(call.ToString());
}
return printCallhistory.ToString();
}
我在 MAIN 中尝试做的是调用 DisplayCallHistory() 方法:
DateTime testCallDate1 = new DateTime(2015, 03, 15, 17, 50, 23);
DateTime testCallDate2 = new DateTime(2015, 03, 15, 20, 20, 05);
DateTime testCallDate3 = new DateTime(2015, 03, 17, 11, 45, 00);
var callHistory = new List<Call>
{
new Call(testCallDate1, 0889111111, 5),
new Call(testCallDate2, 0889222222, 10),
new Call(testCallDate3, 0889333333, 3)
};
GSM theGSM = new GSM("MODEL", "MANUFACTURER", callHistory);
theGSM.DisplayCallHistory(); // <<<< Problem
问题是:
Task_1___GSM.GSM' does not contain a definition for 'DisplayCallHistory'
and no extension method 'DisplayCallHistory' accepting a first argument of type
'Task_1___GSM.GSM' could be found (are you missing a using directive
or an assembly reference?)
这似乎是一个非常常见且明显的错误,但我不知道如何修复它。我能想到两件事:
将我想调用的方法设为静态(然后修复这可能导致的大量问题)。
从 GSMCallHistoryTest 创建一个对象。但这对我来说似乎不对。将来自另一个 class 的对象与另一个对象一起使用,以使用第一个对象的方法!?这不可能行得通。
你说 GSMCallHistoryTest
有一个函数 DisplayCallHistory
()
但是你在 GSM
上调用函数 DisplayCallHistory
(),它没有那个函数,它有 PrintCallHistory()
阅读完您在回复 Rufus L 的评论中提到的任务列表后,也许这就是您应该做的事情:
//Task 1 - Create GSMCallHistoryTest class
public class GSMCallHistoryTest
{
private GSM theGSM;
public void DisplayCallHistory()
{
//Create the calls
DateTime testCallDate1 = new DateTime(2015, 03, 15, 17, 50, 23);
DateTime testCallDate2 = new DateTime(2015, 03, 15, 20, 20, 05);
DateTime testCallDate3 = new DateTime(2015, 03, 17, 11, 45, 00);
var callHistory = new List<Call>
{
new Call(testCallDate1, 0889111111, 5),
new Call(testCallDate2, 0889222222, 10),
new Call(testCallDate3, 0889333333, 3)
};
//Tasks 2 and 3 - Create instance of theGSM and add the calls
theGSM = new GSM("MODEL", "MANUFACTURER", callHistory);
//Task 4 - Display information about the calls
Console.WriteLine(theGSM.PrintCallHistory());
}
}
然后您可以从您的应用程序的主要方法中调用它
var callHistoryTest = new GsMCallHistoryTest();
callHistoryTest.DisplayCallHistory();
已经回答了许多类似的问题,但他们的解决方案并没有帮助我理解如何解决我的问题。这就是为什么我决定问。 我有一个 class GSMCallHistoryTest 方法:
public void DisplayCallHistory()
{
Console.WriteLine(theGSM.PrintCallHistory());
}
我还有一个 class GSM,带有构造函数、属性和 PrintCallHistory() 方法:
public string PrintCallHistory()
{
StringBuilder printCallhistory = new StringBuilder();
foreach (Call call in this.callHistory)
{
printCallhistory.Append(call.ToString());
}
return printCallhistory.ToString();
}
我在 MAIN 中尝试做的是调用 DisplayCallHistory() 方法:
DateTime testCallDate1 = new DateTime(2015, 03, 15, 17, 50, 23);
DateTime testCallDate2 = new DateTime(2015, 03, 15, 20, 20, 05);
DateTime testCallDate3 = new DateTime(2015, 03, 17, 11, 45, 00);
var callHistory = new List<Call>
{
new Call(testCallDate1, 0889111111, 5),
new Call(testCallDate2, 0889222222, 10),
new Call(testCallDate3, 0889333333, 3)
};
GSM theGSM = new GSM("MODEL", "MANUFACTURER", callHistory);
theGSM.DisplayCallHistory(); // <<<< Problem
问题是:
Task_1___GSM.GSM' does not contain a definition for 'DisplayCallHistory'
and no extension method 'DisplayCallHistory' accepting a first argument of type
'Task_1___GSM.GSM' could be found (are you missing a using directive
or an assembly reference?)
这似乎是一个非常常见且明显的错误,但我不知道如何修复它。我能想到两件事:
将我想调用的方法设为静态(然后修复这可能导致的大量问题)。
从 GSMCallHistoryTest 创建一个对象。但这对我来说似乎不对。将来自另一个 class 的对象与另一个对象一起使用,以使用第一个对象的方法!?这不可能行得通。
你说 GSMCallHistoryTest
有一个函数 DisplayCallHistory
()
但是你在 GSM
上调用函数 DisplayCallHistory
(),它没有那个函数,它有 PrintCallHistory()
阅读完您在回复 Rufus L 的评论中提到的任务列表后,也许这就是您应该做的事情:
//Task 1 - Create GSMCallHistoryTest class
public class GSMCallHistoryTest
{
private GSM theGSM;
public void DisplayCallHistory()
{
//Create the calls
DateTime testCallDate1 = new DateTime(2015, 03, 15, 17, 50, 23);
DateTime testCallDate2 = new DateTime(2015, 03, 15, 20, 20, 05);
DateTime testCallDate3 = new DateTime(2015, 03, 17, 11, 45, 00);
var callHistory = new List<Call>
{
new Call(testCallDate1, 0889111111, 5),
new Call(testCallDate2, 0889222222, 10),
new Call(testCallDate3, 0889333333, 3)
};
//Tasks 2 and 3 - Create instance of theGSM and add the calls
theGSM = new GSM("MODEL", "MANUFACTURER", callHistory);
//Task 4 - Display information about the calls
Console.WriteLine(theGSM.PrintCallHistory());
}
}
然后您可以从您的应用程序的主要方法中调用它
var callHistoryTest = new GsMCallHistoryTest();
callHistoryTest.DisplayCallHistory();