2014-01-24 18 views
-1

我可以從bool中將我的值結果轉換爲常量字符串嗎?新手C#我可以返回從私人布爾的結果值作爲常量字符串?

public const string MState = ""; 

這裏是我的代碼:

private bool GetStateByZipCode(string ZipOrPostalCode, string ST) 
{ 
    try 
    { 
     string strServerName = ConfigurationManager.AppSettings["ServerName"]; 

     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyNGConnect_ConnectionString"].ToString()); 
     SqlCommand cmd = new SqlCommand(); 
     cmd.Connection = con; 
     cmd.CommandText = "ngc_GetStateByZipCode"; 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.AddWithValue("@ZipOrPostalCode", ZipOrPostalCode); 
     cmd.Parameters.AddWithValue("@ST", ST); 
     SqlDataReader reader = storedProcCommand.ExecuteReader(); 
     { 
      while (reader.Read()) 
      { 
       if (reader["ZipOrPostalCode"].ToString().Equals(ZipOrPostalCode) && 
         reader["ST"].ToString().Equals(ST)) 
       { 
        ST = true; 
        break; 
       } 
      } 
      con.Close(); 
      reader.Close(); 

      return ST; 
     } 
    } 
    finally 
    { 

    } 
+0

常量永遠不會改變。你應該在編譯時定義它的值。 – Steve

+1

我不明白這個問題。 **顯然**你不能在一個布爾變量中存儲一個字符串,反之亦然。你能*請*詳細說明你的問題究竟是什麼? –

+0

@HenkHolterman在C#方面,一個'const string'已經很好的定義了。 –

回答

2

一個const不是一個變量,故名。您無法在運行時將值分配給const

0

你不能那樣做。由於常量變量不會更改,因此無法在運行時定義它們。 只能在編譯時定義常量值。此外,我不會建議在字符串中存儲布爾。它堅持使用正確的數據類型的良好做法。

0

我想你真正想要的是這個...

首先,你不能在const運行時間值存儲。

所以更改聲明:

public string MState; 

那麼你不想在你的方法返回bool,而不是返回string

注意:我增加了一些using statments清理它,但它遠非完美。

private string GetStateByZipCode(string ZipOrPostalCode, string ST) 
    { 
     try 
     { 
      string strServerName = ConfigurationManager.AppSettings["ServerName"]; 

      using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyNGConnect_ConnectionString"].ToString())) 
      using (var cmd = new SqlCommand()) 
      { 
       cmd.Connection = con; 
       cmd.CommandText = "ngc_GetStateByZipCode"; 
       cmd.CommandType = CommandType.StoredProcedure; 
       cmd.Parameters.AddWithValue("@ZipOrPostalCode", ZipOrPostalCode); 
       cmd.Parameters.AddWithValue("@ST", ST); 
       using (SqlDataReader reader = storedProcCommand.ExecuteReader()) 
       { 
        while (reader.Read()) 
        { 
         if (reader["ZipOrPostalCode"].ToString().Equals(ZipOrPostalCode) && 
          reader["ST"].ToString().Equals(ST)) 
         { 
          return ST; 
         } 
        } 

       } 
      } 
      finally 
      { 

      } 
     } 
    } 

然後某處指定的MState值,像這樣:

MState = GetStateByZipCode(zipCode, state); 
+1

由於'MState'沒有在常量聲明之外的任何地方使用,所以不可能說出如何在實際生活場景中使用它。 –

+0

@ LasseV.Karlsen我同意......我假設OP想要在某個時間點爲'MState'賦值。所以我添加了要分配的代碼。雖然真正的問題是爲什麼一個方法需要一個狀態和郵政編碼只是爲了吐出一個狀態(可能是同一個笑)。 –

+0

@ LasseV.Karlsen我有一個存儲過程,當用戶將他們的zip輸入到表單中時。它會返回郵政所在的美國州。我希望在代碼的多個位置使用該結果。 –

1

我想你在找什麼是兩個部分......第一是每個人都告訴你......不,你在代碼編譯完成後沒有一些非常糟糕的編碼實踐,不能做const字符串。我不收集這是你真正想問的。我認爲你真正要問的是:我可以將此方法的返回值(或ST,如果您將方法更改爲返回字符串)轉換爲字符串。答案是肯定的。只需在var上使用ToString()。

實施例...

GetStateByZipCode("something", "something").ToString(); 
0

如已經提到的const不是一個變量,它的值是在編譯時定義。然而,你可以做的是定義一個只讀屬性,其值在其類的範圍內定義。在這種情況下,只有該類可以操作它,並且該屬性在其他任何地方像只讀屬性(您想要的而不是const)那樣起作用。

假設我明白你想幹什麼,這是可以做到這樣的:

class Address 
{ 
    private string _state; // the _state field 
    public string State// the State property 
    { 
    get 
    { 
     return _state; 
    } 
    } 
    private void SetState() 
    { 
     if(GetStateByZipCode(string ZipOrPostalCode, string ST)) 
     { 
      _state = "ST"; 
     } 
     else 
     { 
      _state = "Not ST"; 
     } 
    } 

} 

別的地方,你可以使用只讀屬性是這樣的:

class OtherClass 
{ 
    Address add = new Address(); 
    add.SetState(); 
    //Prints the value assigned to State in Class Address 
    Console.Println(add.State); 

    //cannot do this 
    add.State = "Some Value"; 

} 
+0

上面的代碼出現編譯器錯誤。 –

+0

此代碼應該顯示您使用ReadOnly屬性,它並不意味着直接複製並粘貼到您的項目中。我認爲你應該閱讀這個http://msdn.microsoft.com/en-us/library/w86s7x04.aspx –

相關問題