returning void from a Function

M

Mark Kamoski

Hi Everyone.

Please help.

Is there a way to return void from a Function?

That is, what are the ramifications of writing something like the
following...


Private Function DoSomething()

''''''''''' code

Return Nothing

End Function


Is there any advantage to doing this?

What if there is no return type stated and the Return is left off entirely?
What happens then? Are there any implications?

(What I want to do here is to both explicitly state that there is no return
value and at the same time to use a Function rather than a Sub.)

Please advise.

Thank you.

--Mark.
 
M

Marina

If you return Nothing, that will simply be returning a null reference, as
opposed to a reference to an object. There is nothing wrong with that. You
just need to make sure that whoever is calling this function, knows to check
that the return value is not Nothing before trying to call a method or a
property on the object.
 
J

Jay B. Harlow [MVP - Outlook]

Mark,
Void is a C#, C++, Java term, it is not available in VB.NET. Void is no
where near Nothing. Nothing is the same as C#, C++, Java's null.

As Mattias suggested start using Option Strict On at the top of your source
files, you will find that your function needs a return type, your's has a
return type of Object.

To have a 'void' return type you need to use a Sub.

' make Option Strict On compatible
Private Function DoSomething() As Nothing

''''''''''' code

Return Nothing

End Function

' a Void Function
Private Sub DoSomething()
Return
End Sub

Yes, coming from those other languages, its a little odd having two keywords
to define a function, however that's the rules. ;-)

Hope this helps
Jay
 
T

Tom Spink

I think you've answered your own question in your previous post, make a
Sub!!

in C, when you declare a method as returning a void, you are essentially
doing the same in VB when you make a Sub:

Public Sub MyFunc(Param1 As Integer)
....
End Sub

void MyFunc( int Param1 )
{
...;
}

--
Happy to help,
-- Tom Spink
([email protected])

" There's no place like 127.0.0.1 "

Please respond to the newsgroup,
so all can benefit.


One Day,
 
J

Jerry

A function must specify a return type.
If you don't know what "type" you will be returning you need to return an
"object" and then do a late binding to it to read the return value as
whatever you are doing with it.
I get a sneaking suspicion that you might be better off redesigning your
idea using a "Sub" and passing your parameters as referenced objects. You
will be able to set flags as well stating what the result of your Sub was.
The trouble is that a function can only have one return value. Of course
you can pass parameters by reference to it and change their values from
within the function as you see fit, but this would be confusing code.
Functions should return values and not change the value of parameters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top