-
Mar 9th, 2018, 10:32 AM
#1
Thread Starter
Fanatic Member
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!
-
Mar 9th, 2018, 10:42 AM
#2
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
-
Mar 9th, 2018, 10:47 AM
#3
Re: IsNot Nothing Question
Originally Posted by ssabc
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.
-
Mar 9th, 2018, 10:48 AM
#4
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
-
Mar 9th, 2018, 10:54 AM
#5
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.
-
Mar 9th, 2018, 11:01 AM
#6
Thread Starter
Fanatic Member
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.
-
Mar 9th, 2018, 11:09 AM
#7
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
-
Mar 9th, 2018, 11:30 AM
#8
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?
-
Mar 9th, 2018, 11:47 AM
#9
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
-
Mar 9th, 2018, 07:47 PM
#10
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:
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:
If Not String.IsNullOrEmpty(EdmSearchResult?.Path) Then
-
Mar 13th, 2018, 06:56 PM
#11
Re: IsNot Nothing Question
Originally Posted by jmcilhinney
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:
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:
If Not String.IsNullOrEmpty(EdmSearchResult?.Path) Then
Also for the second eg here you can just use:
VB.Net Code:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|