|
-
Jul 1st, 2004, 08:53 AM
#1
Thread Starter
Hyperactive Member
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
-
Jul 1st, 2004, 09:05 AM
#2
Lively Member
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...
-
Jul 1st, 2004, 09:40 AM
#3
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.
-
Jul 1st, 2004, 10:01 AM
#4
Frenzied Member
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.
-
Jul 1st, 2004, 10:11 AM
#5
PowerPoster
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.
-
Jul 1st, 2004, 11:09 AM
#6
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|