Click to See Complete Forum and Search --> : Calling A Function


Simonkale
June 18th, 2001, 05:58 AM
I have a function "CheckModifyState" in a module:

private Function CheckModifyState(byval ExitMsg as string, byval MsgReturn as Integer) as Boolean
'set the message text
ExitMsg = "The document has been modified, do you want to save it?"

'check if the content of the current document has been modified or not
If SendMessage(rtfText.hwnd, EM_GETMODIFY, 0, 0) = true then
'if true then show a messagebox asking if he would like to save the doc.
MsgReturn = MsgBox(ExitMsg, vbQuestion + vbYesNoCancel, "Note Editor")
'if Yes then save the document
If MsgReturn = vbYes then
mnuFileSaveAs_Click
'if cancel then return to application
ElseIf MsgReturn = vbCancel then
CheckState = false
else
Exit Function
End If
'if the document has not been modified then return true
else
CheckState = true
End If
End Function



And this function is called by mnuFileNew_OnClick event

private Sub mnuFileNew_Click()
Dim ModifyState as Boolean
'check if the current document has been modified or not
ModifyState = CheckModifyState(byval ExitMsg, byval MsgReturn)
'if modified then ask to save the document
If ModifyState = true then
'open a new file
rtfText.SelStart = 0
rtfText.SelLength = len(ActiveControl.Text)
rtfText.Text = ""
me.Caption = "Note Editor - [Untitled]"
End If
End Sub



When I click "New" menu, it returns compiler error: Variable not defined
And highlights "ExitMsg"

ModifyState = CheckModifyState(byval ExitMsg, byval MsgReturn)



What could be the problem? Isn't this how to call a function or do I have to define variable before calling?
Like this;

Dim ExitMsg as string
Dim MsgReturn as Integer
Dim ModifyState as Boolean
'check if the current document has been modified or not
ModifyState = CheckModifyState(byval ExitMsg, byval MsgReturn)



Other questions;
Is there any limitation of number of modules one can have in a project?
Is it a good idea to have a modules for a Sub procedure or function called from more than two places?
What is the best situation in which one should have a module?

Thanks
Simon

Cimperiali
June 18th, 2001, 06:07 AM
change
private Function CheckModifyState
to
public Function CheckModifyState

Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.

Cimperiali
June 18th, 2001, 06:10 AM
Moreover, in calling funtion, do not code "byval"
Dim ExitMsg as string
Dim MsgReturn as Integer
Dim ModifyState as Boolean
'check if the current document has been modified or not
ModifyState = CheckModifyState(ExitMsg, MsgReturn)

Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.

Cimperiali
June 18th, 2001, 06:20 AM
>>Is there any limitation of number of modules one can have in a project?
you may ahe a lot of them. But it would be better not to excede, or you will find hard to find out matters...Usually, I make a module for Api calls, one for constants and variables which need to be pubblic, one for functions or sub used in many places

>>Is it a good idea to have a modules for a Sub procedure or function called from more than two places?
It should be. One module for this should be enough, but you may decide to have more to find easily what you need (ie: one for ScreenFunctons, one for MathFunctions, and so on)

>>What is the best situation in which one should have a module?
Exactly as you said, when a procedure is shared among different places (forms or classes)


Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.

Simonkale
June 18th, 2001, 08:27 AM
I'm confused about these things;
Public VS Private and
ByVal VS ByRef

I think when I need a subroutine within a form then I can use "Private" but when I need to invoke it from another form then I should use "Public". Am I correct?

But what about ByVal and ByRef? When should I use ByVal and ByRef?

Simonkale
June 18th, 2001, 08:47 AM
It is still not working. It returns:
Expected variable or procedure, not module
error message.

Is it possible to have more than one subroutine in a single module? If Yes then, is it a good idea?

