VB.NET - How to call a module in vb.net - Asked By Chuan Cheng Foo on 19-Apr-07 03:09 AM

Hi, I would like to know how to call a module in vb.net?


 I received this warning : TypeInitializeException was unhandled. The type initializer for 'SAPWVA.basDetail' threw an exception.


Below is my code.... Can anyone pls tell me whats wrong with my code?


Many thanks.


frmDetail :

Private Sub InitializeScreen()

With Me

gScrMode.Value = "INI"

.fraKey.Enabled = True

.lblStatus.Text = ""

.txtCustCode.Text = ""

...

..

.

Call basDetail.MenuControl(Me, 0, 0, 0, 0, 0, 0) ---warning shown at this line

.fraDetail.Enabled = True

.fraDetail.Visible = False

System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default

End With

End Sub

-------------------------------------------------------

basDetail :

Module basDetail

Dim GetDate As String

Dim StdRate As Decimal

Dim ctlMonth As Short

...

..

.

Dim vHexMth As New VB6.FixedLengthString("{0,-12}", "123456789ABC") 'YYH

'YYH -Amendment on Doc Ref, eg 0210 = 02A

Sub MenuControl(ByRef objForm As Object, ByRef V1 As Boolean, ByRef V2 As Boolean, ByRef V3 As Boolean, ByRef V4 As Boolean, ByRef V5 As Boolean, ByRef V6 As Boolean)

With objForm

.mnuCreate.Visible = V1

.mnuChange.Visible = V2

.mnuDelete.Visible = V3

.mnuSave.Visible = V4

.mnuCancel.Visible = V5

.mnuPrint.Visible = V6

End With

End Sub

Is it nothing? - dan scan replied to Chuan Cheng Foo on 19-Apr-07 09:53 AM

Did you make sure that the basDetail is not nothing?  Have you tried to step through the code putting break points around the code causing the problem.  Next try to step through the menucontrol initialization.  Try putting try catchs around the code and examine the exception thrown. 

Good luck

Declare the Sub as Public - Lucas Bussey replied to Chuan Cheng Foo on 19-Apr-07 02:54 PM

A module isn't a class. If you change the Sub MenuControl(...) statement to include the Public keyword:

Public Sub MenuControl(...)

That should expose that subroutine to your project. You won't need to reference the module name, just the function name.

Hope this helps!

How to call a module in vb.net - Chuan Cheng Foo replied to Lucas Bussey on 19-Apr-07 08:52 PM

Hi, thanks for the reply...but that did not solve the problem.

There's one guy telling me this :
you're calling the method like this:
basDetail.MenuControl(Me, 0, 0, 0, 0, 0, 0)

and the method accepts this:
Sub MenuControl(ByRef objForm As Object, ByRef V1 As Boolean, ByRef V2 As Boolean, ByRef V3 As Boolean, ByRef V4 As Boolean, ByRef V5 As Boolean, ByRef V6 As Boolean)

so you need to call is like this:
basDetail.MenuControl(Me, True, True, True, True, True, True)

----------------------------------------------------------
But that also did not help...the warning is still shown at the same line.
What else could be the problem?...please help.

Many thanks.

ByRef should be ByVal - Lucas Bussey replied to Chuan Cheng Foo on 19-Apr-07 11:05 PM
I believe I see the error now.

Change all the Boolean declarations to ByVal instead of ByRef. ByRef is looking for a variable of type Boolean (kind of like a pointer). ByVal should allow you to pass a contant value to that function.

As a side note: if you specify an Integer type contant when you call a function that is looking for a Boolean type, it will implicitly typecast it to a Boolean. This should be avoided to make debugging easier.
Its still not working... - Chuan Cheng Foo replied to Lucas Bussey on 20-Apr-07 04:32 AM

Ive tried your way...but its still the same....the same warning appear at the same line of code.

I wonder what exactly is wrong with my code.


Please help.

Many thanks.

Something to try - Lucas Bussey replied to Chuan Cheng Foo on 20-Apr-07 08:42 AM
The possibility exists that the error is occurring INSIDE your subroutine, and the exception is being raised where it is being called because it recognizes that it cannot be executed.

Something to try: change MenuControl(ByRef objForm As Object, ByRef V1 As Boolean, ByRef V2 As Boolean, ByRef V3 As Boolean, ByRef V4 As Boolean, ByRef V5 As Boolean, ByRef V6 As Boolean)
to
MenuControl(ByRef objForm As frmDetail, ByRef V1 As Boolean, ByRef V2 As Boolean, ByRef V3 As Boolean, ByRef V4 As Boolean, ByRef V5 As Boolean, ByRef V6 As Boolean)

I've highlighted the change in red.

Other than this, I'm really not sure. If I can think of anything else I will post. Otherwise, lets hope someone else can shed some light on this.
calling module sub - john replied to Lucas Bussey on 24-Mar-10 08:58 AM
sample module :

Public Sub load()


With Form1.listbox1.Items
.add(newitem)

End

 

Sub


example form :


Private


Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'call module within form'
Call module1.load()
End Sub