throwing ArgumentNullException with multiple parameters

  • Thread starter Thread starter Andy B.
  • Start date Start date
A

Andy B.

If I have a method that takes 3 arguments:

public sub 3Parameters(ByVal one as int, ByVal two as int, ByVal three as
int)
end sub

And I need to test the 3 parameters for null (none of them can be null), how
would I throw the ArgumentNullException to deal with all 3 parameters?
 
Andy B. said:
If I have a method that takes 3 arguments:

public sub 3Parameters(ByVal one as int, ByVal two as int, ByVal three as
int)
end sub

And I need to test the 3 parameters for null (none of them can be null),
how would I throw the ArgumentNullException to deal with all 3 parameters?

'Integer' ('int'?) is a value type, and it cannot be 'Nothing' (in terms of
a reference). Thus ArgumentNullException' is not suitable here.

I'd test each parameter for 'Nothing' and throw an 'ArgumentNullException'
for the parameter if it's a reference to 'Nothing'.
 
Andy said:
If I have a method that takes 3 arguments:

public sub 3Parameters(ByVal one as int, ByVal two as int, ByVal
three as int)
end sub

And I need to test the 3 parameters for null (none of them can be
null), how would I throw the ArgumentNullException to deal with all 3
parameters?

If none of them can be Null, what's the meaning of your question?


Armin
 
Andy B. said:
If I have a method that takes 3 arguments:

public sub 3Parameters(ByVal one as int, ByVal two as int, ByVal three as
int)
end sub

And I need to test the 3 parameters for null (none of them can be null),
how would I throw the ArgumentNullException to deal with all 3 parameters?


Does the following answer what you meant to ask?

Public Sub ThreeParams _
(ByVal obj1 As Object, ByVal obj2 As Object, ByVal obj3 As Object)

If (obj1 Is Nothing Or obj2 Is Nothing Or obj3 Is Nothing) Then
Throw New ArgumentNullException
End If

End Sub
 
James said:
An integer can be nullable, so ArgumentNullException may
well be relevant.

Even so, from MSDN (1):

<quote>
ArgumentNullException - The exception that is thrown when a null
reference (Nothing in Visual Basic) is passed to a method that does
not accept it as a valid argument.
</quote>

To my understanding (which may be wrong), the relevant word here is
*reference* (as in "null reference"), and the exception indicates that
a *reference type* argument is null when it shouldn't be, the
reasoning being that the use of a null reference may bring evident ill
consequences in many contexts.

For the case when a given parameter is invalid (as an integer being 0
when it shouldn't be) it's probably more adequate to throw
"ArgumentException" ("The exception that is thrown when one of the
arguments provided to a method is not valid" -- again from MSDN).

More over, if the invalid argument is being used as an index or such,
then there's also a possibility of throwing
ArgumentOutOfRangeException.

HTH.

Regards,

Branco.

(1) http://msdn.microsoft.com/en-us/library/system.argumentnullexception.aspx
 
Andy,

As "int" is an by you created type, then you can use the set from the
property to create the exception.

As in is not an by you created type, then your code simple does not compile
in VB. "int" is a keyword from C type languages

Cor
 
0 is not null - OP said the parameters can not be null. If an integer is
nullable then it is a reference type and the reference can be nothing.
That's why the ArgumentNullException can be relevant.
 
James Hahn said:
0 is not null - OP said the parameters can not be null. If an integer is
nullable then it is a reference type and the reference can be nothing.
That's why the ArgumentNullException can be relevant.

It's not really a reference type as 'Nullable(Of T)' is a structure.
However, it behaves like a reference type.
 
Addendum:
It's not really a reference type as 'Nullable(Of T)' is a structure.
However, it behaves like a reference type.

This can be easily tested:

\\\
MsgBox(GetType(Nullable(Of Int32)).IsValueType) ' True.
///
 
A nullable value type is a reference to a structure that will always appear
as the underlying type. That's the reason for the type, compared with, say,
a class wrapper. The point is, OP could well be using integers that need to
be tested for not null.
 
James said:
An integer can be nullable, so ArgumentNullException may well be relevant.

The parameters are Integer, not Nullable(Of Integer), so they are not
nullable. An Integer parameter can not contain a Nullable(Of Integer) value.
 
According to OP the parameters are "int", so they could be Integer or
Nullable(Of Integer) or, in fact, something else entirely. As the problem
was how to test them for null, the possibility that they are Nullable(Of
Integer) should be considered.
 
If the parameters are "Integer", how could they be anything except
"Integer"? Nullable(Of Integer) is an entirely different thing than
"Integer".
 
I'll say it again - According to OP the parameters are "int", so they could
be Integer or Nullable(Of Integer) or, in fact, something else entirely.
There is no basis for assuming the parameters are Integer, as Wagner did,
and the fact that OP implied they could be null suggests they are not
Integer, but something that could be null, such as Nullable(Of Integer).
Please look at the post I originally replied to.
 
If a function is defined as:

Function SomeFunc(i As Integer)

How are you going to call that function passing anything but an
Integer?
 
James said:
I'll say it again - According to OP the parameters are "int", so they could
be Integer or Nullable(Of Integer) or, in fact, something else entirely.
There is no basis for assuming the parameters are Integer, as Wagner did
<snip>

I beg to disagree. It's quite clear that the OP simply typed the code
in the body of the message, instead of cutting/pasting from a live
source. In that case, and using common sense, it's reasonable to
assume that the parameters are supposed to represent Integers.

If the original code was to be taken so literally as you're implying,
then we should, first and foremost, address the fact that the method
name ("3Parameters") is invalid, which would be far more inportant
than arguing if "int" means an Integer or a user defined type. Once we
concede that it is just air-code, then the actual intentions of the OP
(to pass 3 integer parameters) become evident. At least to me.

Regards,

Branco.
 
You are using selective quoting. You have ignored the point of the
question, namely "And I need to test the 3 parameters for null (none of them
can be null), how would I throw the ArgumentNullException to deal with all 3
parameters?"

If OP has a need to test the parameters for null then there is an
implication that they can, in fact, be null. So the bland assertion that
'ArgumentNullException is not suitable here' is inappropriate. I agree that
the parameters are supposed to represent integers, but that does not mean
that they have been defined as type Integer. They could be defined as a type
which can be null, and it is possible that testing for null is relevant and
should not be dismissed.
 
Back
Top