CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1

    Calling A Function

    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


  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Calling A Function

    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.
    ...at present time, using mainly Net 4.0, Vs 2010



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

  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Calling A Function

    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.
    ...at present time, using mainly Net 4.0, Vs 2010



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

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Calling A Function

    >>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.
    ...at present time, using mainly Net 4.0, Vs 2010



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

  5. #5

    Re: Calling A Function

    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?


  6. #6

    Re: Calling A Function

    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?


  7. #7
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Calling A Function

    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

  8. #8

    Re: Calling A Function

    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


  9. #9
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Calling A Function

    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

  10. #10

    Re: Calling A Function

    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



  11. #11
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Calling A Function

    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.
    ...at present time, using mainly Net 4.0, Vs 2010



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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured