Results 1 to 6 of 6

Thread: Why does Sub work but not function

  1. #1

    Thread Starter
    Hyperactive Member greg_quinn's Avatar
    Join Date
    Nov 2002
    Location
    South Africa
    Posts
    366

    Why does Sub work but not function

    I have a form in my app named frmMain

    In the form I have a Sub

    Public Sub mySub()
    End Sub

    and a function

    Public Funtion GetValue() as Long
    GetValue = 100
    End Function


    Now when I call mySub from another form, I do something like

    Dim frM as frmMain

    frmMain.mySub()

    And that works fine..

    But why can't I get the return value of the GetValue() function?

    i.e

    Dim frM as frmMain
    Dim myValue as Long

    myValue = frmMain.GetValue()

    It returns an error, object reference not set to instance of an object...

    Thanks

  2. #2
    Lively Member
    Join Date
    Jun 2004
    Posts
    74
    Just a guess without actually trying it, but try this instead...

    Dim frm as New frmMain
    Dim myValue as Long

    myValue = frm.GetValue()

    Now you are calling the method of the frm object (of frmMain type). Before you did not actually have an object created to call the method. I'm not sure why the Sub should allow this behavior in the first place...

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Why does Sub work but not function

    Originally posted by greg_quinn

    And that works fine..
    No it doesn't.
    I don't live here any more.

  4. #4
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    I would also suggest you get in the habit of using return statements. Since VB.NET includes them now, they tend to make the code more read friendly.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Why does Sub work but not function

    Originally posted by greg_quinn
    I have a form in my app named frmMain

    In the form I have a Sub

    Public Sub mySub()
    End Sub

    and a function

    Public Funtion GetValue() as Long
    GetValue = 100
    End Function


    Now when I call mySub from another form, I do something like

    Dim frM as frmMain

    frmMain.mySub()

    And that works fine..

    But why can't I get the return value of the GetValue() function?

    i.e

    Dim frM as frmMain
    Dim myValue as Long

    myValue = frmMain.GetValue()

    It returns an error, object reference not set to instance of an object...

    Thanks
    Sorry but you are all screwed up here (I think )

    It looks as though your frmMain is NOT the startup object of your project. In that case;

    Dim frM as frmMain should be

    Dim frM as New frmMain

    and frmMain.mySub should be

    frM.mySub

    mySub will have to be Public and so will myValue.

    Try that and see what happens.

    By the way, I agree with wossname, none of your code can possibly work
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6

    Thread Starter
    Hyperactive Member greg_quinn's Avatar
    Join Date
    Nov 2002
    Location
    South Africa
    Posts
    366
    Sorry, I must admit I didn't go through my post after I wrote it.

    Adding the New to the Form declaration helps everything.

    (Although I swear I tried that and it didn't work before



    But then again its been a long day!!)

    Thanks for the help

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