Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Problem with the Invoke Method

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dom - 28 Feb 2008 22:41 GMT
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
Marc Gravell - 28 Feb 2008 23:10 GMT
... 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
Jon Skeet [C# MVP] - 28 Feb 2008 23:26 GMT
> I'm getting a "Parameter Count mismatch error" with the following.
>
[quoted text clipped - 19 lines]
>
> 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}));

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Dom - 29 Feb 2008 15:54 GMT
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

> > I'm getting a "Parameter Count mismatch error" with the following.
>
[quoted text clipped - 36 lines]
>
> - Show quoted text -
Jon Skeet [C# MVP] - 29 Feb 2008 16:05 GMT
> Thanks to everyone for this.  I used Control.Invoke (Job, (object) new
> object [] {...})
[quoted text clipped - 7 lines]
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Rene - 28 Feb 2008 23:40 GMT
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);

> I'm getting a "Parameter Count mismatch error" with the following.
>
[quoted text clipped - 21 lines]
>
> Dom

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.