2013-06-24 27 views
0

我有Option Strict On。當它開啓時,下面的代碼不起作用。我能因爲該行沒有從文本將值更改爲十進制,我的代碼的其餘部分不工作,我的標籤顯示$ 0到隔離問題,以這條線tryParse不轉換值

Decimal.TryParse(lblAnnualMid.Text, decAnnualMid) 

我該如何解決這個問題。我仍然感覺通過VB.Net,所以如果這看起來很明顯,請原諒我。

這裏是我的代碼的其餘部分:

Option Strict On 
Option Explicit On 

Public Class frmCustomRanges 

    'Variables for the MidPoint TextBoxes 
    Dim decAnnualMid As Decimal 
    Dim decHourlyMid As Decimal 

Private Sub txtRangeSpread_TextChanged(sender As Object, e As EventArgs) Handles txtRangeSpread.TextChanged 

     'This event runs when the user enters a number in the 
     'txtRangeSpread text box. The amount is converted to a decimal 
     'it then calculates the min and max of the range for annual 
     'and hourly ranges. The ranges change as the user changes the range 
     'spread. 


     'Variables for the event 
     Dim decRangeSpreadResults As Decimal 

     'Verify entry is numeric 
     If IsNumeric(txtRangeSpread.Text) Then 

      'Convert entry to decimal 
      Dim decRangeSpread As Decimal = Convert.ToDecimal(txtRangeSpread.Text) 

      'convert the Mid point value to decimal 
      Decimal.TryParse(lblAnnualMid.Text, decAnnualMid) 

      'convert range spread value to percentage 
      decRangeSpreadResults = decRangeSpread/100 


      'Display results in dollar string Annual salary 
      lblAnnualMin.Text = Convert.ToDecimal(decAnnualMid - (decRangeSpreadResults * decAnnualMid)).ToString("C") 
      lblAnnualMax.Text = Convert.ToDecimal(decAnnualMid + (decRangeSpreadResults * decAnnualMid)).ToString("C") 

      ''Display results in dollar string in Hourly rate 
      lblHourlyMin.Text = Convert.ToDecimal(decAnnualMid + (decRangeSpreadResults * decAnnualMid)/52/40).ToString("C") 
      lblHourlyMax.Text = Convert.ToDecimal(decAnnualMid + (decRangeSpreadResults * decAnnualMid)/52/40).ToString("C") 

     Else 

      MsgBox("You have entered a non-numeric value. Please check value and enter again", vbCritical, "Input Error") 


      With txtRangeSpread 
       .Text = "" 
       .Focus() 

      End With 

     End If 


    End Sub 
+0

什麼是您的輸入看上去像個? –

+0

輸入是一個2位數字,即:35 –

+0

程序開始處的'lblAnnualMid.Text'中有什麼?它在啓動時是否有默認值? – Tim

回答

0

你想要的其實應該是這樣

decimal.TryParse("$35,000",NumberStyles.Currency ,null , out test) 

例如:

using System; 
    using System.Globalization; 

    namespace ConsoleApplication1 
    { 
     class Program 
     { 
      static void Main(string[] args) 
      { 
       decimal test; 
       Console.WriteLine(decimal.TryParse("$35,000",NumberStyles.Currency ,null , out test)); 
       Console.WriteLine(test); 
       Console.Read(); 
      } 
     } 
    } 

more information about NumberStyles

0

我改變了我的行:

Decimal.TryParse(lblAnnualMid.Text.Replace("$", ""), decAnnualMid) 

這個工作完美。

謝謝大家。

+0

你應該用'if Decimal.TryParse()'來檢查返回值,這樣你的代碼就可以判斷它的工作與否,並決定如何繼續。 –

+0

@KenWhite:嘗試解析返回默認值,如果失敗,有時這就是你想要的。但是你是對的,那看起來並不是那個時候所期望的,IsNumeric應該可能被tryparse替代。 – jmoreno

+0

@jmoreno - TryParse返回一個布爾值,指示解析是否成功。 –