Object of type 'System.Int32' cannot be converted to type 'System.Object[]'.
Is The Error I am Getting In c#.
I need Immediate Remedy Please
class SomeClass
{
public delegate void SomeDel ( params object[] Params_list );
public void SomeFunc(params object[] Params_List)
{
// Updates Value Of Some Treeview According To Value Recieved
}
public void SomeThreadFunc()
{
SomeDel a ;
a = SomeFunc ;
object[] Params_List = new object[1];
Params_List.setvalue(0,1);
this.invoke( a, Params_List); // Here Is Where Comes The Error
// Althou i Have Tried new object[] {1}
// but with no respite
}
}
It Compiles Well But While Runing It Creates Problems
what am i doing wrong how to go about it
Thankyou
RIGHT_THEN
AlanPosted Jan 30, 2008, 5:09 AM
Well, the 'standard' solution here would be:
this.Invoke(a, new object[]{Params_List});
because the second parameter of Invoke is an object array of parameters to the 'a' delegate and the first (and only) such parameter is itself an object array.
However, the reason why the following also works:
this.Invoke(a, (object)Params_List);
is because the second parameter to Invoke is a parameter array and you can therefore supply it with a list of object references (in this case just one) rather than an object array reference. Moreover, because all types inherit from object then (somewhat paradoxically) even an object[] can be cast to an object!
tot2ivnPosted Feb 29, 2008, 3:04 AM
Totti
RightthenPosted Feb 1, 2008, 10:35 AM
Sir Mr. Dr. Spack
You were Right!!! Althou i had placed and checked the code before too
but when it gets multithreaded control remains in the primary thread
while debugging unless we place a breakpoint in secondary thread too
so i placed try and catcth in secondary thread too
and there the culprit was found. Your Diagnostic about the situation was
right and comes as a lesson to me that debugging mulithreaded application
could be problemetic.
i think i will have to make all those mistakes again
just to be sure if they actually were mistakes since the bug has been found.
That i had not discarded the right things for wrong at some other place.
Regards and million thanks
Gratefully
RIGHT_THEN
Dr SpackPosted Jan 31, 2008, 10:48 AM
I think that the Invoke call is just working
and your function (SomeFunc) does throw that
exception.
So I would set a breakpoint in that function and debug it.
Or add a try-catch block to check that out.
Dr SpackPosted Jan 30, 2008, 3:23 PM
it took me a while, but now I've got it. ;)
Thank you!
Dr SpackPosted Jan 30, 2008, 4:50 AM
that is a strange thing, but I have a solution:
this.Invoke( a, (object)Params_List );
Just add (object) and it will work! Why? I don't know. Maybe someone else does know an explanation for that!?