algorithmic modeling for Rhino
Hi all,
This is probably a dumb question, but can't seem to find the key to it. How can you add a Null element to a List of some kind?
Explanation: my C# component returns a list of vectors as solutions to a geometry check, some of which may not be valid. But since amount of solutions and order must be consistent for other reasons, how can I insert a <null> element to the List<Vector3d> to convey a solution that wasn't found?
Thanks a lot!
Jose Luis
Tags:
Use nullable type(s) (see the ? used in List<Vector3d?> )
Awesome, thanks to both.
And here's the trad update (more complexity, more bugs, one division by zero == progress)
have nullable fun
Wow Peter, thank you for such a comprehensive response.
For the sake of completeness and indexability, I would like to post here some valuable comments in Peter's definition:
VB.Net’s Nothing keyword is is not the same as C#’s null. MSDN states, “Assigning Nothing to a variable sets it to the default value for its declared type. If that type contains variable members, they are all set to their default value”.
In other words, the Nothing keyword is actually equivalent to C#’s default(T) keyword, where T is the type that the expression is used as.
This can lead to nasty surprises with nullable types in conditional operators.
In C#, the expression (...) ? null : 1 will not compile, since “there is no implicit conversion between '<null>' and 'int'”. Since null is an untyped expression, the type of the conditional is inferred to be int, resulting in an error because null cannot be converted to int.
In VB.Net, by contrast, the equivalent expression, If((...), Nothing, 1), will compile, but will have unexpected results.Here too, Nothing is an untyped expression, so the type of the conditional is inferred to be Integer. However, unlike null, Nothing can be converted to Integer, so this is compiled as If((...), 0, 1), which is probably not what the programmer intended.
In both languages, the solution is to use an expression which is actually typed as int?, by writing new int?(), (in C#) or New Integer?() (in VB.Net).
Thanks a lot again Peter, your example was super helpful.
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by