CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2001
    Posts
    13

    why cannot work?

    hi

    hope some one can help.
    After running the code below i always can this run-time error '91', Object Variable or with block variable not set
    why is that so?


    private function myfn() as integer
    dim i as integer

    i = 1
    if i = 1 then
    fn = 0
    msgbox ("0",)
    else if i = 3 then
    fn = 2
    end if
    end function

    private sub mysub ()
    dim abc as integer

    abc = myfn

    if abc = 1 then
    msgbox "error:", , "debug"
    elseif abd = 2 then
    exit sub
    end if
    end sub




    after 0 was printed out , the error message came out.

    thank you!


  2. #2
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: why cannot work?

    You should always include

    option Explicit



    as the first line in any module, form or class.
    This forces you to declare variables before using them and the compiler would pick up the fact that you have decl;ared your function using the name "myfn" but tried to use "fn" to set it.
    The corrected version is:

    private function myfn() as integer
    dim i as integer

    i = 1
    if i = 1 then
    myfn = 0
    msgbox ("0",)
    else if i = 3 then
    myfn = 2
    end if

    end function




    HTH,
    Duncan


    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

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