473,320 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Integer Functions that return Nothing?

Is it possible to return "nothing" from an Integer function? This seems to
give me "0" rather than "nothing".

Private Function MyFunction() As Integer
Return Nothing

End Function
Feb 17 '06 #1
8 6042

'Nothing' is not a value per se, but a keyword which reprensents the
default value for a given data type.

For integers this is 0, so Return Nothing, is equivalent to Return 0.
hth,
Alan.

Feb 17 '06 #2
gregory_may wrote:
Is it possible to return "nothing" from an Integer function? This seems to
give me "0" rather than "nothing".

Private Function MyFunction() As Integer
Return Nothing

End Function


Since an Integer is not a Reference type, no you can't.

Nothing can only be returned for "pointers"

Chris
Feb 17 '06 #3
So, how could I define this function to return "nothing" or an integer? Is
this possible?

"Chris" <no@spam.com> wrote in message
news:eR**************@TK2MSFTNGP11.phx.gbl...
gregory_may wrote:
Is it possible to return "nothing" from an Integer function? This seems
to give me "0" rather than "nothing".

Private Function MyFunction() As Integer
Return Nothing

End Function


Since an Integer is not a Reference type, no you can't.

Nothing can only be returned for "pointers"

Chris

Feb 17 '06 #4
gregory_may wrote:
So, how could I define this function to return "nothing" or an integer? Is
this possible?

"Chris" <no@spam.com> wrote in message
news:eR**************@TK2MSFTNGP11.phx.gbl...
gregory_may wrote:
Is it possible to return "nothing" from an Integer function? This seems
to give me "0" rather than "nothing".

Private Function MyFunction() As Integer
Return Nothing

End Function


Since an Integer is not a Reference type, no you can't.

Nothing can only be returned for "pointers"

Chris



No, you will have to return a reference type to check for nothing. If
you are trying to catch when something bad happens and can not return a
value like -1 to indicate it, you can throw an exception.

Chris
Feb 18 '06 #5
That's a good idea.

Thanks!

I did find this:
http://nullabletypes.sourceforge.net/

But, not sure I want to tackle it.

"Chris" <no@spam.com> wrote in message
news:e6*************@TK2MSFTNGP15.phx.gbl...
gregory_may wrote:
So, how could I define this function to return "nothing" or an integer?
Is this possible?

"Chris" <no@spam.com> wrote in message
news:eR**************@TK2MSFTNGP11.phx.gbl...
gregory_may wrote:

Is it possible to return "nothing" from an Integer function? This seems
to give me "0" rather than "nothing".

Private Function MyFunction() As Integer
Return Nothing

End Function

Since an Integer is not a Reference type, no you can't.

Nothing can only be returned for "pointers"

Chris



No, you will have to return a reference type to check for nothing. If you
are trying to catch when something bad happens and can not return a value
like -1 to indicate it, you can throw an exception.

Chris

Feb 18 '06 #6
"gregory_may" <None> wrote in message
news:%2******************@TK2MSFTNGP15.phx.gbl...
:
: "Chris" <no@spam.com> wrote in message
: news:e6*************@TK2MSFTNGP15.phx.gbl...
: >
: > gregory_may wrote:
: >>
: >> "Chris" <no@spam.com> wrote in message
: >> news:eR**************@TK2MSFTNGP11.phx.gbl...
: >>
: >>> gregory_may wrote:
: >>>
: >>>> Is it possible to return "nothing" from an Integer function?
: >>>> This seems to give me "0" rather than "nothing".
: >>>>
: >>>> Private Function MyFunction() As Integer
: >>>>
: >>>>
: >>>> Return Nothing
: >>>>
: >>>> End Function
: >>>>
: >>>>
: >>> Since an Integer is not a Reference type, no you can't.
: >>>
: >>> Nothing can only be returned for "pointers"
: >>>
: >>> Chris
: >>
: >>
: >> So, how could I define this function to return "nothing" or
: >> an integer? Is this possible?
: >
: > No, you will have to return a reference type to check for nothing.
: > If you are trying to catch when something bad happens and can not
: > return a value like -1 to indicate it, you can throw an exception.
: >
: > Chris
:
: That's a good idea.
:
: Thanks!
:
: I did find this:
: http://nullabletypes.sourceforge.net/
:
: But, not sure I want to tackle it.
Why not? The nullable type sounds like it may serve your needs. Consider the
following code block (note that this is new to version 2.0 of the
framework):
'------------------------------------------------
Option Strict

Imports Microsoft.VisualBasic
Imports System

Public Module [module]
Public Sub Main()

Dim n0 As Nullable(Of Integer)

n0 = GetNullValue()
PrintValue(n0)

n0 = GetValue()
PrintValue(n0)

Dim n1 As Integer = CType((GetValue), Integer)
PrintValue(n1)

Dim n2 As Short = CType((GetValue), Short)
PrintValue(n2)

PrintValue(Nothing)

PrintValue(100)

End Sub

Public Sub PrintValue(n As Nullable(Of Integer))

If n.HasValue Then
Console.WriteLine("n = " & n.Value)
Else
Console.WriteLine("n is a null value")
End If

End Sub

Public Function GetValue() As Nullable(Of Integer)
Return 0
End Function

Public Function GetNullValue() As Nullable(Of Integer)
Return Nothing
End Function

