473,289 Members | 1,808 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,289 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 10531
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sam Smith | last post by:
Hi, I wan't a function to take a const char*, a start bit position and number of bits and convert that bit-stream into a primitive of desired type. I.e. something like: char convert(const...
4
by: Eric Lilja | last post by:
Hello, I've made a templated class Option (a child of the abstract base class OptionBase) that stores an option name (in the form someoption=) and the value belonging to that option. The value is...
4
by: Rodusa | last post by:
I am having problem to apply updates into this function below. I tried using cursor for updates, etc. but no success. Sql server keeps telling me that I cannot execute insert or update from inside...
2
by: Bubba | last post by:
I know it's possible, just don't know how to do it. I have a spreadsheet that I imported into access. Two of the columns in the table have Hard Drive space values listed for example 2.45 GB and 453...
2
by: William Stacey | last post by:
Example line: string temp = Convert.ToString(null); Convert.ToString() says it will return empty string if null is passed as parm. This returns a null. Is this oversight in the Convert method?...
17
by: David Scemama | last post by:
Hi, I'm writing a program using VB.NET that needs to communicate with a DOS Pascal program than cannot be modified. The communication channel is through some file databases, and I have a huge...
7
by: patang | last post by:
I want to convert amount to words. Is there any funciton available? Example: $230.30 Two Hundred Thirty Dollars and 30/100
6
by: patang | last post by:
Could someone please tell me where am I supposed to put this code. Actually my project has two forms. I created a new module and have put the following code sent by someone. All the function...
4
by: Edwin Knoppert | last post by:
In my code i use the text from a textbox and convert it to a double value. I was using Convert.ToDouble() but i'm used to convert comma to dot. This way i can assure the text is correct. However...
4
by: dba_222 | last post by:
Dear Experts, Ok, I hate to ask such a seemingly dumb question, but I've already spent far too much time on this. More that I would care to admit. In Sql server, how do I simply change a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.