Can I validate that input is numeric without regular expressions?

L

-Lost

Response to "Peter Duniho said:
I was currently doing this:
scores.GetType().ToString() == typeof(String).FullName

So, it works to the extent that it will not allow me to declare
an instance of my custom class and pass string arguments to the
constructor.

I don't understand how this relates to your question. The code
you posted validates the argument as being a string (sort
of...to do that, you should actually use "scores is string"
rather than comparing the type names), not as being numeric.


If for example I was doing this:

myAverageClass average = new myAverageClass(100, "90", 80);

I wanted to be able to catch that. However, it disallowed me
being able to pass arguments via the command-line (as they were
all caught by it).


I still don't understand. You want to be able to catch what?


That a string was passed into it (yet I've come to realize that was
not the problem, the problem was no input that did not represent
numeric input).
What disallowed you being able to pass arguments via the
command-line?

The way I was originally checking if the argument was a string (at
the top of this post).
And what does constructing a new instance of "myAverageClass"
have to do with the command-line arguments?

It doesn't. I was referring to the way I was checking for strings
above. I couldn't pass anything via the command-line because
everything is a string from the command-line.

All in all, I'm probably just explaining everything very poorly. I
hope I cleared it up.

Thanks, Pete.
 
P

Peter Duniho

That a string was passed into it (yet I've come to realize that was
not the problem, the problem was no input that did not represent
numeric input).

I'm still not sure I understand. The proper way to ensure that you don't
get a string is to make sure the constructor parameter isn't a string.
Then the compiler won't let you pass a string to the constructor.
The way I was originally checking if the argument was a string (at
the top of this post).

If you make sure that the parameters are declared of the correct type,
then you never need to check the type at run-time at all.
It doesn't. I was referring to the way I was checking for strings
above. I couldn't pass anything via the command-line because
everything is a string from the command-line.

All in all, I'm probably just explaining everything very poorly. I
hope I cleared it up.

I'm not sure. I _think_ that maybe all of this is superfluous and that
you now know to use TryParse() to convert a string to the appropriate
type. But if any of the above is still relevant, and you still need or
desire help with some other aspect of the question, you'll have to try
explaining it some other way I think.

If you've gotten your program to work the way you want, then it probably
doesn't matter that I still don't get exactly what some of this thread was
about. :)

Pete
 
N

Nicholas Paldino [.NET/C# MVP]

The formats for hex and octal numbers have to be in the literal format
that VB expects, which means a leading "h" for hexidecimal (e.g. "h2f") or
"o" for octal (e.g. "o71").
 
C

christery

    The formats for hex and octal numbers have to be in the literal format
that VB expects, which means a leading "h" for hexidecimal (e.g. "h2f") or
"o" for octal (e.g. "o71").

--
          - Nicholas Paldino [.NET/C# MVP]
          - (e-mail address removed)




Yup, but didnt think hex would wor, and it didnt, if I didnt code
wrong
           x = Information.IsNumeric("987E+2"); //True
           Console.WriteLine("{0}", x);
           x = Information.IsNumeric("0x2B"); //False
           Console.WriteLine("{0}", x);
Maybe should be called is decimal ;)
happy regex on 987e+2
//CY- Dölj citerad text -

- Visa citerad text -

Thanks, forgot I wonderd back to VB land... like they said in some
movie "Im getting to old for this" 8)
//CY
 
C

christery

if (string.IsNullOrEmpty(myArgument))
throw new ArgumentNullException("...");


This doesn't tell you if input is numeric.


And then
That a string was passed into it (yet I've come to realize that was
not the problem, the problem was no input that did not represent
numeric input).

My understanding and deep admire the language of the mighty ale
brewers might be slighty off but checking for no input?
that sorta what I suggested by copying a nice solution found on the
web.

Thin this was a nice thread, sorry it ended so abruptly without a
regex solution to 234e+4 and 234,432.234, or hex, oct, roman XVIC,
binary fits well into decimal som I wont go there, just a problem to
know "there ar only 10 sort of programmers, those who know binary and
those who dont..."

//CY

//CY
 
L

-Lost

Response to (e-mail address removed):
My understanding and deep admire the language of the mighty ale
brewers might be slighty off but checking for no input?
that sorta what I suggested by copying a nice solution found on
the web.

I didn't word that well -- I meant that I didn't want anyone to
input something that could not possibly be interpreted as numeric.

For example, "zrx" was not allowed yet I (at the time) was not aware
of what I needed to do to accomplish that (or how to word it
properly).
Thin this was a nice thread, sorry it ended so abruptly without a
regex solution to 234e+4 and 234,432.234, or hex, oct, roman XVIC,
binary fits well into decimal som I wont go there, just a problem
to know "there ar only 10 sort of programmers, those who know
binary and those who dont..."

I'm not entirely sure what that meant, but just in case, I was not
looking for a handout. I simply wanted a better handle on my problem
which was I had not read far enough to answer the questions I had
come up with or enough to allow me to understand what was really
going on.

Parts of this thread and a bit of reading on the items mentioned here
actually solved all of my problems.

Have a good day.
 
C

christery

Response to (e-mail address removed):



I didn't word that well -- I meant that I didn't want anyone to
input something that could not possibly be interpreted as numeric.

For example, "zrx" was not allowed yet I (at the time) was not aware
of what I needed to do to accomplish that (or how to word it
properly).


I'm not entirely sure what that meant, but just in case, I was not
looking for a handout.  I simply wanted a better handle on my problem
which was I had not read far enough to answer the questions I had
come up with or enough to allow me to understand what was really
going on.

Parts of this thread and a bit of reading on the items mentioned here
actually solved all of my problems.

Have a good day.

Not to mention that we all now remember that exponential numbers and
hexadecimal numbers are numeric inputs ;)
To put the question just right is problematic, at the least, me runing
it is worse... but still is "IV" != 4
//CY
 

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

Top