convert of VB fix function

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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.
 
Ed,

Why not set a reference to Microsoft.VisualBasic.dll and then just use
the Fix function that is exported through that?
 
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 said:
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.
 
If I use your suggestion, does that give any deployment issues?

Thanks for your reply...Ed


Nicholas Paldino said:
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]
- (e-mail address removed)

Ed said:
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
 
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.
 
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]
- (e-mail address removed)

Ed said:
If I use your suggestion, does that give any deployment issues?

Thanks for your reply...Ed


Nicholas Paldino said:
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]
- (e-mail address removed)

Ed said:
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
 
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#.
 
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 said:
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.
 

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

Back
Top