- PR -

動的配列

投稿者投稿内容
Beginner
会議室デビュー日: 2004/02/06
投稿数: 17
投稿日時: 2004-02-06 16:05
今、構造体と動的配列を使ってテキストに書いた文字をボタンを押すとListBoxに、ListBoxで項目をダブルクリックすると構造体のデータをテキストに戻すというものを作っています。
そこで配列の要素が決定できないので動的配列を使いたいのですがどのようにしたらよいのかわかりません。

public struct TSeiseki
{
public int Eigo, Kokugo, Sugaku;
}

public struct TSeito
{
public string Neme;
public TSeiseki Seiseki;
}

int index; //ここのあたりの宣言、動的配列の使用法がわかりません。
TSeito[] seito = new TSeito[index]; //

private void button1_Click(object sender, System.EventArgs e)
{

index = listBox1.Items.Count;
TSeito[] seito = new TSeito[index + 1];

seito[index].Neme = textBox1.Text;
seito[index].Seiseki.Eigo = Convert.ToInt32(textBox2.Text);
seito[index].Seiseki.Kokugo = Convert.ToInt32(textBox3.Text);
seito[index].Seiseki.Sugaku = Convert.ToInt32(textBox4.Text);

listBox1.Items.Add(textBox1.Text);
}

private void listBox1_DoubleClick(object sender, System.EventArgs e)
{
int selectIndex = listBox1.SelectedIndex;

textBox1.Text = seito[selectIndex].Neme;
textBox2.Text = seito[selectIndex].Seiseki.Eigo.ToString();
textBox3.Text = seito[selectIndex].Seiseki.Kokugo.ToString();
textBox4.Text = seito[selectIndex].Seiseki.Sugaku.ToString();
}
よろしくお願いします。
Jitta
ぬし
会議室デビュー日: 2002/07/05
投稿数: 6267
お住まい・勤務地: 兵庫県・海手
投稿日時: 2004-02-06 16:31
System.Collections.ArrayList

詳細はMSDN参照のこと


設計に出しゃばれば、テストの科目が増えたらどうなります?
Beginner
会議室デビュー日: 2004/02/06
投稿数: 17
投稿日時: 2004-02-06 17:57
>System.Collections.ArrayList
>詳細はMSDN参照のこと

一応調べてみましたかいまいち使用の仕方がわかりません。
使用例をあげていただくとうれしいです。

>設計に出しゃばれば、テストの科目が増えたらどうなります?

まだ、入門レベルなので、文法の練習とでも言いますか、サンプルを作って
動かしてみようと思ったレベルなのです・・・

ご指導よろしくお願いします。
きくちゃん
ぬし
会議室デビュー日: 2003/08/01
投稿数: 854
お住まい・勤務地: 都内某所
投稿日時: 2004-02-06 18:16
レコードさん、こんばんは。

引用:

>System.Collections.ArrayList
>詳細はMSDN参照のこと

一応調べてみましたかいまいち使用の仕方がわかりません。
使用例をあげていただくとうれしいです。


ArrayList の概要に使用例、ありませんでした?
Beginner
会議室デビュー日: 2004/02/06
投稿数: 17
投稿日時: 2004-02-06 20:59
一応調べて途中までやってみましたが
構造体のTSeitoをArrayListに入れるのは、こんな感じでしょうか?

static void Main()
{
Application.Run(new Form1());
}

public struct TSeiseki
{
public int Eigo, Kokugo, Sugaku;
}

public struct TSeito
{
public string Neme;
public TSeiseki Seiseki;
}

int index;

TSeito seito = new TSeito();
ArrayList myAL = new ArrayList();

private void button1_Click(object sender, System.EventArgs e)
{
myAL.Add(seito.Neme = textBox1.Text);
myAL.Add(seito.Seiseki.Eigo = Convert.ToInt32(textBox2.Text));
myAL.Add(seito.Seiseki.Kokugo = Convert.ToInt32(textBox3.Text));
myAL.Add(seito.Seiseki.Sugaku = Convert.ToInt32(textBox4.Text));

listBox1.Items.Add(textBox1.Text);
}

private void listBox1_DoubleClick(object sender, System.EventArgs e)
{
//登録された名前、点数をArrayListから読み出し方がわかりません。
//textBox1.Text =
//textBox2.Text =
//textBox3.Text =
//textBox4.Text =
}

よろしくお願いします。
He
大ベテラン
会議室デビュー日: 2002/12/18
投稿数: 141
投稿日時: 2004-02-06 21:29
seito[selectIndex].Neme
seito[selectIndex].Seiseki.Eigo.ToString();

といった具合に値を取り出したいのであれば、
seito[n]にTSeitoのインスタンスが入っている必要がありますよね。
というわけで、myALにAddするのは、TSeitoのインスタンスです。

レコードさんが最初の書き込みでされていたような
「インスタンスを配列に入れてから、値をセット」でもできますが、
わたしのおすすめは
「値がセットされたインスタンスを、配列に入れる」やり方です。
(キャストとか面倒なので。)

--以下、追加。--
すいません。
いまよくみたら、
レコードさんは「インスタンスを配列に入れてから、値をセット」してるわけじゃありませんでした。
↓これが
TSeito[] seito = new TSeito[index + 1];
↓こうみえてました
seito[index] = new TSeito();


[ メッセージ編集済み 編集者: He 編集日時 2004-02-06 21:37 ]
Beginner
会議室デビュー日: 2004/02/06
投稿数: 17
投稿日時: 2004-02-06 21:50
皆様ご返信ありがとうございます。

>といった具合に値を取り出したいのであれば、
>seito[n]にTSeitoのインスタンスが入っている必要がありますよね。
>というわけで、myALにAddするのは、TSeitoのインスタンスです。

大変申し訳ないですが、なにぶん未熟者なので、
私のコードを少々変更して実例を挙げていただくとありがたいです。

お手数だと思いますがぜひお願いいたします。
He
大ベテラン
会議室デビュー日: 2002/12/18
投稿数: 141
投稿日時: 2004-02-06 23:58
TSeitoのインスタンスを作る実例です。
コード:
TSeito seito = TSeito;


次に、TSeitoのインスタンスをmyALにAddする実例です。
コード:
myAL.Add(seito);


最後に、myALからTSeitoのインスタンスを取り出す実例です。
Itemプロパティの型に注意してください。
コード:
TSeito seito = (TSeito)myAL[n];



【参考】
C# プログラマーズ リファレンス(例2が参考になります)
ArrayList.Item プロパティ

# VBっぽく書いてた個所を修正

[ メッセージ編集済み 編集者: He 編集日時 2004-02-07 01:19 ]

スキルアップ/キャリアアップ(JOB@IT)