Reputation: 777
I would like to cast a C# nullable enum into an int.
For now, I have something like:
c#
public enum Dir
{
Up = 0,
Down = 1,
Left = 2,
Right = 3
}
c++/cli
function(System::Nullable<Dir> d){
if(d != NULL){
(int)d; error C2440: 'type cast' : cannot convert from 'System::Nullable<T>' to 'int'
}
}
So, how to cast a nullable enum ? There is no such nullable int c++/CLI or ?
Thanks
Upvotes: 1
Views: 1846
Reputation: 27864
You need to use HasValue
to test for null (C++/CLI won't let you compare against nullptr
, for some reason), and you can use Value
to retrieve the value, which can then be cast to integer.
void function(System::Nullable<Dir> d){
if(d.HasValue){
int i = (int)d.Value;
}
}
Upvotes: 3
Reputation: 887797
You should be able to access the value of the Nullable<T>
with the .Value
property, and cast that to int
.
Disclaimer: I've never used C++/CLI
Upvotes: 0