I\'m attempting to resolve the following exercise:
You need to create a class named
Productthat represents a product. The class has a
Your if state is wrong. Let's do a truth table:
if (value != String.Empty || value != null)
Name = null True Or False = True
Name = "name" True Or True = True
Name = "" False Or True = True
Your if statement is always true!
I would re-write it thus:
if (value == String.Empty || value == null)
{
throw new ArgumentException("Name cannot be null or empty string", "Name");
}
else
{
name = value;
}
you could just change the Or to and AND but I think the above reads better (the below has an unnecessary double negative):
if (value != String.Empty && value != null)
{
name = value;
}
else
{
throw new ArgumentException("Name cannot be null or empty string", "value");
}
As Dmitry Bychenko says, I didn't notice you were not testing for value. In getters you should use the value property. Not the name of your property
The second parameter (again pointed out by Dmitry Bychenko) in your exception should be:
The name of the parameter that caused the current exception.
MSDN
which in your case is the string "value":
throw new ArgumentException("Name cannot be null or empty string", "value");