2016-10-18 5 views
0

はコードで自己いる場合、そのクラス内のC#の

class Class1 
{ 
    public string compute (string no1, string no2) 
    { 
     string computed; 
     if (Convert.ToInt32(no1) - 8 == 0) 
     { 
      if (Convert.ToInt32(no2) > 0) 
      { 
       computed = (60/Convert.ToInt32(no2)).ToString(); 
      } 
      if (Convert.ToUInt32(no2) == 0) 
      { 
       computed = "0"; 
      } 
     } 
     if (Convert.ToInt32(no1) - 8 != 1) 
     { 
      if (Convert.ToInt32(no2) > 0) 
      { 
       computed = ((Convert.ToInt32(no1) - 8) + (60/Convert.ToInt32(no2)).ToString(); 
      } 
      if (Convert.ToInt32(no2) == 0) 
      { 
       computed = ((Convert.ToInt32(no1) - 8)).ToString(); 
      } 
     } 
     return computed; 
    } 
} 

アイブ氏はまた、メインフォームで直接コードを使用しようとしましたが、私はそれを使用傾けますCommandTextで

  connect.Open(); 
      OleDbCommand command = new OleDbCommand(); 
      command.Connection = connect; 
      command.CommandText = "update Table1 set ot='"+ computed +"' where Fname = '" + uname+"' "; 
      command.ExecuteNonQuery(); 
      connect.Close(); 
      MessageBox.Show("Save Complete"); 

も同じエラーが発生しました。

+1

問題は、 '文字列computed'がそのif文の試合の非場合に割り当てられることはありませんということです。 –

+0

この行の最後に 'computed'(Convert.ToInt32(no1) - 8)+(60/Convert.ToInt32(no2))。ToString(); 'がなくなっています – Hank

+0

'var computed = string.Empty;'は空の値を返し、エラーはなくなります。 'computed'に何も割り当てられていなければエラーを返すべきですか? – Equalsk

答えて

1

してみます= String.Emptyを計算string computed = String.Empty;

+0

行終端記号がありません –

+0

ありがとう、私は自分の投稿を編集しました。 – Basvo

0

設定した文字列と、例えば計算された文字列を検証するためにstring.IsNullOrWhiteSpaceを使用

if (!string.IsNullOrWhiteSpace(computed)) 
 
      { 
 
       connect.Open(); 
 
       OleDbCommand command = new OleDbCommand(); 
 
       command.Connection = connect; 
 
       command.CommandText = "update Table1 set ot='" + computed + "' where Fname = '" + uname + "' "; 
 
       command.ExecuteNonQuery(); 
 
       connect.Close(); 
 
       MessageBox.Show("Save Complete"); 
 
      }