473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

nullable types got me down

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
Jun 21 '06 #1
2 1310
Hi Steve,

The following built OK for me:

int? a = 111;
short? b = (short) a;

"Steve" <sk**@skle.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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

Jun 21 '06 #2
Steve <sk**@skle.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: John Wood | last post by:
I was just looking at an article about using nullable value types. (value types that can effectively have no value and not be set). The syntax is to append a question-mark to the value type in...
12
by: Steven Livingstone | last post by:
I've just blogged some stuff on Nullable types in net 2.0. http://stevenr2.blogspot.com/2006/01/nullable-types-and-null-coalescing.html Question however as to why you can't simply get an implcit...
6
by: Steven Livingstone | last post by:
Bit of advice here folks. I am creating a default constructor that just initializes a new instance of my object and a secon constructor that takes an ID and loads an object with data from the...
8
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked...
0
by: Larry Lard | last post by:
There seems to be something a bit lacking in the way the dataset designer thing deals (or rather doesn't) with nullable fields in VS2005. Maybe it's cos I'm using VB2005 Express (which is variously...
8
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
3
by: Mike P | last post by:
What are nullable types used for? The only use I can think of is where I have a method like this where some values being passed to it may be null : public int AddUser(int? UserRegion, string...
3
by: MobileBoy36 | last post by:
Hi all, Nullable types were announced as new handy stuff in .NET 2.0 But it seems like the datareader doesn't support nullable types. You have still to check for "IsDbNull". So, are Nullable...
6
by: Tony Johansson | last post by:
Hello! I'm reading in a book called Visual C# 2005. In the chapter about Generics there ia a section about Nullable types. Here is the text that isn't complete true I think. It says: "You...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.