By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,142 Members | 1,900 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,142 IT Pros & Developers. It's quick & easy.

Casting Obj to Float

P: n/a
I posted this in the wrong forum, story of my life.

foreach (DataRow dr1 in MyTable1.Rows)
{

reg[count] = (float)dr1["REG"];
prem[count] = (float)dr1["PREM"];
die[count] = (float)dr1["DIESEL"];
eth[count] = (float)dr1["ETH"];
term[count] = reg[count]+prem[count]+die[count]+eth[count];
count++;
}

Results in specified cast is invalid. Do I have to box? I know I'm trying to
cram a ref into a val, but that's how SQL objects get retrieved.

Thanks,

--
Steve Wasser
http://xdissent.com
the journal of contrarian social discourse and neurotic opinion
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
If the type being returned by SQL is a truly a SQL float, then you need to
cast as *double*. It's just one of those idiosyncricies that a SQL Server
float == .NET System.Double and SQL Server *real* == .NET System.Single
(float in C#).

So if my assumption is true and you are using a SQL Server float type then
this narrowing cast should work:

reg[count] = (float)(double)dr1["REG"];

Richard

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Steve Wasser" <se******@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
I posted this in the wrong forum, story of my life.

foreach (DataRow dr1 in MyTable1.Rows)
{

reg[count] = (float)dr1["REG"];
prem[count] = (float)dr1["PREM"];
die[count] = (float)dr1["DIESEL"];
eth[count] = (float)dr1["ETH"];
term[count] = reg[count]+prem[count]+die[count]+eth[count];
count++;
}

Results in specified cast is invalid. Do I have to box? I know I'm trying to cram a ref into a val, but that's how SQL objects get retrieved.

Thanks,

--
Steve Wasser
http://xdissent.com
the journal of contrarian social discourse and neurotic opinion

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.