Integer Functions that return Nothing?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gregory_may

    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


  • AlanT

    #2
    Re: Integer Functions that return Nothing?


    '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.

    Comment

    • Chris

      #3
      Re: Integer Functions that return Nothing?

      gregory_may wrote:[color=blue]
      > 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
      >
      >[/color]

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

      Nothing can only be returned for "pointers"

      Chris

      Comment

      • gregory_may

        #4
        Re: Integer Functions that return Nothing?

        So, how could I define this function to return "nothing" or an integer? Is
        this possible?

        "Chris" <no@spam.com> wrote in message
        news:eRnjpfBNGH A.3144@TK2MSFTN GP11.phx.gbl...[color=blue]
        > gregory_may wrote:[color=green]
        >> 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
        >>
        >>[/color]
        >
        > Since an Integer is not a Reference type, no you can't.
        >
        > Nothing can only be returned for "pointers"
        >
        > Chris[/color]


        Comment

        • Chris

          #5
          Re: Integer Functions that return Nothing?

          gregory_may wrote:[color=blue]
          > So, how could I define this function to return "nothing" or an integer? Is
          > this possible?
          >
          > "Chris" <no@spam.com> wrote in message
          > news:eRnjpfBNGH A.3144@TK2MSFTN GP11.phx.gbl...
          >[color=green]
          >>gregory_may wrote:
          >>[color=darkred]
          >>>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
          >>>
          >>>[/color]
          >>
          >>Since an Integer is not a Reference type, no you can't.
          >>
          >>Nothing can only be returned for "pointers"
          >>
          >>Chris[/color]
          >
          >
          >[/color]

          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

          Comment

          • gregory_may

            #6
            Re: Integer Functions that return Nothing?

            That's a good idea.

            Thanks!

            I did find this:
            open source, source code, freeware, shareware, programmer, developer, VB, Visual Basic, C++, C#, C Sharp, Oracle, Oracle8i, Oracle9i, VB 6, VB6, VB.NET, Visual Basic.NET, Visual Basic 6, Visual Studio, Visual Studio.NET, framework .net, dotNet, microsoft, oracle, OO, OOA, OOD, OOP, UML, unified modeling language


            But, not sure I want to tackle it.



            "Chris" <no@spam.com> wrote in message
            news:e62Mv2BNGH A.456@TK2MSFTNG P15.phx.gbl...[color=blue]
            > gregory_may wrote:[color=green]
            >> So, how could I define this function to return "nothing" or an integer?
            >> Is this possible?
            >>
            >> "Chris" <no@spam.com> wrote in message
            >> news:eRnjpfBNGH A.3144@TK2MSFTN GP11.phx.gbl...
            >>[color=darkred]
            >>>gregory_ma y 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[/color]
            >>
            >>
            >>[/color]
            >
            > 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[/color]


            Comment

            • _AnonCoward

              #7
              Re: Integer Functions that return Nothing?

              "gregory_ma y" <None> wrote in message
              news:%23eo$U%23 BNGHA.3896@TK2M SFTNGP15.phx.gb l...
              :
              : "Chris" <no@spam.com> wrote in message
              : news:e62Mv2BNGH A.456@TK2MSFTNG P15.phx.gbl...
              : >
              : > gregory_may wrote:
              : >>
              : >> "Chris" <no@spam.com> wrote in message
              : >> news:eRnjpfBNGH A.3144@TK2MSFTN GP11.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.Visua lBasic
              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(Noth ing)

              PrintValue(100)

              End Sub

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

              If n.HasValue Then
              Console.WriteLi ne("n = " & n.Value)
              Else
              Console.WriteLi ne("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.


              Comment

              • Herfried K. Wagner [MVP]

                #8
                Re: Integer Functions that return Nothing?

                "Chris" <no@spam.com> schrieb:[color=blue][color=green]
                >> 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
                >>
                >>[/color]
                >
                > Since an Integer is not a Reference type, no you can't.
                >
                > Nothing can only be returned for "pointers"[/color]

                '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(Intege r)' in C#.

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

                Comment

                • gregory_may

                  #9
                  Re: Integer Functions that return Nothing?

                  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.


                  "_AnonCowar d" <abcdef@uvwxyz. com> wrote in message
                  news:0JwJf.2117 0$no3.1266@torn ado.southeast.r r.com...[color=blue]
                  > "gregory_ma y" <None> wrote in message
                  > news:%23eo$U%23 BNGHA.3896@TK2M SFTNGP15.phx.gb l...
                  > :
                  > : "Chris" <no@spam.com> wrote in message
                  > : news:e62Mv2BNGH A.456@TK2MSFTNG P15.phx.gbl...
                  > : >
                  > : > gregory_may wrote:
                  > : >>
                  > : >> "Chris" <no@spam.com> wrote in message
                  > : >> news:eRnjpfBNGH A.3144@TK2MSFTN GP11.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.Visua lBasic
                  > 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(Noth ing)
                  >
                  > PrintValue(100)
                  >
                  > End Sub
                  >
                  > Public Sub PrintValue(n As Nullable(Of Integer))
                  >
                  > If n.HasValue Then
                  > Console.WriteLi ne("n = " & n.Value)
                  > Else
                  > Console.WriteLi ne("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.
                  >
                  >[/color]


                  Comment

                  Working...