nullable types got me down

S

Steve

I'm in the very early stages of evaluating an OR Mapper library called
EntitySpaces (www.entityspaces.com) and it's really cool, but they use
nullable types which are new to me and it's making my life very hard.

I'm generating business entities from different MySql Dbs and some of the
tables that have logical relationships have different integral fields, which
should be OK. Problem is, when my generated business entities have
different nullable integral types I can't assign to them.

For example:
int? a = 111;
short? b = a;

will not work. I have tried every kind of cast I could think of and none
will work.
b = (int?)a;
b = (int)a;
b = (int)a.value;

This will work:
b = int.Parse(a.Value.ToString());

However that code makes me ill and feel like I will vomit ;0)

So my trial and error has not led me to a clean solution. Other than a DB
redesign or refactoring generated business entities to all use the same
integral type, is there another solution that someone might know of?
Suggestions? Sympathy?

Thanks for reading,
Steve
 
J

Jon Skeet [C# MVP]

Steve said:
I'm in the very early stages of evaluating an OR Mapper library called
EntitySpaces (www.entityspaces.com) and it's really cool, but they use
nullable types which are new to me and it's making my life very hard.

I'm generating business entities from different MySql Dbs and some of the
tables that have logical relationships have different integral fields, which
should be OK. Problem is, when my generated business entities have
different nullable integral types I can't assign to them.

For example:
int? a = 111;
short? b = a;

will not work.

Indeed - and that has nothing to do with them being nullable. This
won't work either:

int a = 111;
short b = a;
This will work:
b = int.Parse(a.Value.ToString());

No it won't:

"Test.cs(10,13): error CS0266: Cannot implicitly convert type 'int' to
'short?'. An explicit conversion exists (are you missing a cast?)"

b = (short?) a;

works fine though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top