Exception message

jpdbay

Active member
Joined
Feb 22, 2006
Messages
31
Programming Experience
Beginner
Hi buddies,

I'm working on a MDI application. I'm gettting this exception message "Exception has been thrown by the target of an invocation". It appears when I try to call a function or procedure in child form which is active using toolbar button. Below is the code in MDI frorm:

Private Sub tBar_mdi_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles tBar_mdi.ButtonClick

Call Me.ToolBarButtonProc(tBar_mdi.Buttons.IndexOf(e.Button))

End Sub

Public Sub ToolBarButtonProc(ByVal IndexNo As Integer)

Dim FRMNAME = ActiveMdiChild

If Me.ActiveMdiChild Is Nothing Then
Exit Sub
End If

Try
If IndexNo = 0 Then
Call FRMNAME.cmdAdd()

ElseIf IndexNo = 1 Then
Call FRMNAME.cmdModify()

ElseIf IndexNo = 2 Then
Call FRMNAME.cmdView()

ElseIf IndexNo = 3 Then
Call FRMNAME.cmdDelete()

ElseIf IndexNo = 4 Then
Call FRMNAME.cmdPrint()

ElseIf IndexNo = 5 Then
Call FRMNAME.cmdSave()

ElseIf IndexNo = 6 Then
Call FRMNAME.cmdCancel()

ElseIf IndexNo = 7 Then
Call FRMNAME.cmdRead()

ElseIf IndexNo = 8 Then
Call FRMNAME.cmdWrite()

ElseIf IndexNo = 9 Then
Me.Close()

End If

Catch Exp As Exception

MsgBox(Exp.Message, MsgBoxStyle.Information, "Index Error")

End Try
Exit Sub
End Sub

Please guide me why I'm getting this error message and correct me since I'm newbie this forum as well as in Vb.net. It would be kind enough if they is sample code or comments from you ppl.

Looking forward for your prompt reply.

Thnx n rgds,
jpdbay
 
You're using late binding there, so I'd get rid of that for a start. It may be that you're calling a method of a form that doesn't exist. You haven't even declared the FRMNAME variable as Form. You've left it without a type which means it defaults to Object. That you turn Option Strict On, then you cast the ActiveMdiChild property to the type that you believe it is. The compiler will then tell you whether you're calling an invalid member. Yo should also get the call stack when the exception was thrown. You can use the StackTrace property of the exception or else call its ToString method, which gives you the Message and StackTrace properties combined.
 
Hi jmcilhinney,

Thak you for prompt reply. Can you explain what is late binding? I even tried to declare FRMNAME as Form but still I cant call the function in the active chils form. I went through Multiple Forms examples and it's very useful yet I dont know how to implement StackTrace in my application.

Thank you,

jpdbay
 
Late binding occurs when you don't know what type an object is until run time. You have not specified the type of the FRMNAME variable so it defaults to Object. The Object class does not have all those methods you are callingso at run time your app has to check what type it is and then check whether it has that method. Using early binding you would specify the type at design time and the compiler will pick up whether you are calling an invalid member before you ever get to run your app. I suggest that you read about Option Strict and late binding in the help or on MSDN online.

As for StackTrace, it's a member of the Exception class, just like Message. You can put it in place of Exp.Message or, better still, call ToString on the exception and, as I already said in my last post, it will give you the Message and the StackTrace.
 
Back
Top