471,319 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

convert of VB fix function

Ed
I want to convert the following VB code to C#:

Dim r as datarow
Dim i as integer

i = fix(r("NumberField"))

Fix as you may know truncates a floating point number to the right of the
decimal
point and returns an integer.

I have tried in C#:

i = convert.int32(r["NumberField"]) ' which produced rounding errors

i = round(r["NumberField"],0) ' which also produced rounding errors

i = (int) (r["NumberField"]) 'gives the correct results but
'raises an exception somtimes -
I think when the
value is NULL (this is data from
SQL)
I tried to write a function like:

private int fix(object o) {
return (int) o)
}

But this seems to produce numeric errors also.

So folks how do I write a "Fix" function in C#.

Thanks in advance ...Ed
Feb 16 '06 #1
8 10372
> i = (int) (r["NumberField"]) 'gives the correct results but
'raises an exception somtimes -
I think when the
value is NULL (this is data
from
SQL)


This seems like the best option, but what DO you want to happen when the
value is null?

You could have something like:

i = (r["NumberField"] == DBNull.Value) ? 0 : (int)(r["NumberField"]);

But only if that suits your needs.
Feb 16 '06 #2
Ed,

Why not set a reference to Microsoft.VisualBasic.dll and then just use
the Fix function that is exported through that?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ed" <Ed@discussions.microsoft.com> wrote in message
news:56**********************************@microsof t.com...
I want to convert the following VB code to C#:

Dim r as datarow
Dim i as integer

i = fix(r("NumberField"))

Fix as you may know truncates a floating point number to the right of the
decimal
point and returns an integer.

I have tried in C#:

i = convert.int32(r["NumberField"]) ' which produced rounding errors

i = round(r["NumberField"],0) ' which also produced rounding errors

i = (int) (r["NumberField"]) 'gives the correct results but
'raises an exception somtimes -
I think when the
value is NULL (this is data
from
SQL)
I tried to write a function like:

private int fix(object o) {
return (int) o)
}

But this seems to produce numeric errors also.

So folks how do I write a "Fix" function in C#.

Thanks in advance ...Ed

Feb 16 '06 #3
Ed
If I use your suggestion, does that give any deployment issues?

Thanks for your reply...Ed
"Nicholas Paldino [.NET/C# MVP]" wrote:
Ed,

Why not set a reference to Microsoft.VisualBasic.dll and then just use
the Fix function that is exported through that?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ed" <Ed@discussions.microsoft.com> wrote in message
news:56**********************************@microsof t.com...
I want to convert the following VB code to C#:

Dim r as datarow
Dim i as integer

i = fix(r("NumberField"))

Fix as you may know truncates a floating point number to the right of the
decimal
point and returns an integer.

I have tried in C#:

i = convert.int32(r["NumberField"]) ' which produced rounding errors

i = round(r["NumberField"],0) ' which also produced rounding errors

i = (int) (r["NumberField"]) 'gives the correct results but
'raises an exception somtimes -
I think when the
value is NULL (this is data
from
SQL)
I tried to write a function like:

private int fix(object o) {
return (int) o)
}

But this seems to produce numeric errors also.

So folks how do I write a "Fix" function in C#.

Thanks in advance ...Ed


Feb 16 '06 #4
Ed
Gabriel:

Yes, I would like to return 0 when it is dbnull, but I have trouble
when I try to make the line of code you have into a function.
Strange things seem to happen when I try to pass r["NumberField"] as
type object. Is there something else I should be passing it as.

- Ed

"Gabriel Magaña" wrote:
i = (int) (r["NumberField"]) 'gives the correct results but
'raises an exception somtimes -
I think when the
value is NULL (this is data
from
SQL)


This seems like the best option, but what DO you want to happen when the
value is null?

You could have something like:

i = (r["NumberField"] == DBNull.Value) ? 0 : (int)(r["NumberField"]);

But only if that suits your needs.

Feb 16 '06 #5
> Strange things seem to happen when I try to pass r["NumberField"] as
type object. Is there something else I should be passing it as.


Well, post your function... You really cannot have it be anything more
precise than object because the field can hold either DBNull.Value or the
typed data.
Feb 16 '06 #6
Ed,

