我有一個叫methods.cs
的類包含兩種方法。如何使方法在同一個類中調用另一個方法?
例如method1
和method2
我要讓method2
呼叫method1
代碼澄清:
public static void Method1()
{
// Method1
}
public static void Method2()
{
// Method2
}
我要讓Method2
電話Method1
。我怎樣才能做到這一點?
我有一個叫methods.cs
的類包含兩種方法。如何使方法在同一個類中調用另一個方法?
例如method1
和method2
我要讓method2
呼叫method1
代碼澄清:
public static void Method1()
{
// Method1
}
public static void Method2()
{
// Method2
}
我要讓Method2
電話Method1
。我怎樣才能做到這一點?
也許我失去了從你的問題的東西,但它應該是這樣簡單:
public static void Method1()
{
}
public static void Method2()
{
Method1();
}
public static void Method2()
{
// Method2
Method1();
}
這幾乎是完全按照你剛纔問同樣的問題:
How to make method call another one in classes C#?
但是...
public class MyClass
{
public static void Method1()
{
// Method1
}
public static void Method2()
{
Method1();
}
}
很高興見到你求助!爲了在另一個Method中調用Method,它包含在同一個Class中非常簡單。只是叫它的名字! Here is a nice little tutorial on Methods和我的例子在下面!
public class ClassName
{
// Method definition to call in another Method
public void MethodToCall()
{
// Add what you want to be performed here
}
// Method definition performing a Call to another Method
public void MethodCalling()
{
// Method being called. Do this by using its Method Name
// be sure to not forget the semicolon! :)
MethodToCall();
}
}
祝你好運,希望這有助於!
同意,你剛剛提出的兩個問題強烈建議一個非常新的初學者,應該諮詢初學者的內容。 – Gjeltema 2013-04-26 01:00:14
你在用什麼C#書?您應該退還並要求退款。 – 2013-04-26 01:11:46