因此,我正在撰寫 ac# 程式,其中客戶端從服務器接收字符,并根據接收到的字符運行后臺作業程式。但問題是按鈕狀態并非所有代碼路徑都回傳一個值:
private object SL_Click(object sender, EventArgs e)
{
try
{
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
tcpclnt.Connect(RecieveIP.Text, 8001); // use the ipaddress as in the server program
MessageBox.Show("Connected");
Console.Write("Enter the string to be transmitted : ");
String str = Console.ReadLine();
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
MessageBox.Show("Transmitting.....");
stm.Write(ba, 0, ba.Length);
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
for (int i = 0; i < k; i )
Console.Write(Convert.ToChar(bb[i]));
string toattk = k.ToString();
if (toattk == "g")
{
googlesearch.RunWorkerAsync();
}
else if (toattk == "y")
{
ysearch.RunWorkerAsync();
}
else if (toattk == "a")
{
aolsearch.RunWorkerAsync();
}
else if (toattk == "yo")
{
youtubesearch.RunWorkerAsync();
}
else if (toattk == "s")
{
ssearch.RunWorkerAsync();
}
tcpclnt.Close();
}
catch (Exception)
{
MessageBox.Show("Could not find server computer.");
}
}
它說 SLCLick - 并非所有代碼路徑都回傳一個值。這似乎很奇怪,因為每個 if then 陳述句都有一個動作?非常感謝。
uj5u.com熱心網友回復:
該方法需要回傳物件
private object SL_Click(object sender, EventArgs e){
.....
return whatObject
}
或將回傳型別更改為void
private void SL_Click(object sender, EventArgs e){
.....
}
uj5u.com熱心網友回復:
這似乎很奇怪,因為每個 if then 陳述句都有一個動作?
這不是編譯器所抱怨的。事實上,可以撰寫if什么都不做的陳述句:
if(someVariable == someValue){
} else {
}
這是完全合法的 C#;“并非所有代碼路徑都回傳一個值”不是關于您的方法在各種機會中呼叫其他方法
訊息的出現是因為您在這里做出了承諾:
private object SL_Click
^^^^^^
"I promise to return something"
而你還沒有實作它;宣告某種型別物件的方法將被回傳,必須在其主體代碼中的某處具有return ...something here....
“這里的東西”必須是(可轉換為)您在方法頭中承諾的型別的型別
你不能:
public string MyMethod(){. //I promise to return a string
return 1; //not a string, not implicitly convertible to a string
}
此外,通過代碼,通過所有 if 和 else 等必須沒有可檢測的路線,這意味著 C# 可以到達方法的末尾而不會碰到return ...something here...
public string MyMethod(){ //I promise to return a string
if(DateTime.Today.DayOfWeek == DayOfWeek.Monday){
return "Hello";
}
}
This also gives "not all paths return a value" - you have to completely and explicitly tell C# what to do in every case; you can't just leave it to decide itself what to return if it's Tuesday
If you don't plan to return anything, declare the method void:
public void MyMethod(){
if(DateTime.Today.DayOfWeek == DayOfWeek.Monday){
return; //I don't work Mondays
}
}
You can still quit early in a void method, by doing a return without a value after it. Also you can let C# reach the end without encountering any return - C# knows what to do if it reaches the end of a void method without having encountered a return, but for anything non-void you have to be explicit about what value to return on every possible path through the code
In your case I'd say it should be a void method. Try also to avoid returning object - use a more specific type if you can.
We'll get to Task-returning methods some other time :)
Small additional, because it's bound to come up for you soon- how do you return more than one value? In C# you can only ever return one thing. If you want to return multiple values, you're free to make something that holds multiple values, make one of those things and put your multiple values inside it.
That one thing could be an array, a dedicated class, some built in convenient like a a tuple or anonymous type.. but all in remember you can only return one thing; if you have lots of values to return, put them inside one thing and return that.
It's like working in a bar and needing to carry 10 different drinks on one hand; you put the drinks on a tray and carry the tray.
uj5u.com熱心網友回復:
我認為這是一個事件處理程式,如果您仔細觀察,您的方法需要回傳一個物件private object SL_Click(object sender, EventArgs e)。
如果可以,則將方法的回傳型別從更改object為void
private object SL_Click(object sender, EventArgs e)
try如果您無法更改方法簽名,那么您可以在and的末尾簡單地回傳任何物件(前提是您不需要回傳任何內容),catch或者添加一個finally塊并回傳一個虛擬物件。
finally
{
return "Event Handled";
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/453411.html
上一篇:any()和if陳述句背后的邏輯