End Module
'------------------------------------------------
This generates the following output:
'------------------------------------------------
n is a null value
n = 0
n = 0
n = 0
n is a null value
n = 100
'------------------------------------------------
The earlier suggestion of using -1 or throwing an exception are valid
approaches but exceptions can penalize performance and should be used
sparingly. Exceptions are for exceptional conditions. If what your are
trying to capture with the null value is a normal event, throwing and
catching exceptions may be excessive.
On the other hand, using a value such as -1 to flag something only works
if -1 isn't a valid value otherwise. And it has the additional drawback of
requiring anyone using the function to know the significance of that value
before hand.
If any of these limitations adversely affect what you are trying to do, I'd
say try the nullable type approach and see what it does for you.
HTH
Ralf
--
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.
Feb 18 '06 #7
"Chris" <no@spam.com> schrieb:
Is it possible to return "nothing" from an Integer function? This seems
to give me "0" rather than "nothing".

Private Function MyFunction() As Integer
Return Nothing

End Function


Since an Integer is not a Reference type, no you can't.

Nothing can only be returned for "pointers"


'Nothing' means "default value" for value types, which is 0 for most numeric
types such as 'Integer'. 'Nothing' for value types in VB.NET is the same as
'default(Integer)' in C#.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Feb 18 '06 #8
I think the nullable types could be a perfect fit, but I need to look at
them in a small "test" project to see what I think. If they look good, I
may end using them on all my projects.
"_AnonCoward" <ab****@uvwxyz.com> wrote in message
news:0J******************@tornado.southeast.rr.com ...
"gregory_may" <None> wrote in message
news:%2******************@TK2MSFTNGP15.phx.gbl...
:
: "Chris" <no@spam.com> wrote in message
: news:e6*************@TK2MSFTNGP15.phx.gbl...
: >
: > gregory_may wrote:
: >>
: >> "Chris" <no@spam.com> wrote in message
: >> news:eR**************@TK2MSFTNGP11.phx.gbl...
: >>
: >>> gregory_may wrote:
: >>>
: >>>> Is it possible to return "nothing" from an Integer function?
: >>>> This seems to give me "0" rather than "nothing".
: >>>>
: >>>> Private Function MyFunction() As Integer
: >>>>
: >>>>
: >>>> Return Nothing
: >>>>
: >>>> End Function
: >>>>
: >>>>
: >>> Since an Integer is not a Reference type, no you can't.
: >>>
: >>> Nothing can only be returned for "pointers"
: >>>
: >>> Chris
: >>
: >>
: >> So, how could I define this function to return "nothing" or
: >> an integer? Is this possible?
: >
: > No, you will have to return a reference type to check for nothing.
: > If you are trying to catch when something bad happens and can not
: > return a value like -1 to indicate it, you can throw an exception.
: >
: > Chris
:
: That's a good idea.
:
: Thanks!
:
: I did find this:
: http://nullabletypes.sourceforge.net/
:
: But, not sure I want to tackle it.
Why not? The nullable type sounds like it may serve your needs. Consider
the
following code block (note that this is new to version 2.0 of the
framework):
'------------------------------------------------
Option Strict

Imports Microsoft.VisualBasic
Imports System

Public Module [module]
Public Sub Main()

Dim n0 As Nullable(Of Integer)

n0 = GetNullValue()
PrintValue(n0)

n0 = GetValue()
PrintValue(n0)

Dim n1 As Integer = CType((GetValue), Integer)
PrintValue(n1)

Dim n2 As Short = CType((GetValue), Short)
PrintValue(n2)

PrintValue(Nothing)

PrintValue(100)

End Sub

Public Sub PrintValue(n As Nullable(Of Integer))

If n.HasValue Then
Console.WriteLine("n = " & n.Value)
Else
Console.WriteLine("n is a null value")
End If

End Sub

Public Function GetValue() As Nullable(Of Integer)
Return 0
End Function

Public Function GetNullValue() As Nullable(Of Integer)
Return Nothing
End Function

End Module
'------------------------------------------------
This generates the following output:
'------------------------------------------------
n is a null value
n = 0
n = 0
n = 0
n is a null value
n = 100
'------------------------------------------------
The earlier suggestion of using -1 or throwing an exception are valid
approaches but exceptions can penalize performance and should be used
sparingly. Exceptions are for exceptional conditions. If what your are
trying to capture with the null value is a normal event, throwing and
catching exceptions may be excessive.
On the other hand, using a value such as -1 to flag something only works
if -1 isn't a valid value otherwise. And it has the additional drawback of
requiring anyone using the function to know the significance of that value
before hand.
If any of these limitations adversely affect what you are trying to do,
I'd
say try the nullable type approach and see what it does for you.
HTH
Ralf
--
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.

Feb 20 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
3
by: Simon G Best | last post by:
Hello! The C++ standard library provides facilities for finding out the sizes (and other such stuff) of numeric types (::std::numeric_limits<>, for example). What I would like to do is to...
3
by: Pierre Espenan | last post by:
A have a long integer class. The built integer type within a conditional statement returns bool false for int i=0 and bool true for any other non zero value. I want my long integer class to have...
0
by: Joe Sullivan | last post by:
I am working on some VB dot net classes and am inheriting from a class that has a method that returns a general object. My problem is, of course, is when i want to return an integer using this...
10
by: ruroma | last post by:
Hello, I need some help with the following: 1) I need to split a 16bit INT into two 8bit characters, and then be able to join these two characters to form again the original 16bit integer....
12
by: Abhishek | last post by:
now suppose I have declared an integer value inside a function as int x; now if the return type of the function is of type (void *) then can I write return((void *)x) in side the function? I...
6
by: comp.lang.php | last post by:
I'm involved in a rather nasty debate involving a strange issue (whereby the exasperated tell me to RTFM even after my having done so), where this is insanely possible: print_r(is_int('1'));...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
12
by: aaragon | last post by:
I have this scenario: several arrays for which I have their fixed values at compilation time. Now, at runtime I need to access a specific array depending on an integer but I want to avoid if and...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.