Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sir i have use the following code for converting arraylist to array, my array list consist of int values of bezier points code is as below:

C#
SqlCommand inkcmd;
cn = new SqlConnection("Data Source=localhost;Initial Catalog=amit;Integrated Security=True;Persist Security Info=True;Packet Size=4096");
cn.Open();
inkcmd = new SqlCommand("SELECT BPX,BPY FROM BezierTable where Alphabet= @Alphabet", this.sqlConnection1);
SqlParameter param = new SqlParameter();
param.ParameterName = "@Alphabet";
param.Value = txtImage.Text;
inkcmd.Parameters.Add(param);
inkcmd.Connection = cn;
                 
ArrayList rowList = new ArrayList();
rdr = inkcmd.ExecuteReader();
while (rdr.Read())
{
    object [] values = new object[rdr.FieldCount];
    rdr.GetValues(values);
    rowList.Add(values);
}
                      
Point[] y = (Point)rowList.ToArray(typeof(Point[]));/// here it throws error throws System.InvalidCastException: At least one element in the source > array could not be cast down to the destination array type.
            
Stroke newStroke = inkOverlay.Ink.CreateStroke(y);
newStroke.Move(2000,300);



with regards

Googlejumbo
Posted
Updated 30-Jan-11 3:31am
v2

To be able to cast an object to a Point Structure[^], the object most be created as a Point Structure.

You have an array of object, possibly of type Int32, that's not something you can cast to an array of Point.

Regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-Jan-11 12:49pm    
That is correct - my 5.
OP may need an extra explanation.
--SA
Sergey Alexandrovich Kryukov 30-Jan-11 13:02pm    
So I added my suggestion as a new answer.
A teamwork?
--SA
Espen's answer is correct. Now, suggestions:

The class ArrayList is obsolete, as it was created before generics (v.2.0, and all the versions of .NET Framework prior to 2.0 are practically useless, so you practically need to use v.2.0 or later in all new development).
Also, the method ArrayList.ToArray with Type parameters should be too slow without a good purpose, because it needs to check type on every element (exactly what caused a fail in your code).

Instead, always use System.Collection.Generic.List<> with the type you really need, or other types from the name space <code>System.Collection.Generic.

Good luck,
—SA
 
Share this answer
 
Comments
Espen Harlinn 30-Jan-11 13:29pm    
Yes, nice teamwork :)
Hi
Please check your array list values. May be elements type in the array list is different.

Thanks
 
Share this answer
 
The answers above are correct, but don't actually explain this specific exception.

Currently the Type of the values returned by SqlDataReader.GetValues() at compile-time and runtime is unknown, so by default it is the lowest common denominator, Object. The actual type depends on (a) the type of the column in the table in the database; (b) the type to which SqlDataReader translates the column's data type. Let's assume the sql column is SqlInt32 and SqlDataReader is converting it to Int32 (aka int).

Once your loop is done you are left with rowList that looks something like this (let's say there are 4 rows):

ArrayList 
{
    object[] { 1, 2 },
    object[] { 3, 4 },
    object[] { 5, 6 },
    object[] { 7, 8 },
}


Although we know that those values are ints, if you were to check their types they are actually boxed ints. (see Boxing)

Now let's look at your code:

Point[] y = (Point)rowList.ToArray(typeof(Point[]));


This will be run first:

rowList.ToArray(typeof(Point[]))


So you're asking it to take each element of the ArrayList and convert it to Point[], then return an array, which will be Point[][]. That poses a few problems. There is no implicit or explicit conversion from object[] to Point[]. Regardless, I can't think of a way to convert { 1, 2 } to an array of points, can you? I think you meant:

rowList.ToArray(typeof(Point));


but even that won't work because there is no conversion from object[] to Point.

Even if that were successful and returned Point[], you are then explicitly casting the result to Point and then assigning it to Point[] again.

Here's how I'd write it:

var HappyFunPoints = new List<point>();
rdr = inkcmd.ExecuteReader();
while (rdr.Read())
{
    var x = rdr.GetInt32(0);
    var y = rdr.GetInt32(1);
    var p = new Point(x, y);
    HappyFunPoints.Add(p);
}

foreach(var p in HappyFunPoints)
    // Do something with your points here.</point>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900