dcsimg
Results 1 to 10 of 10

Thread: Numeric Property returns 0 although we did not assign 0

  1. #1

    Thread Starter
    Addicted Member Davor Geci's Avatar
    Join Date
    Sep 2009
    Posts
    179

    Numeric Property returns 0 although we did not assign 0

    How (do you) would you handle a problem when you need to have a numeric property (for example integer) and 0 and not assigned are not equal?

    For Example:

    Code:
    Private m_iMyNumber As Integer
    
    Public Property Get MyNumber() As Integer
    
        MyNumber = m_iMyNumber
    
    End Property
    
    Public Property Let MyNumber(ByVal iMyNumber As Integer)
    
        m_iMyNumber = iMyNumber
    
    End Property
    It returns 0 if I assign it or not.

    I know about the trick to go with String and assign Null to it, but what if you need to have it as Numeric, how would you distinguish whether it is blank (not assigned) or 0 is assigned?

    Thanks,
    Davor
    My projects:
    Virtual Forms
    VBA Telemetry

  2. #2
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    190

    Re: Numeric Property returns 0 although we did not assign 0

    I would properbly initialize the integer on a value i know it wouldnt reach... I would take -32,768
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  3. #3
    Hyperactive Member
    Join Date
    Jul 2007
    Location
    Essex, UK.
    Posts
    471

    Re: Numeric Property returns 0 although we did not assign 0

    Well unless you have not stated your case clearly, it works for me.

    Code:
    Option Explicit
    
    Private m_iMyNumber As Integer
    
    Public Property Get MyNumber() As Integer
    
        MyNumber = m_iMyNumber
    
    End Property
    
    Public Property Let MyNumber(ByVal iMyNumber As Integer)
    
        m_iMyNumber = iMyNumber
    
    End Property
    
    Private Sub Form_Load()
        MsgBox MyNumber
        MyNumber = 515
        MsgBox MyNumber
    End Sub
    Result was 0 then 515.

    Obviously without storing m_iMyNumber with WriteProperties or some other method, it won't survive after the app closes.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,524

    Re: Numeric Property returns 0 although we did not assign 0

    Quote Originally Posted by Davor Geci View Post
    It returns 0 if I assign it or not.
    Unlike some other languages, VB assigns default values to declared variables (when applicable).

    As others have suggested, use a value that your property will never be assigned, something outside valid values for your program. Another option is to use a class-level flag that is set when the property is set.

    However, I don't know what that does for you. What value would you want to return if it wasn't assigned? Maybe a variant would be better? The default value for new Variants is Empty.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,991

    Re: Numeric Property returns 0 although we did not assign 0

    Quote Originally Posted by Davor Geci View Post
    I know about the trick to go with String and assign Null to it,
    Actually you can not assign Null to a string. Doing so will result in an invalid use of Null error. By default a string is empty. Numeric variables default to 0.

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,524

    Re: Numeric Property returns 0 although we did not assign 0

    Quote Originally Posted by DataMiser View Post
    Actually you can not assign Null to a string. Doing so will result in an invalid use of Null error. By default a string is empty. Numeric variables default to 0.
    I believe he meant null string, i.e., StrPtr(vbNullString) = 0 whereas StrPtr("") <> 0
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,832

    Re: Numeric Property returns 0 although we did not assign 0

    Quote Originally Posted by Davor Geci View Post
    I know about the trick to go with String and assign Null to it
    Yeah, I was going to point that out as well. You can assign an empty string to a string. Or, there is a minor (but rather useless) distinction between an empty string and an unassigned string. But neither of those is considered Null.

    Personally, if I wanted this type of thing, and I wasn't willing to do some form of initialization, I'd use a Variant type. Upon declaration, they contain an Empty value, or, more precisely, an Empty type.

    An Empty isn't quite the same as a Null either, but it will certainly serve your purposes.

    Code:
    
    Option Explicit
    
    Private m_vMyNumber As Variant
    
    Public Property Get MyNumber() As Variant
    
        MyNumber = m_vMyNumber
    
    End Property
    
    Public Property Let MyNumber(ByVal vMyNumber As Variant)
    
        m_vMyNumber = vMyNumber
    
    End Property
    
    Private Sub Form_Load()
        Debug.Print TypeName(MyNumber)
        If MyNumber = Empty Then Debug.Print "We're not assigned yet."
    End Sub
    
    
    EDIT1: Ahh, and LaVolpe snuck in while I was typing. And yeah, I've always hated the name of that constant. And here's part of the reason why:

    Code:
    
    Private Sub Form_Load()
    
        Dim s As String
    
        s = ""
    
        If s = vbNullString Then Debug.Print "They're compared as equal."
    
    End Sub
    
    

    Yeah, I get the StrPtr difference, but that's sort of digging to get some use out of the distinction.
    Last edited by Elroy; Oct 15th, 2019 at 07:52 AM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,524

    Re: Numeric Property returns 0 although we did not assign 0

    @Elroy. I'd be tempted to dummy-proof that a bit by testing the passed iMyNumber, i.e., minimally testing VarType(iMyNumber) = vbInteger. And can be more robust by using CInt() with error trapping to allow strings, singles, doubles, etc, while not allowing iMyNumber to be unassigned by passing Empty (unless that is desired).
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,832

    Re: Numeric Property returns 0 although we did not assign 0

    Quote Originally Posted by LaVolpe View Post
    @Elroy. I'd be tempted to dummy-proof that a bit by testing the passed iMyNumber, i.e., minimally testing VarType(iMyNumber) = vbInteger. And can be more robust by using CInt() with error trapping to allow strings, singles, doubles, etc, while not allowing iMyNumber to be unassigned by passing Empty (unless that is desired).
    Good point, and I totally agree. I just didn't want to complicate the answer. And yeah, if an Integer or Long isn't being passed in, Davor, you might consider throwing a Type Mismatch (#13) error.

    IMHO, these Variants are just designed to do precisely what you're asking. All variable types (even objects) have an initial value. With all numbers, they're forced to pick from a number, so 0 is always chosen. With floats (Single or Double), if you'd be willing to do some initialization, you could assign a NaN to start with. But Integers (nor Longs) have anything like that, as every bit-pattern in 2s compliment is used as some integer.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  10. #10

    Thread Starter
    Addicted Member Davor Geci's Avatar
    Join Date
    Sep 2009
    Posts
    179

    Re: Numeric Property returns 0 although we did not assign 0

    Thank you guys,
    as always you did provide helpful, constructive and interesting suggestions and solutions.
    I had some troubles logging in to vbforums for past few days so couldn't respond.
    At the end I went with the class-level flag.

    Code:
    Private m_iPageLoadTime As Integer: Private h_bPageLoadTime As Boolean  ' because in VBA we can't know if a number 0 is passed to a property or is 0 because we didn't assigned any value to this property, that is why we use a helper boolean variable to determine if this 0 is assigned or is it because we didn't use this numeric property
    
    Public Property Get PageLoadTime() As Integer
    
        PageLoadTime = m_iPageLoadTime
    
    End Property
    
    Public Property Let PageLoadTime(ByVal iPageLoadTime As Integer)
        h_bPageLoadTime = True
        m_iPageLoadTime = iPageLoadTime
    
    End Property
    
    ' fill the payload string (t) for Google Analytics hit tracking with the PageLoadTime passed to the class in the property PageLoadTime
    If h_bPageLoadTime = True Then
        t = t & "&plt=" & Me.PageLoadTime
    else
       ' do nothing
    End If
    My projects:
    Virtual Forms
    VBA Telemetry

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width