Hi:
I've run into a runtime exception with trying to use a .Net Delegate. I'm using MS VisualStudio 2008.
I'm essentailly trying to wrap access to a datagridview control with a delegate. Upon entering the method in question, I test to see whether an Invoke is required or not. If it is, I wrap the my method call with a delegate. Upon calling the delegated method, I get a runtime exception. I've used this exact method of invoking in other programs, and it has worked fine. For the life of me, I can't seem to figure what the issue is here.
It appears to be throwing the exception on the 2nd parameter. It is of type string and gets cast to an object[].
Things I've tried:
------------------
1) Remove the offending 2nd (string) parameter, and everything works fine.
2) Remove the 1st (int) parameter, and everything works fine.
Here is the exception I get:
--------------------------
--
System.ArgumentException: Object of type 'System.Object[]' cannot be converted to type 'System.String'.
at System.Windows.Forms.Contr
ol.Marshal
edInvoke(C
ontrol caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Contr
ol.Invoke(
Delegate method, Object[] args)
at IntervalData.NStarImporter
.MainForm.
UpdateImpo
rtResultsG
rid(Int32 iMeterID, String sMessage) in C:\Documents and Settings\xxxx\My Documents\Visual Studio 2008\Projects\IDS\Developm
ent\xxx\tr
unk\xxx\Ma
inForm.cs:
line 567}
Here is a code snippet:
-----------------------
delegate void UpdateImportResultsGridDel
egate(int iParam, string message);
public void UpdateImportResultsGrid(in
t iParam, string sMessage)
{
try
{
// Does this require being wrapped by an invoke?
if (this.dataGridViewImportRe
sults.Invo
keRequired
.Equals(tr
ue))
{
UpdateImportResultsGridDel
egate del = new UpdateImportResultsGridDel
egate(Upda
teImportRe
sultsGrid)
;
this.dataGridViewImportRes
ults.Invok
e(del, iParam, new object[] { sMessage });
}
// Do a bunch of updating to the datagridview control
catch (Exception ex)
{
throw ex;
}
}
I've used the exact same construct in other applications, and it has worked fine.
This is what confuses me.
Any and all help will be appreciated.
Thanks,
JohnB
Start Free Trial