Hi Jon, Daniel and William
Thank you very much
Here is my test code
Haobi
using System
using System.Drawing
using System.Collections
using System.Windows.Forms
using System.Data
namespace DrawTes
public class Form1 : System.Windows.Forms.For
public ArrayList alDataSets = new ArrayList()
private System.Windows.Forms.Button button1
private void button1_Click(object sender, System.EventArgs e
float expData1,expData2
Random rand = new Random()
expData1 = (float)rand.NextDouble()
expData2 = (float)rand.NextDouble()
DataSets dataset1 = new DataSets();
dataset1.fData = expData1
dataset1.strDesc = "X"
alDataSets.Add(expData1)
DataSets dataset2 = new DataSets();
dataset2.fData = expData2
dataset2.strDesc = "Y"
alDataSets.Add(expData2)
label1.Text = alDataSets.Count.ToString()
DataSets dataset3
dataset3= null
dataset3 = (DataSets)alDataSets[0]; //get "System.InvalidCastException
string strFirst
strFirst = dataset3.strDesc
label2.Text = strFirst
DataSets dataset4
dataset4 = (DataSets)alDataSets[1]; //get "System.InvalidCastException
string strSecond
strSecond = dataset4.strDesc
label3.Text = strSecond
//the base clas
using System
namespace DrawTes
public class DataSet
public DataSets(
private float arData
private string desc
public string strDes
get{return desc;
set{desc = value;
public float fDat
get{return arData;
set{arData = value;
----- Jon Skeet [C# MVP] wrote: ----
Haobin <an*******@discussions.microsoft.com> wrote
Thanks for your replies
I modified my code based on your replies. It passed compilation but
got "InvalidCastException" in runtime at the step 1 in Jon's repl
and at the line "so = (SomeObject)aryOfObjects[a];" in Daniel'
reply. What could be wrong
I appreciate your help
Basically exactly what the exception says: you're trying to cast
something which isn't an instance of your class
Could you post a short but complete program which demonstrates th
problem
See
http://www.pobox.com/~skeet/csharp/complete.html for details o
what I mean by that
--
Jon Skeet - <sk***@pobox.com
http://www.pobox.com/~skee
If replying to the group, please do not mail me to