IIF to the rescue...
-
Because there is no other way to compare OrderNumber to 0 :laugh:
Private Function IsNew(ByVal OrderNumber As Long) As Boolean
Return IIf(OrderNumber = 0, True, False)
End FunctionIt's not horrible, but it's pretty representative for the code as a whole. A lot of redundancy, a lot of global variables, no null values (but still NullReferenceExceptions...), Forms with 1000+ lines of code, etc. VB is not the problem, it's the programmers that code it.
It's an OO world.
public class SanderRossel : Lazy<Person>
{
public void DoWork()
{
throw new NotSupportedException();
}
}Where do you see the problem behind that code that justifies it to be posted in the Weird and Wonderful? Well, that IsNew function should better be a function of the Order class and not need a parameter (because OrderNumber could be expected to be a Property). But otherwise? Does VB.Net have the ternary operator like C# (return OrderNumber = 0 ? true : false;)? I don't know. Or did you want to tell us that it's better to do the comparison of OrderNumber to 0 whenever that IsNew function is used? Oh no! That would deserve to be posted here! IsNew clearly tells you the intention of the code, while If(OrderNumber=0) does not.
-
Where do you see the problem behind that code that justifies it to be posted in the Weird and Wonderful? Well, that IsNew function should better be a function of the Order class and not need a parameter (because OrderNumber could be expected to be a Property). But otherwise? Does VB.Net have the ternary operator like C# (return OrderNumber = 0 ? true : false;)? I don't know. Or did you want to tell us that it's better to do the comparison of OrderNumber to 0 whenever that IsNew function is used? Oh no! That would deserve to be posted here! IsNew clearly tells you the intention of the code, while If(OrderNumber=0) does not.
It is a function on the
Order
class, but it still takes a parameter (it just getsmOrderNumber
given to it every time). VB does not have a ternary operator. I'm saying the function could look like this:Return OrderNumber = 0
There is no need for anIIF
anywhere in the code. As I said, it's not truly horrible, but it's an 'interesting' way to return ifOrderNumber = 0
:)It's an OO world.
public class SanderRossel : Lazy<Person>
{
public void DoWork()
{
throw new NotSupportedException();
}
} -
It is a function on the
Order
class, but it still takes a parameter (it just getsmOrderNumber
given to it every time). VB does not have a ternary operator. I'm saying the function could look like this:Return OrderNumber = 0
There is no need for anIIF
anywhere in the code. As I said, it's not truly horrible, but it's an 'interesting' way to return ifOrderNumber = 0
:)It's an OO world.
public class SanderRossel : Lazy<Person>
{
public void DoWork()
{
throw new NotSupportedException();
}
}Ah, thanks. That explains it. It really deserves this place.
-
Ah, thanks. That explains it. It really deserves this place.
Don't worry, we all have our off-days :laugh:
It's an OO world.
public class SanderRossel : Lazy<Person>
{
public void DoWork()
{
throw new NotSupportedException();
}
} -
It is a function on the
Order
class, but it still takes a parameter (it just getsmOrderNumber
given to it every time). VB does not have a ternary operator. I'm saying the function could look like this:Return OrderNumber = 0
There is no need for anIIF
anywhere in the code. As I said, it's not truly horrible, but it's an 'interesting' way to return ifOrderNumber = 0
:)It's an OO world.
public class SanderRossel : Lazy<Person>
{
public void DoWork()
{
throw new NotSupportedException();
}
}Sander Rossel wrote:
VB does not have a ternary operator.
It does, it's the
IIf
function/method/whateva -
Sander Rossel wrote:
VB does not have a ternary operator.
It does, it's the
IIf
function/method/whatevaIs that an operator or a function? ;) I wouldn't know the difference, but I believe there is one. Something like an operator is a function, but a function is not an operator.
It's an OO world.
public class SanderRossel : Lazy<Person>
{
public void DoWork()
{
throw new NotSupportedException();
}
} -
Sander Rossel wrote:
VB does not have a ternary operator.
It does, it's the
IIf
function/method/whatevaNot quite.
IIf
is a function[^], and as such it evaluates both arguments before checking the condition. So while this works in C#:someObject == null ? "Null" : someObject.ToString()
the equivalent using
IIf
will throw aNullReferenceException
:IIf(someObject Is Nothing, "Null", someObject.ToString()) ' Boom!
The real ternary operator for VB.NET is the
If
operator[^], which was added in .NET 3.5:If(someObject Is Nothing, "Null", someObject.ToString()) ' No Boom.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Don't worry, we all have our off-days :laugh:
It's an OO world.
public class SanderRossel : Lazy<Person>
{
public void DoWork()
{
throw new NotSupportedException();
}
}I guess it was an unhandled
ConfusedByVBException
... -
I guess it was an unhandled
ConfusedByVBException
...:laugh:
It's an OO world.
public class SanderRossel : Lazy<Person>
{
public void DoWork()
{
throw new NotSupportedException();
}
} -
It is a function on the
Order
class, but it still takes a parameter (it just getsmOrderNumber
given to it every time). VB does not have a ternary operator. I'm saying the function could look like this:Return OrderNumber = 0
There is no need for anIIF
anywhere in the code. As I said, it's not truly horrible, but it's an 'interesting' way to return ifOrderNumber = 0
:)It's an OO world.
public class SanderRossel : Lazy<Person>
{
public void DoWork()
{
throw new NotSupportedException();
}
}I don't know VB and was just wondering ... if
Return OrderNumber = 0
does a comparison and returns the result then how would you set the value of OrderNumber to 0 and then return OrderNumber? In C#, the first is
return OrderNumber == 0;
and the second is
return OrderNumber = 0;
-
I don't know VB and was just wondering ... if
Return OrderNumber = 0
does a comparison and returns the result then how would you set the value of OrderNumber to 0 and then return OrderNumber? In C#, the first is
return OrderNumber == 0;
and the second is
return OrderNumber = 0;
PaulLinton wrote:
I don't know VB
Obviously :) VB doesn't know the
==
operator. Instead=
can be either assignment or comparison, dependent on context. In this case it's a comparison, but no assignment. SoReturn OrderNumber = 0
Would be
return OrderNumber == 0;
in C#. Your second C# example isn't possible in VB (as far as I know, but I didn't know it was possible in C# either :) )
It's an OO world.
public class SanderRossel : Lazy<Person>
{
public void DoWork()
{
throw new NotSupportedException();
}
} -
PaulLinton wrote:
I don't know VB
Obviously :) VB doesn't know the
==
operator. Instead=
can be either assignment or comparison, dependent on context. In this case it's a comparison, but no assignment. SoReturn OrderNumber = 0
Would be
return OrderNumber == 0;
in C#. Your second C# example isn't possible in VB (as far as I know, but I didn't know it was possible in C# either :) )
It's an OO world.
public class SanderRossel : Lazy<Person>
{
public void DoWork()
{
throw new NotSupportedException();
}
}The second one would be:
Return (Ordernumber = 0)
<edit>Or like this:
Return 0 = Ordernumber
</edit>
Wrong is evil and must be defeated. - Jeff Ello[^]