2011-07-14 44 views
0

才能一個數據列的值分配給一個變量和以不同的方法使用該變量。 我有以下代碼如何一個DataColumn值分配給一個變量和使用該變量在不同的方法

button_click() 
{ 
create and fill data set as data adapter.fill(data set,"ABC"); 
and then 
int x= data set.tables["ABC"].rows[0]["col name"] ; 
} 

and i have another method button_click2() 
{ 
int y = x; 
} 

我可以這樣做嗎?或者是有什麼辦法可以指定數據set.tables [「ABC」。行[0] [「山口名」]值直接爲x

+0

是的,你可以將一個變量分配給另一個。你嘗試過嗎?問題是什麼? – vittore

+0

這worked.thanx大家 – laila

回答

0

如果您創建變量出方的方法,你就可以在類內的任何地方使用它。

public class Test 
{ 
    private int ColValue {get;set;} //This is a property 

    button_click() 
    { 
      ColValue = Convert.ToInt(dataset.tables["ABC"].rows[0]["col name"].ToString()) ; 
    } 

    button_click2() 
    { 
     int y = ColValue ; 
    } 
} 

如果您使用的是Asp.net,請參閱user608576有關在Viewstate中存儲值的答案。

0

由於您沒有指定,它的Web應用程序或窗口申請我將按照以下方式回答ASP.NET。

在這種情況下,頁面會做備份後您的變量都將丟失。所以只有通過將viewstate/session/pass作爲查詢字符串添加到相同頁面或將其放入某個隱藏控件中,才能保留值。

從ASP.NET頁面 加載到ASP.NET的最受歡迎的方法是將它們存儲在隱藏輸入的視圖狀態中。

你會在ViewState中的項目存儲,如:

ViewState[key] = value; 

And retrieve it like: 

value = ViewState[key] 

OR

Session["username"] = username 

And access it 

String Username = Session["username"]; 

如果其窗口的應用程序,你可以有一個全局變量,將事件之間被保留。

Public class Test{ 

private int x= 0;//this is global here. 

button_click() 
{ 
create and fill data set as data adapter.fill(data set,"ABC"); 
and then 
x= data set.tables["ABC"].rows[0]["col name"] ; 
} 

and i have another method button_click2() 
{ 
int y = x; 
} 

}

+0

我不知道的話題起動了一些關於asp.net – vittore

相關問題