已经回答了许多类似的问题,但他们的解决方案并没有帮助我理解如何解决我的问题。这就是我决定问的原因。 我有一个类 GSMCallHistoryTest ,其方法为:
public void DisplayCallHistory()
{
Console.WriteLine(theGSM.PrintCallHistory());
}
我还有一个带有构造函数,属性和PrintCallHistory()方法的 GSM 类:
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创建一个对象。但这对我来说似乎不对。使用另一个类中的对象和另一个对象来使用第一个中的方法!?这不可行。
答案 0 :(得分:4)
您说GSMCallHistoryTest
有一个函数DisplayCallHistory
()
但是你在DisplayCallHistory
上调用了GSM
()函数,该函数没有该函数,它有PrintCallHistory()
答案 1 :(得分:0)
在阅读了你在回复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();