--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
"djozy" <anonymous@disc ussions.microso ft.com> wrote in message
news:10dee01c3b f1a$d28803d0$a6 01280a@phx.gbl. ..[color=blue]
> Please, how can I convert selected item from dropdown list
> to integer?
> Thank you[/color]
int myInt = System.Convert. ToInt32 ( name of your item )
Dan.
"djozy" <anonymous@disc ussions.microso ft.com> wrote in message
news:10dee01c3b f1a$d28803d0$a6 01280a@phx.gbl. ..
Please, how can I convert selected item from dropdown list
to integer?
Thank you
If you are sure that the values of the dropdown are numbers you can do
like:
int val = Convert.ToInt32 ( DropDownControl . SelectedValue );
If you are not then you can wrap the above line in a try/catch block.
Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"djozy" <anonymous@disc ussions.microso ft.com> wrote in message
news:10dee01c3b f1a$d28803d0$a6 01280a@phx.gbl. ..[color=blue]
> Please, how can I convert selected item from dropdown list
> to integer?
> Thank you[/color]
Is there a CanConvert like method that returns a bool instead of an
exception. The problem with wrapping in try is the wait time for the
exception.
--
William Stacey, MVP
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:#2XwdHyvDH A.2136@TK2MSFTN GP10.phx.gbl...[color=blue]
> Hi,
>
> If you are sure that the values of the dropdown are numbers you can do
> like:
> int val = Convert.ToInt32 ( DropDownControl . SelectedValue );
>
> If you are not then you can wrap the above line in a try/catch block.
>
>
> Cheers,
>
> --
> Ignacio Machin,
> ignacio.machin AT dot.state.fl.us
> Florida Department Of Transportation
>
> "djozy" <anonymous@disc ussions.microso ft.com> wrote in message
> news:10dee01c3b f1a$d28803d0$a6 01280a@phx.gbl. ..[color=green]
> > Please, how can I convert selected item from dropdown list
> > to integer?
> > Thank you[/color]
>
>[/color]
AFAIK there is not such a method in the framework, what you can do is
include the VB.NET DLL which define a IsNumeric function.
Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"William Stacey" <staceywREMOVE@ mvps.org> wrote in message
news:%23jwCgBzv DHA.1740@TK2MSF TNGP12.phx.gbl. ..[color=blue]
> Is there a CanConvert like method that returns a bool instead of an
> exception. The problem with wrapping in try is the wait time for the
> exception.
>
> --
> William Stacey, MVP
>
> "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >[/color]
wrote[color=blue]
> in message news:#2XwdHyvDH A.2136@TK2MSFTN GP10.phx.gbl...[color=green]
> > Hi,
> >
> > If you are sure that the values of the dropdown are numbers you can do
> > like:
> > int val = Convert.ToInt32 ( DropDownControl . SelectedValue );
> >
> > If you are not then you can wrap the above line in a try/catch block.
> >
> >
> > Cheers,
> >
> > --
> > Ignacio Machin,
> > ignacio.machin AT dot.state.fl.us
> > Florida Department Of Transportation
> >
> > "djozy" <anonymous@disc ussions.microso ft.com> wrote in message
> > news:10dee01c3b f1a$d28803d0$a6 01280a@phx.gbl. ..[color=darkred]
> > > Please, how can I convert selected item from dropdown list
> > > to integer?
> > > Thank you[/color]
> >
> >[/color]
>
>[/color]
You might also use Regex as it should be fairly simple to check for the
integers.
--
Miha Markic - DXSquad/RightHand .NET consulting & software development
miha at rthand com
"William Stacey" <staceywREMOVE@ mvps.org> wrote in message
news:%23jwCgBzv DHA.1740@TK2MSF TNGP12.phx.gbl. ..[color=blue]
> Is there a CanConvert like method that returns a bool instead of an
> exception. The problem with wrapping in try is the wait time for the
> exception.
>
> --
> William Stacey, MVP
>
> "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >[/color]
wrote[color=blue]
> in message news:#2XwdHyvDH A.2136@TK2MSFTN GP10.phx.gbl...[color=green]
> > Hi,
> >
> > If you are sure that the values of the dropdown are numbers you can do
> > like:
> > int val = Convert.ToInt32 ( DropDownControl . SelectedValue );
> >
> > If you are not then you can wrap the above line in a try/catch block.
> >
> >
> > Cheers,
> >
> > --
> > Ignacio Machin,
> > ignacio.machin AT dot.state.fl.us
> > Florida Department Of Transportation
> >
> > "djozy" <anonymous@disc ussions.microso ft.com> wrote in message
> > news:10dee01c3b f1a$d28803d0$a6 01280a@phx.gbl. ..[color=darkred]
> > > Please, how can I convert selected item from dropdown list
> > > to integer?
> > > Thank you[/color]
> >
> >[/color]
>
>[/color]
Hi Ignacio,
one problem with VB IsNumeric: does not explicitly check for integers.
that means IsNumeric() does also return true, if your string is a double(or
float). that's not always what yout want.
Reinhold
(Sorry for bad english)
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >
schrieb im Newsbeitrag news:OpRgoJzvDH A.536@tk2msftng p13.phx.gbl...[color=blue]
> Hi William,
>
> AFAIK there is not such a method in the framework, what you can do is
> include the VB.NET DLL which define a IsNumeric function.
>
>
> Cheers,
>
> --
> Ignacio Machin,
> ignacio.machin AT dot.state.fl.us
> Florida Department Of Transportation
>
> "William Stacey" <staceywREMOVE@ mvps.org> wrote in message
> news:%23jwCgBzv DHA.1740@TK2MSF TNGP12.phx.gbl. ..[color=green]
> > Is there a CanConvert like method that returns a bool instead of an
> > exception. The problem with wrapping in try is the wait time for the
> > exception.
> >
> > --
> > William Stacey, MVP
> >
> > "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >[/color]
> wrote[color=green]
> > in message news:#2XwdHyvDH A.2136@TK2MSFTN GP10.phx.gbl...[color=darkred]
> > > Hi,
> > >
> > > If you are sure that the values of the dropdown are numbers you can[/color][/color][/color]
do[color=blue][color=green][color=darkred]
> > > like:
> > > int val = Convert.ToInt32 ( DropDownControl . SelectedValue );
> > >
> > > If you are not then you can wrap the above line in a try/catch block.
> > >
> > >
> > > Cheers,
> > >
> > > --
> > > Ignacio Machin,
> > > ignacio.machin AT dot.state.fl.us
> > > Florida Department Of Transportation
> > >
> > > "djozy" <anonymous@disc ussions.microso ft.com> wrote in message
> > > news:10dee01c3b f1a$d28803d0$a6 01280a@phx.gbl. ..
> > > > Please, how can I convert selected item from dropdown list
> > > > to integer?
> > > > Thank you
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]
Using this you will also need to check for overflow :)
Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Miha Markic" <miha at rthand com> wrote in message
news:er5acMzvDH A.2492@TK2MSFTN GP12.phx.gbl...[color=blue]
> Hi William,
>
> You might also use Regex as it should be fairly simple to check for the
> integers.
>
> --
> Miha Markic - DXSquad/RightHand .NET consulting & software development
> miha at rthand com
>
>
> "William Stacey" <staceywREMOVE@ mvps.org> wrote in message
> news:%23jwCgBzv DHA.1740@TK2MSF TNGP12.phx.gbl. ..[color=green]
> > Is there a CanConvert like method that returns a bool instead of an
> > exception. The problem with wrapping in try is the wait time for the
> > exception.
> >
> > --
> > William Stacey, MVP
> >
> > "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >[/color]
> wrote[color=green]
> > in message news:#2XwdHyvDH A.2136@TK2MSFTN GP10.phx.gbl...[color=darkred]
> > > Hi,
> > >
> > > If you are sure that the values of the dropdown are numbers you can[/color][/color][/color]
do[color=blue][color=green][color=darkred]
> > > like:
> > > int val = Convert.ToInt32 ( DropDownControl . SelectedValue );
> > >
> > > If you are not then you can wrap the above line in a try/catch block.
> > >
> > >
> > > Cheers,
> > >
> > > --
> > > Ignacio Machin,
> > > ignacio.machin AT dot.state.fl.us
> > > Florida Department Of Transportation
> > >
> > > "djozy" <anonymous@disc ussions.microso ft.com> wrote in message
> > > news:10dee01c3b f1a$d28803d0$a6 01280a@phx.gbl. ..
> > > > Please, how can I convert selected item from dropdown list
> > > > to integer?
> > > > Thank you
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]
You're slightly missing the point then if the only check you have is an
exception.
Exceptions are meant to catch errors that are unforseen, and while .Net (in
particular) c# and VB.Net seem to use more exceptions for returning errors
(rather than a bool or hresult), good code (i.e. code that's past the beta
stage of testing and in the RC) should never hit an exception.
wrapping code in try/catch is essential for reporting to you when things
don't go as you'd expect... if you leave the door open (ie an alpha numeric
text box) for user errors then there should be tighter restrictions made on
what the user can do to prevent this from happening.
If you hit a situation where you're trying to access an object for a
conversion to int and it can't be a converted to an int, then your code
needs reviewing, and perhaps tighter controls implemented, but the exception
should still be there to catch anything that goes wrong.
In this case, the initial enquiry has little weight since an exception is
more of a debugging facility when the unexpected errors occur, rather than
handling everything that goes wrong that could go wrong, because it's not
been catered for.
Hope that helps.
Dan.
"William Stacey" <staceywREMOVE@ mvps.org> wrote in message
news:%23jwCgBzv DHA.1740@TK2MSF TNGP12.phx.gbl. ..
Is there a CanConvert like method that returns a bool instead of an
exception. The problem with wrapping in try is the wait time for the
exception.
--
William Stacey, MVP
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:#2XwdHyvDH A.2136@TK2MSFTN GP10.phx.gbl...[color=blue]
> Hi,
>
> If you are sure that the values of the dropdown are numbers you can do
> like:
> int val = Convert.ToInt32 ( DropDownControl . SelectedValue );
>
> If you are not then you can wrap the above line in a try/catch block.
>
>
> Cheers,
>
> --
> Ignacio Machin,
> ignacio.machin AT dot.state.fl.us
> Florida Department Of Transportation
>
> "djozy" <anonymous@disc ussions.microso ft.com> wrote in message
> news:10dee01c3b f1a$d28803d0$a6 01280a@phx.gbl. ..[color=green]
> > Please, how can I convert selected item from dropdown list
> > to integer?
> > Thank you[/color]
>
>[/color]
<"Daniel Bass" <I'm really @ sick of spam>> wrote:[color=blue]
> the regex is probably one of the more efficient ways of confirming the
> number...[/color]
Not really. Going through the characters in the string and checking for
digits is *far* more efficient.
Search in groups.google.c om for a thread called "Checking if a string
can be converted to Int32" in microsoft.publi c.dotnet.framew ork. The
results from a bit of benchmarking were (in times for checking 9
strings 100,000 times each):
Just using an exception: 1m 16s
A hand-crafted checking routine: 0.7s
Double.TryParse (for first pass removals): 41s
Regular expression: 43s
The VB "IsNumeric" method: 1m 7s
Regular expressions are incredibly powerful things, but for simple
tests like this, hand-crafted code is almost always going to be more
efficient.
Thanks. That was point of checking for isnumeric first, so you don't have
to hit the exception.
--
William Stacey, MVP
"Daniel Bass" <I'm really @ sick of spam> wrote in message
news:OtPRIazvDH A.1088@tk2msftn gp13.phx.gbl...[color=blue]
> You're slightly missing the point then if the only check you have is an
> exception.[/color]
....
Thats good info. I would think that after you got that far, converting to a
number yourself could be faster then calling int.parse() or the like.
--
William Stacey, MVP
"Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
news:MPG.1a4159 b176e0ec9989bf7 @msnews.microso ft.com...[color=blue]
> <"Daniel Bass" <I'm really @ sick of spam>> wrote:[color=green]
> > the regex is probably one of the more efficient ways of confirming the
> > number...[/color]
>
> Not really. Going through the characters in the string and checking for
> digits is *far* more efficient.
>
> Search in groups.google.c om for a thread called "Checking if a string
> can be converted to Int32" in microsoft.publi c.dotnet.framew ork. The
> results from a bit of benchmarking were (in times for checking 9
> strings 100,000 times each):
>
> Just using an exception: 1m 16s
> A hand-crafted checking routine: 0.7s
> Double.TryParse (for first pass removals): 41s
> Regular expression: 43s
> The VB "IsNumeric" method: 1m 7s
>
> Regular expressions are incredibly powerful things, but for simple
> tests like this, hand-crafted code is almost always going to be more
> efficient.
>
> --
> Jon Skeet - <skeet@pobox.co m>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too[/color]
Comment