■18258 / ) |
Re[1]: 複数の型のインデクサ |
□投稿者/ シャノン (399回)-(2008/05/12(Mon) 14:05:03)
|
■No18257 (tanaka さん) に返信 > 複数の型のパラメータをインデクサを使用して処理してますが > この場合取得した側で型変換させているのでこれをclass TESTの方で対処して > 取得する側はそのまま使用するにはどのようにすればいいのでしょうか。 > > 【例】 > class TEST > { > // [0]:int > // [1]:byte > // [2]:char > // [3]:string > private static object[] Parameter = new object[4]; > public string this[int index] > { > get{return Parameter[index];} > set{Parameter[index] = value;} > } > } > > class Main > { > void Main() > { > int a; > byte b; > char c; > string d; > > TEST param = new TEST(); > a = Convert.ToInt32(param[0]); > b = Convert.ToByte(param[1]); > c = Convert.ToChar(param[2]); > d = Convert.ToString(param[3]); > > // 以下のように処理したい > // a = param[0]; > // b = param[1]; > // c = param[2]; > // d = param[3]; > } > }
型が違うものをインデクサで返したり、文字列でないものを文字列で返したりする設計はお勧めできません。 例えば、こういうわけにはいかんのですか? インデクサであることは必須ですか?
class Value { public int Integer{ get; set; } public byte Byte{ get; set; } public char Char{ get; set; } public string String{ get; set; } }
class Test { public Value Value { get; set; } }
class Program { void Main() { Test test = new Test(); Value value = test.Value; int a = value.Integer; byte b = value.Byte; char c = value.Char; string d = value.String; } }
|
|