John G Duffy
June 18th, 2001, 10:42 AM
CheckModifyState is expecting two variables to be passed to it so the calling routine must supply these variales. In your example,ExitMsg and ExitReturn will need to be defined in the mnuFileNew_Click event or someplace accessable to it.
ByVal vs ByRef
When Sub A calls Sub B passing an argument BYRef,VB will pass the actual location of the Argument to Sub B.
When Sub A calls Sub B using BYVal, VB will pass Sub B a COPY of the argument. Sub B can change the argument to anything it wants, but using ByVal, Sub is changing only the copy not the actual argument passed by Sub A. If you want to have Sub B's changes reflected back to Sub A, then you need to use BYREF, passing the actual location of SUB A's variable.
Two examples below. The first demonstrates using ByRef. The second BYVal. The only difference is the BYVal / ByREf.

' Sample 1
'
private Sub Command1_Click()
Dim A
A = "ABCDEFG"
Call MySub(A)
MsgBox A
End Sub

public Sub MySub(byref someARG)
someARG = "HIJKLMNOP"
End Sub
'
'
' Sample two
private Sub Command1_Click()
Dim A
A = "ABCDEFG"
Call MySub(A)
MsgBox A
End Sub

public Sub MySub(byval someARG)
someARG = "HIJKLMNOP"
End Sub




I should also point out that in VB 6 the default is ByREF but that is all changing in VB.NET (VB 7)
there are no defaults in VB.NET

John G

Simonkale
June 19th, 2001, 04:40 AM
I'm running out of patience. It is still not working and return error message:
Expected variable or procedure, not module

Here is the "CheckModifyState" function in a module

public Function CheckModifyState(byval ExitMsg as string, byval MsgReturn as Integer) as Boolean
'set the message text
ExitMsg = "The document has been modified, do you want to save it?"
'check if the content of the current document has been modified or not
If SendMessage(rtfText.hwnd, EM_GETMODIFY, 0, 0) = true then
'if true then show a messagebox asking if he would like to save the doc.
MsgReturn = MsgBox(ExitMsg, vbQuestion + vbYesNoCancel, "Note Editor")
'if Yes then save the document
If MsgReturn = vbYes then
mnuFileSaveAs_Click
'if cancel then return to application
ElseIf MsgReturn = vbCancel then
CheckModifyState = false
else
Exit Function
End If
'if the document has not been modified then return true
else
CheckModifyState = true
End If
End Function



Here is "mnuFileNew_Click" event code:

private Sub mnuFileNew_Click()
Dim ExitMsg as string
Dim MsgReturn as Integer
Dim ModifyState as Boolean
'check if the current document has been modified or not
ModifyState = CheckModifyState(ExitMsg, MsgReturn)
'if modified then ask to save the document
If ModifyState = true then
'open a new file
rtfText.SelStart = 0
rtfText.SelLength = len(ActiveControl.Text)
rtfText.Text = ""
me.Caption = "Note Editor - [Untitled]"
End If
End Sub



What is the problem here? Why VB returns:
Expected variable or procedure, not module?

Thanks,
Simon

John G Duffy
June 19th, 2001, 07:40 AM
Listen to what the message is telling you. Is your module the same name as a sub? If yes, change the Module name to something else. It can be anything. VB doesn't care and you will never call a module. You call the subroutines in the module.
You can have multiple Subs in a single module. Modules are used when you have common Subs, Functions and Variables that are used by mode than one routine.

John G

Simonkale
June 19th, 2001, 09:27 AM
Oops..I never realised that, can't have save module name as function in it.
But there is another problem.
I have called a subproc from the module:
mnuFileSaveAs_Click
Now the module doesn't recognise it. How do I call main form's code (i.e events) from module? Like in this case,
mnuFileNew_Click and rtfText.hwnd

Cimperiali
June 19th, 2001, 09:35 AM
nameofyourform.mnuFileNew_Click
ie:
Form1.mnuFileNew_Click
or
FMainform.mnuFileNew_Click
(look at name of your form. "Name", not "Caption"!)

Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.