0

データ列の値を変数に割り当てて、この変数を別の方法で使用できますか?私は次のコードを持っています

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;
}

これはできますか?または、データセット.tables["ABC"]。rows[0]["colname"]の値をxに直接割り当てる方法はありますか

4

2 に答える 2

0

Webアプリケーションかウィンドウアプリケーションかを指定していないので、ASP.NETについては以下のように回答します。

この場合、ページはポストバックを実行し、変数は失われます。したがって、値を保持できる唯一の方法は、値をビューステート/セッション/パスにクエリ文字列として同じページに追加するか、非表示のコントロールに配置することです。

あるページの読み込みから次のページの読み込みまでASP.NETで変数を保持するために最もよく選択される方法は、非表示の入力であるビューステートに変数を格納することです。

次のように、ViewStateにアイテムを保存します。

ViewState[key] = value;

And retrieve it like:

value = ViewState[key]

また

Session["username"] = username

And access it 

String Username = Session["username"];

そのWindowアプリケーションの場合、イベント間で保持されるグローバル変数を持つことができます。

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;
}

}

于 2011-07-14T18:44:25.193 に答える
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を使用している場合は、Viewstateでの値の保存に関するuser608576の回答を参照してください。

于 2011-07-14T18:45:46.290 に答える