2011-03-28 67 views
0
public static string SEARCH_STRING = "searchkey"; 
string key=Request.QueryString.Get(SEARCH_STRING);  

如何更改上面的代碼,以使SEARCH_STRING使用屬性來訪問(獲取;集;),而不是一個公共變量如何在C#中定義一個屬性,而不是公共變量的

+2

你試圖解決什麼問題?如何使用SEARCH_STRING?你會在下面看到7個答案,都是正確的,但如果你的問題是別的,他們都不會對你有用。詳細描述你的問題。 – 2011-03-28 06:30:40

+0

都是正確的..但是,這裏使用「SEAR」 – vasmay 2011-03-28 07:35:14

回答

0

如果這個變量不會改變,這是更好地利用不斷

public const string SEARCH_STRING = "searchkey"; 

,這裏是你如何讓財產

private static string _searchString = "searchkey"; 
public static string SEARCH_STRING { 
    get { return _searchString; } 
    private set { _searchString = value; } 
} 
0
private static string _searchString = "searchkey"; 
public static string SearchString { 
    get { return _searchString; } 
    set { _searchString = value; } 
} 
0
public static string SEARCH_STRING { get; set; } 
0

試試這個代碼:

class Foo { 
static string m_searchString="searchKey"; 
public static string SEARCH_STRING 
{ 
    get {return m_searchString;} 
    set {m_searchString=value;} 
} 
} 
0

這是你的意思是什麼?或許可以將其設置爲只讀,如果你還打算在運行時加載它的價值...

public static string SEARCH_STRING 
{ 
    get 
    { 
     return "searchkey"; 
    } 
} 
0
public string SEARCH_STRING 
    { 
     get { return search_string; } 
     set { search_string = value; } 
    } 
0
public static string SEARCH_STRING 
    { 
     get; 
     set; 
    } 
0

更多的封裝,使用性能是這樣的:

public string Name 
{ 
    get; 
    private set; 
} 

因此該類的對象只能設置它,其他對象只能讀取它。

相關問題