No, as it comes with a standard install of the .NET framework.

The only thing is that if the only function you are going to use is Fix
from that DLL, then you are loading a large dll into your app domain for
very little benefit.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ed" <Ed@discussions.microsoft.com> wrote in message
news:38**********************************@microsof t.com...
If I use your suggestion, does that give any deployment issues?

Thanks for your reply...Ed
"Nicholas Paldino [.NET/C# MVP]" wrote:
Ed,

Why not set a reference to Microsoft.VisualBasic.dll and then just
use
the Fix function that is exported through that?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ed" <Ed@discussions.microsoft.com> wrote in message
news:56**********************************@microsof t.com...
>I want to convert the following VB code to C#:
>
> Dim r as datarow
> Dim i as integer
>
> i = fix(r("NumberField"))
>
> Fix as you may know truncates a floating point number to the right of
> the
> decimal
> point and returns an integer.
>
> I have tried in C#:
>
> i = convert.int32(r["NumberField"]) ' which produced rounding
> errors
>
> i = round(r["NumberField"],0) ' which also produced rounding errors
>
> i = (int) (r["NumberField"]) 'gives the correct results but
> 'raises an exception
> somtimes -
> I think when the
> value is NULL (this is data
> from
> SQL)
> I tried to write a function like:
>
> private int fix(object o) {
> return (int) o)
> }
>
> But this seems to produce numeric errors also.
>
> So folks how do I write a "Fix" function in C#.
>
> Thanks in advance ...Ed
>
>


Feb 16 '06 #7
"=?Utf-8?B?RWQ=?=" <Ed@discussions.microsoft.com> wrote in
news:56**********************************@microsof t.com:
I want to convert the following VB code to C#:

Dim r as datarow
Dim i as integer

i = fix(r("NumberField"))

Fix as you may know truncates a floating point number to the
right of the decimal
point and returns an integer.

I have tried in C#:

i = convert.int32(r["NumberField"]) ' which produced
rounding errors

i = round(r["NumberField"],0) ' which also produced rounding
errors

i = (int) (r["NumberField"]) 'gives the correct results but
'raises an exception
somtimes -
I think when the
value is NULL (this
is data from
SQL)
I tried to write a function like:

private int fix(object o) {
return (int) o)
}

But this seems to produce numeric errors also.

So folks how do I write a "Fix" function in C#.


Ed,

Use Reflector (http://www.aisto.com/roeder/dotnet/) to see
how the Fix methods are implemented.

- In the toolbar at the top of Reflector's work area, set
the language to C#.
- Load Microsoft.VisualBasic.dll
- Open the Microsoft.VisualBasic namespace.
- Open the Conversion class.

There are several overloads of Fix listed there. Double click
on one of the Fix methods to see how it's implemented in C#.

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Feb 17 '06 #8
Ed
Gabriel -

I am still having problems. I need the "fix" function to work with both
types from MS SQL Server, and decimal types. The function I tried to write
throws an exception when it incounters a decimal type. I need the fix
function to truncate everyting to the right of the decimal sign. I have
pasted the function below:

- Ed

-----------------------------------------------------------------------------------------
private int fix(object o)
{
int i;
decimal testD = 0;
try
{
if (o.GetType() == testD.GetType())
{
i = (int) o;
return i;
}
if (o == DBNull.Value)
{
return 0;
}
else
{
i = (int) o;
return i;
}

}
catch
{
return 0;
}

}
---------------------------------------------------------------------------------


"Gabriel Magaña" wrote:
Strange things seem to happen when I try to pass r["NumberField"] as
type object. Is there something else I should be passing it as.


Well, post your function... You really cannot have it be anything more
precise than object because the field can hold either DBNull.Value or the
typed data.

Mar 3 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Sam Smith | last post: by
2 posts views Thread by William Stacey | last post: by
17 posts views Thread by David Scemama | last post: by
7 posts views Thread by patang | last post: by
6 posts views Thread by patang | last post: by
4 posts views Thread by Edwin Knoppert | last post: by
reply views Thread by rosydwin | last post: by

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.