473,409 Members | 1,943 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,409 software developers and data experts.

Returning Integer from String

Hi All!

Hopefully this is an easy question...

I am trying to convert input which is read in from a text file. All
data is read in as String. What I'm trying to do is safely convert
string data to integer data, if possible, or else return nothing. For
instance,

intDatum = ConvertToInt(strDatum)

where

Function ConvertToInt (ByVal strDatum as String) As Integer
Try
Return CInt(strDatum)
Catch
Return DBNull.Value
End Try
End Function
If strDatum = "3", then intDatum = 3
If strDatum = "?", then intDatum = Nothing (DBNull.Value, whatever
works...)
TIA,

DaveS
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #1
9 1187
* DaveS <an*******@devdex.com> scripsit:
I am trying to convert input which is read in from a text file. All
data is read in as String. What I'm trying to do is safely convert
string data to integer data, if possible, or else return nothing. For
instance,

intDatum = ConvertToInt(strDatum)

where

Function ConvertToInt (ByVal strDatum as String) As Integer
Try
Return CInt(strDatum)
Catch
Return DBNull.Value
End Try
End Function
If strDatum = "3", then intDatum = 3
If strDatum = "?", then intDatum = Nothing (DBNull.Value, whatever
works...)


Return 0, for example. Assigning 'DBNull.Value' doesn't make sense hier
since an 'Integer' cannot store this value.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2
Cor
Hi Dave,

Maybe your language is Dutch,

intDatum = ConvertToInt(strDatum)

And than Datum is the English Date

When that is so, than probably it is better to first convert it to the date
the date format .
(Because with the date format you can do so much more than direct to an
integer and even convert it than in a more secure way to an integer).

Did you look already at the Cdate commands to convert?

Cor


Nov 20 '05 #3
Cor
Hi Dave,

Maybe your language is Dutch,

intDatum = ConvertToInt(strDatum)

And than Datum is the English Date

When that is so, than probably it is better to first convert it to the date
the date format .
(Because with the date format you can do so much more than direct to an
integer and even convert it than in a more secure way to an integer).

Did you look already at the Cdate commands to convert?

Cor


Nov 20 '05 #4
Thanks Herfried!

The function I currently use returns -1 for non-integer input. I am
looking for a way to simplify the following code:

If ValidInt (strDatum) Then
intDatum = Int32.Parse(strDatum)
End If

where

Function ValidInt (ByVal strDatum as String) As Boolean
' Returns TRUE if strDatum is an can be represented
' as an integer; otherwise FALSE
'
Dim intDatum As Integer

Try
intDatum = Int32.Parse(strDatum)
Return True
Catch
Return False
End Try
End Function

This way, if strDatum is non-integer, intDatum doesn't get assigned
anything.

TIA,

DaveS

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #5
Cor,

American English is my native language.

Datum = one piece of data.

I am simply trying to find an elegant way to convert a string number
(such as "123" to and integer 123) or return nothing at all.

I know how to accomplish the above using If..Then..End If construct.

TIA,

DaveS

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #6
Thanks Herfried!

The function I currently use returns -1 for non-integer input. I am
looking for a way to simplify the following code:

If ValidInt (strDatum) Then
intDatum = Int32.Parse(strDatum)
End If

where

Function ValidInt (ByVal strDatum as String) As Boolean
' Returns TRUE if strDatum is an can be represented
' as an integer; otherwise FALSE
'
Dim intDatum As Integer

Try
intDatum = Int32.Parse(strDatum)
Return True
Catch
Return False
End Try
End Function

This way, if strDatum is non-integer, intDatum doesn't get assigned
anything.

TIA,

DaveS

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #7
Cor,

American English is my native language.

Datum = one piece of data.

I am simply trying to find an elegant way to convert a string number
(such as "123" to and integer 123) or return nothing at all.

I know how to accomplish the above using If..Then..End If construct.

TIA,

DaveS

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #8
Cor
Hi Dave,

Did you look at the Val function.

http://msdn.microsoft.com/library/de...l/vafctVal.asp

Cor
Nov 20 '05 #9
Cor
Hi Dave,

Did you look at the Val function.

http://msdn.microsoft.com/library/de...l/vafctVal.asp

Cor
Nov 20 '05 #10

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

Similar topics

3
by: dgaucher | last post by:
Hi, I want to consume a Web Service that returns a choice, but my C++ client always receives the same returned type. On the other hand, when I am using a Java client, it is working fine (of...
4
by: Craig Kenisston | last post by:
Hi, I have a dll that must return a string to a C# .Net application and also needs to server a Delphi application. I have defined my delphi function like this : function GetString(var strPtr...
7
by: Aaron | last post by:
Complete code follows. I am new to .NET programming (and programming in general) and I am having a difficult time understanding how to fill a variable in one sub, and then access it from...
8
by: MyAlias | last post by:
Can't solve this CallBack returning structures Error message: An unhandled exception of type 'System.NullReferenceException' occurred in MyTest.exe Additional information: Object reference not...
3
by: Sean Tynan | last post by:
I want to find out the best way for a method to notify calling code of situations such as validation errors, etc. that may occur during method execution. e.g. say I have a method (business...
4
by: ofmars | last post by:
Hi there, In C# I'm using a DLL import attribute to call a function from this DLL. This function was written in an older version of Vb and I know the name of this function and it should...
1
by: Joe Van Meer | last post by:
Hi all, I have an app that currently runs through 3 seperate stored procedures each returning a count of records. What I would like to do is combine these calls into one call, however I am...
4
by: scparker | last post by:
Hello, We have a stored procedure that does a basic insert of values. I am then able to retrieve the ID number created for this new record. We are currently using ASP.NET 2.0 and use N-Tier...
1
by: Ronchese | last post by:
Helllo. I need to return a XML from my WebService, but I'm not getting the result as a XML. I mean, instead of receiveing a tag (<xyy></xyz>), I'm receiving it encoded (with &lt; or &gt;). There is...
1
by: cday119 | last post by:
I have a Class with about 10 properties. All properties return right except for one. It is real annoying and I can't see why its not working. Maybe someone else can see something. It is the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.