2013-04-11 113 views
0
private void button3_Click_1(object sender, EventArgs e) 
{ 
    textBox2.Text = "$" + (textBox1.Text = string.Format(System.Globalization.CultureInfo.GetCultureInfo("us-US"), "{0:#,##0.00}", double.Parse(textBox1.Text))); 

    string digits, temp; 
    long numberVal; 

    string[] powers = new string[] { "Thousand ", "Million " }; 
    string[] ones = new string[] { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; 
    string[] tens = new string[] { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" }; 
    string[] currency = new string[] { "Dollars" }; 
    string wordValue = ""; 

    if (numberVal == 0) 
     return "Nol"; 
    if (numberVal < 0) 
    { 
     wordValue = "negative "; 
     numberVal = -numberVal; 
    } 

    long[] partStack = new long[] { 0, 0, 0, 0 }; 
    int partNdx = 0; 

    while (numberVal > 0) 
    { 
     partStack[partNdx++] = numberVal % 1000; 
     numberVal /= 1000; 
    } 

    for (int i = 3; i >= 0; i--) 
    { 
     long part = partStack[i]; 
     if (part >= 100) 
     { 
      wordValue += ones[part/100 - 1] + " Ratus "; 
      part %= 100; 
     } 

     if (part >= 20) 
     { 
      if ((part % 10) != 0) wordValue += tens[part/10 - 2] + 
       " " + ones[part % 10 - 1] + " "; 
      else wordValue += tens[part/10 - 2] + " "; 
     } 
     else if (part > 0) wordValue += ones[part - 1] + " "; 

     if (part != 0 && i > 0) wordValue += powers[i - 1]; 
    } 

    textBox3.Text = return wordValue; 

} 

我想將一個數字與長類型轉換爲字符串單詞。c#返回功能

爲什麼我的函數返回一個錯誤,我沒有正確使用它或其他東西?

+0

你想說什麼?你在期待什麼? – 2013-04-11 12:45:44

+0

簡單地說'textBox3.Text = wordValue;'? – Mark 2013-04-11 12:45:49

+0

請發佈確切的錯誤消息。 – Aaron 2013-04-11 12:45:54

回答

10

在C#中,return關鍵字用於從函數返回值。在你的情況下,你沒有返回任何東西,但是你想爲某個屬性賦值,這是完全不同的。

刪除return關鍵字。

編輯
看來你複製粘貼&一些代碼,並沒有真正理解它做什麼。您將代碼從一個數字轉換爲其文本表示並將該文本表示形式返回爲文本的功能。

您現在嘗試在事件處理程序中使用此代碼,直接將結果分配給文本框。這樣的作品,如果你通過

{ 
    textbox3.Text = "..."; 
    return; 
} 

更換任何return "..."語句乾淨的解決方案將是將代碼複製到一個功能,並正確地使用它。


實施例爲需要一個函數return關鍵字:

string SayHello(string user) 
{ 
    return "Hello " + user; 
} 

示例的方法,其不返回值,但可使用return被中斷:

void DoSomething() 
{ 
    if (!myConditionIsMet) 
     return; 

    DoSomethingElse(); 
} 

作業示例不能使用使用return關鍵字:

string name = "Brad"; 
name = name + " Pitt"; 
2

試試這個:

//textBox3.Text = return wordValue; 
textBox3.Text = wordValue; 
+0

它的工作,但我應該這樣做︰if(numberVal == 0)return「Nol」; – 2013-04-11 12:49:14

+0

你做'if(numberVal == 0){textBox3.Text =「NOL」;返回; }'。當您複製代碼的功能返回數字字符串時,您現在嘗試將其直接分配給文本框。這意味着:如果'numberVal == 0'需要退出事件處理程序,而不是返回'Nol',則需要將該文本分配給文本框。 – 2013-04-11 12:49:58

+0

由於你的方法是「無效的」,返回值將僅僅是idnore在這裏 - 一種錯誤 – duDE 2013-04-11 12:50:46

3
textBox3.Text = return wordValue; 

是無效的C#代碼。

寫類似:

textBox3.Text = wordValue; 

,沒有回報,accoridng函數定義:void,沒有什麼可以回報。

0

這是不正確的 textBox3.Text = return wordValue;

使其如下

textBox3.Text = wordValue;

由於該函數是無效的,沒有什麼可以返回的。

0

您的方法button_click事件的方法簽名包含void返回類型。

這意味着該方法不打算返回任何東西。

類似地,return語句不能作爲賦值語句的一部分放置(不能將其置於'='的右側)。

正如其他人所說的,您將通過簡單地刪除return關鍵字來得到您要查找的結果。您還需要刪除return聲明是進一步在你的代碼,在檢查後爲numberval == 0

需要返回的地方是它的方法頭部指示返回類型(比void其他任何東西)。在這些方法中,您在自己的語句開頭使用return來返回後面的值或對象。

例如:

/* In a method somewhere */ 
int foo = Bar(); 

/* The Bar method */ 
public int Bar() 
{ 
    /* Whatever code runs */ 
    return 3; //return an object or value whose type matches the type the method signiture says you return 
}