Resolved Zero is stripped from the begining of integer

Status
Not open for further replies.

Jayson

Member
Joined
Jun 3, 2008
Messages
10
Programming Experience
1-3
Hi,

Am am using regex to find numbers matching certains patterns in directory names. These numbers will be either 8 or 10 chars in length.
Code:
Private Function RegEx(ByVal CurrPath As String) As Integer
        'Uses regex to see if the directory has a valid number.

        Dim mTen As Match = regexTen.Match(CurrPath)
        Dim mEight As Match = regexEight.Match(CurrPath)

        If mTen.Length > 1 Then
            'Return an 10 character number

            Return mTen.Value

        ElseIf mEight.Length > 1 Then
            'Return an 8 character number

            Return mEight.Value

        End If

    End Function
This is working fine. The problem I'm having is that if the function finds a number that begins with zero, when I assign that number to an integer, the zero is stripped of.

For example,
If my path is:
C:\someDir 01234567 blah blah\etc etc

I pass this into my RegEx function.

Dim myInt as integer = RegEx("C:\someDir 01234567 blah blah\etc etc")

The number is found and reported correctly within the method (mEight.Value) but as soon as it's assigned to the int the zero is stripped off.

E.g myInt = 1234567 instead of 01234567

I'm probably doing something realy dumb but just can't see what it is.

Thanks.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
The reason is because that's how integers work, there's no need for the beginning zeros when storing as a number.

Store it as a string instead, since it's part of a file name anyways.
 

Jayson

Member
Joined
Jun 3, 2008
Messages
10
Programming Experience
1-3
Thanks for the info. I figured there was something fundamental that I was missing. Much appreciated!
 

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,052
Programming Experience
10+
This is working fine. The problem I'm having is that if the function finds a number that begins with zero, when I assign that number to an integer, the zero is stripped of.
Just curious.. how many times in math class did you write your sums like this:

02 + 02 = 04


No, really.. Can you believe that you just asked why the leading zero on a numeric string, is "lost" when you convert it to a number?
 

Jayson

Member
Joined
Jun 3, 2008
Messages
10
Programming Experience
1-3
Well you'll have to forgive me for asking a "dumb question". Please try to keep in mind when you are next berating a newbie that we're not all as clever as you.
 

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,052
Programming Experience
10+
So you can write a regular expression that is capable of stripping a number out of the middle of a block of text using capturing groups, but then fail to understand why (in standard mathematical systems taught from primary school age) numbers dont have leading zeroes?

Also: berating?
 

Jayson

Member
Joined
Jun 3, 2008
Messages
10
Programming Experience
1-3
Look mate, I don't want to get into an on-line argument.

I'm still learning this stuff and am just doing my best. I'm sorry if I asked a dumb question but I will tell you something for free. No matter how good or bad I am at this stuff, I will "allways" be helpfull when ever I get the chance and I "won't" try to massage my ego at the expensive of someone else.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
Ok guys, it was a simple question that any new person could have overlooked. With that said, let it go.
 
Status
Not open for further replies.
Top Bottom