Results 1 to 11 of 11

Thread: IsNot Nothing Question

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    Minneapolis, MN
    Posts
    531

    IsNot Nothing Question

    Hello:

    I have a string that can occasionally equal nothing, so my solution is to skip it when this is the case.

    Code:
                            If EdmSearchResult.Path IsNot Nothing Then
                                ReturnFileName = EdmSearchResult.Path
                                Return ReturnFileName
    
                            End If
    The problem is, the code gets stuck on the If statement because the value is nothing.

    What about this logic is incorrect?

    Thanks!

  2. #2
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: IsNot Nothing Question

    Are you initializing it as nothing?
    Code:
    Dim ReturnFileName As String = Nothing
    maybe check of empty or null and/or whitespace, etc

    When dealing with checking string nothingness I've grown happy with just checking the Length property

    Code:
    If MyString.Length <=0 Then
     'this is obviously nothing
    End If

  3. #3
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: IsNot Nothing Question

    Quote Originally Posted by ssabc View Post
    Hello:

    I have a string that can occasionally equal nothing, so my solution is to skip it when this is the case.

    Code:
                            If EdmSearchResult.Path IsNot Nothing Then
                                ReturnFileName = EdmSearchResult.Path
                                Return ReturnFileName
    
                            End If
    The problem is, the code gets stuck on the If statement because the value is nothing.

    What about this logic is incorrect?

    Thanks!
    When you say "code gets stuck" are you getting an error or some other unexpected behaviour? Which value is nothing, EdmSearchResult or EdmSearchResult.Path? Your code is checking EdmSearchResult.Path but this will fail if EdmSearchResult is nothing.

  4. #4
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,758

    Re: IsNot Nothing Question

    @kpmc
    Checking the length on a string that is nothing will fail. Since it is nothing, it's length doesn't exist.

    Need to check for nothingness first...
    Code:
    If (MyString IsNot Nothing ANDALSO MyString.Length>0)
    'do something
    End If
    @ssabc
    If it EdmSearchResult.Path that is noting or is EdmSearchResult nothing?
    Try this.
    Code:
    If EdmSearchResult IsNot Nothing AndAlso EdmSearchResult.Path IsNot Nothing Then
    'do something
    End If
    Last edited by kebo; Mar 9th, 2018 at 10:59 AM.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  5. #5
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: IsNot Nothing Question

    Checking the length on a string that is nothing will fail. Since it is nothing, it's length doesn't exist.
    For some reason I thought this would return -1 in that event

    I guess Ive not run into the Null reference exception bcz I always initialize my string with empty value before using them anywhere.
    Last edited by kpmc; Mar 9th, 2018 at 10:59 AM.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    Minneapolis, MN
    Posts
    531

    Re: IsNot Nothing Question

    This is good. I just removed the Path. The issue is, EdmSearchResult.Path was equal to Nothing!! Thus, EdmSearchResult is also equal to Nothing.

    Apparently, this does not play well with strings.

    The EdmSearchResult.Path.Length <=0 thing (previous reply) also did not work.

    Things are moving along, but if I only had a string to check, what would be the answer? Even initializing as Nothing or "" produced undesirable results.

  7. #7
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,758

    Re: IsNot Nothing Question

    The issue is, EdmSearchResult.Path was equal to Nothing!! Thus, EdmSearchResult is also equal to Nothing.
    The existence of child property of a class has no bearing on whether the class itself exists or not so that statement makes no sense. Maybe you just typed it backwards.

    If you want to know if a string (or any other class) exists, you need to test is against Nothing, (i.e MyClass Is/IsNot Nothing). If that is not working you have something else that is wrong.
    Last edited by kebo; Mar 9th, 2018 at 11:13 AM.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    Re: IsNot Nothing Question

    A String is a reference type and can be equal to Nothing, but more often than not the default value of a String is String.Empty. Could you elaborate on the situation that causes the String to be equal to Nothing to begin with?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: IsNot Nothing Question

    Thus, EdmSearchResult is also equal to Nothing.
    I'd say that is the answer, though the "Thus" is wrong. EdmSearchResult was nothing, so accessing the Path member caused an exception.
    My usual boring signature: Nothing

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: IsNot Nothing Question

    If what you want to know is that EdmSearchResult is not Nothing and that EdmSearchResult.Path is not Nothing then the most efficient way in VB 2017 is to use null propagation:
    vb.net Code:
    1. If EdmSearchResult?.Path IsNot Nothing Then
    The 'EdmSearchResult?.Path' expression will evaluate to Nothing if either EdmSearchResult or EdmSearchResult.Path is Nothing. If you want the String to not be empty too then use this:
    vb.net Code:
    1. If Not String.IsNullOrEmpty(EdmSearchResult?.Path) Then

  11. #11
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: IsNot Nothing Question

    Quote Originally Posted by jmcilhinney View Post
    If what you want to know is that EdmSearchResult is not Nothing and that EdmSearchResult.Path is not Nothing then the most efficient way in VB 2017 is to use null propagation:
    vb.net Code:
    1. If EdmSearchResult?.Path IsNot Nothing Then
    The 'EdmSearchResult?.Path' expression will evaluate to Nothing if either EdmSearchResult or EdmSearchResult.Path is Nothing. If you want the String to not be empty too then use this:
    vb.net Code:
    1. If Not String.IsNullOrEmpty(EdmSearchResult?.Path) Then
    Also for the second eg here you can just use:

    VB.Net Code:
    1. If EdmSearchResult?.Path <> "" Then

    ... string comparisons with = "" is the same as IsNullOrEmpty in VB.Net -- which I really like

    Kris

Posting Permissions

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



Click Here to Expand Forum to Full Width