Problem with the Invoke Method

D

Dom

I'm getting a "Parameter Count mismatch error" with the following.

At the class level:
delegate void lvwCallback (object[] Args);

Within a method of the same class:
SQLDataReader r = ...
int TopCount = 100;
lvwCallback Job = new lvwCallback(ShowCurrentRecords);
lvwCurrentRecords.Invoke(Job, new object[] { r,
TopCount } );

And then another method in the same class:
private void ShowCurrentRecords(object[] Args)
{
SqlDataReader r = (SqlDataReader)Args[0];
int TopCount = (int)Args[1];
...
}

The error occurs on the "lvwCurrentRecords.Invoke" line above.

What's happening?

Dom
 
M

Marc Gravell

.... OK; what is "lvwCurrentRecords"? you haven't shown anything of
this in the code.

This *sounds* like there might be a "params object[]" on one of the
methods; when using Invoke etc it can be easy to end up passing an
array (length 1), whose only contents are the array (length whatever)
that you intedend as th eindividual arguments.

If you could include a few more of the missing bits?

I also worry about the SQLDataReader; a simple typo, but it probably
means that the code you have posted is not the code that is failing at
runtime, mainly because it doesn't even compile...

Marc
 
J

Jon Skeet [C# MVP]

Dom said:
I'm getting a "Parameter Count mismatch error" with the following.

At the class level:
delegate void lvwCallback (object[] Args);

Within a method of the same class:
SQLDataReader r = ...
int TopCount = 100;
lvwCallback Job = new lvwCallback(ShowCurrentRecords);
lvwCurrentRecords.Invoke(Job, new object[] { r,
TopCount } );

And then another method in the same class:
private void ShowCurrentRecords(object[] Args)
{
SqlDataReader r = (SqlDataReader)Args[0];
int TopCount = (int)Args[1];
...
}

The error occurs on the "lvwCurrentRecords.Invoke" line above.

What's happening?

The compiler thinks that you're trying to call a method using two
parameters: r, TopCount.

Change it to:

lvwCurrentRecords.Invoke(Job, new object[]{new object[]{r, TopCount}});

Alternatively:

lvwCurrentRecords.Invoke(Job, (object) (new object[] {r, TopCount}));
 
R

Rene

Is the "params" argument that is throwing things off.

public object Invoke(Delegate method, **params** object[] args);

When you pass an array to a "params" argument it will try to take apart the
array and make each entry on the array a different argument. So you're kind
of saying to the compiler:

lvwCurrentRecords.Invoke(Job, r, TopCount);
 
D

Dom

Thanks to everyone for this. I used Control.Invoke (Job, (object) new
object [] {...})

Can someone clean up my mental image. How are arrays stored. For
example, consider this:

object[] x = new object [3];

is "x" a single variable on the stack that is pointing to three
objects on the heap? Or are there 3 variables on the stack, each
pointing to a single object on the heap?

And then, just to make things more complicated, what if I do the
following:

x[0] = new int [3];

It gets a little confusing at this point.

Dom


Dom said:
I'm getting a "Parameter Count mismatch error" with the following.
At the class level:
        delegate void lvwCallback (object[] Args);
Within a method of the same class:
            SQLDataReader r = ...
            int TopCount = 100;
            lvwCallback Job = new lvwCallback(ShowCurrentRecords);
            lvwCurrentRecords.Invoke(Job, new object[] { r,
TopCount } );
And then another method in the same class:
        private void ShowCurrentRecords(object[] Args)
        {
            SqlDataReader r = (SqlDataReader)Args[0];
            int TopCount = (int)Args[1];
            ...
         }
The error occurs on the "lvwCurrentRecords.Invoke" line above.
What's happening?

The compiler thinks that you're trying to call a method using two
parameters: r, TopCount.

Change it to:

lvwCurrentRecords.Invoke(Job, new object[]{new object[]{r, TopCount}});

Alternatively:

lvwCurrentRecords.Invoke(Job, (object) (new object[] {r, TopCount}));

--
Jon Skeet - <[email protected]>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
World class .NET training in the UK:http://iterativetraining.co.uk- Hide quoted text -

- Show quoted text -
 
J

Jon Skeet [C# MVP]

Dom said:
Thanks to everyone for this. I used Control.Invoke (Job, (object) new
object [] {...})

Can someone clean up my mental image. How are arrays stored. For
example, consider this:

object[] x = new object [3];

is "x" a single variable on the stack that is pointing to three
objects on the heap? Or are there 3 variables on the stack, each
pointing to a single object on the heap?

x is a single variable pointing to an array on the heap. The array in
turn has three slots, each of which can contain a reference. Each
reference is then either null or a reference to an object.-
And then, just to make things more complicated, what if I do the
following:

x[0] = new int [3];

It gets a little confusing at this point.

At that point, the reference within the array is a reference to another
array, which has 3 slots but this time each slot contains an int.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top