Help - Array = Nothing - How do I catch

  • Thread starter Thread starter xla76
  • Start date Start date
X

xla76

I have a function that returns an array of string values, occasionally
the returned array value is 'Nothing'
I need to check if the value is nothing before moving on - how can I do
this - none of the options i've looked at seems to fit? (vb2005)

Thanks

Pete
 
xla76 said:
I have a function that returns an array of string values, occasionally
the returned array value is 'Nothing'

\\\
If s(n) IsNot Nothing Then
...
End If
///
 
xla76 said:
Doh!

Thanks

Nothe that is a bad practice returning nothing for an array or a collection.
If the code is your you'd better return an empty array or a collection with
0 elements.
 
Fabio said:
Nothe that is a bad practice returning nothing for an array or a
collection. If the code is your you'd better return an empty array
or a collection with 0 elements.


If there is nothing to return why not return nothing? Sometimes Nothing
expresses something different from an empty array. I think it depends on the
purpose of the function whether it makes sense to return Nothing.


Armin
 
Armin,

I was in doubt if I would write something about this. Now you do it, it is
easy to write that I agree with you. Just to show my opinion in this. In
this case was showing nothing bad.

Cor
 
"Armin Zingler" <[email protected]> ha scritto nel messaggio

If there is nothing to return why not return nothing? Sometimes Nothing
expresses something different from an empty array. I think it depends on
the
purpose of the function whether it makes sense to return Nothing.

Simply because it is ambiguos for the developer that will use that code.
He won't know if the function will return something or not and force him to
overkill code to test useless condition such as

If Not array Is Nothing Then
If array.Lenght > 0 Then
Else
' error
Endif
Else
' error
Endif

resulting in less readable code, less performances and so on, and if Him
forget/don't know that the array can be nothing the app will crash.

This is the same reason for that someone suggest to not return nothing for a
derived class in a factory pattern but an istance of MyInvalidClass.

But everyone can choose its way to be "not CLS compliant" :)
 
Fabio Z said:
"Armin Zingler" <[email protected]> ha scritto nel messaggio



Simply because it is ambiguos for the developer that will use that
code. He won't know if the function will return something or not

If he doesn't know he should read the function's documentation.

"If the file is not found, the return value is Nothing. Otherwise it returns
an array containing one item per entry within the file. There might be zero
items in the file." (just an example)

BTW said:
and
force him to overkill code to test useless condition such as

If Not array Is Nothing Then
If array.Lenght > 0 Then
Else
' error
Endif
Else
' error
Endif

resulting in less readable code, less performances and so on,

I think it is not less readable. The code handles all possible situations.
and if
Him forget/don't know that the array can be nothing the app will
crash.

see 1st paragraph
This is the same reason for that someone suggest to not return
nothing for a derived class in a factory pattern but an istance of
MyInvalidClass.

But everyone can choose its way to be "not CLS compliant" :)

Not a CLS issue, I think.


Armin
 
Armin Zingler said:
If he doesn't know he should read the function's documentation.

If you wrote it.

"If the file is not found, the return value is Nothing.
Boolean?


Otherwise it returns an array containing one item per entry within the
file. There might be zero items in the file." (just an example)

???
I don't understand the example.

BTW, zero <> DBNull.Value.

???
from where DBNull.Value comes now?
DBNull.Value is a special class (As I sayd in previous post) used instead of
Nothing to indicate a non valid value.


Not a CLS issue, I think.

Yes, just a common sense issue.
I would say "everyone can choose its way to write bad code".
 
Fabio Z said:
If you wrote it.

And if you use it. If you don't know what you use, you shouldn't.

"As String()"?

I think I see what you mean but this would require another argument. Both is
valid.
???
I don't understand the example.

It shows that Nothing and an empty array is not the same. Nothing can
express something different than an empty array. The documentation clearly
states when is returned what.
???
from where DBNull.Value comes now?
DBNull.Value is a special class (As I sayd in previous post) used
instead of Nothing to indicate a non valid value.

I wanted to show that zero is not the same as DBNull.Value just like Nothing
is not the same as an empty array.
Yes, just a common sense issue.
I would say "everyone can choose its way to write bad code".

Umm.... yes.


Armin
 
Back
Top