You'd think this works, but it doesn't 
Author Message
 You'd think this works, but it doesn't

I've been seeing this a lot in my code. I try to convert strings to
some type of integer and it gives me trouble... Why should this code
not work? Someone please tell me! The compiler breaks on the first
line, the "If" line... now since the Remove function returns the
string, and it is removing the $ from it (the only non-integer
character), you'd think it'd nicely convert... But it doesnt.

        If System.Convert.ToDecimal(t_Cost.Text.Remove("$", "")) > 0
Then
            Me.t_markup.Text =
System.Math.Round(System.Convert.ToDecimal(t_Price.Text.Remove("$",
"")), 2)
        End If

?

SX



Sat, 04 Dec 2004 17:02:33 GMT  
 You'd think this works, but it doesn't

Quote:

> I've been seeing this a lot in my code. I try to convert strings to
> some type of integer and it gives me trouble... Why should this code
> not work? Someone please tell me! The compiler breaks on the first
> line, the "If" line... now since the Remove function returns the
> string, and it is removing the $ from it (the only non-integer
> character), you'd think it'd nicely convert... But it doesnt.

>         If System.Convert.ToDecimal(t_Cost.Text.Remove("$", "")) > 0
> Then
>             Me.t_markup.Text =
> System.Math.Round(System.Convert.ToDecimal(t_Price.Text.Remove("$",
> "")), 2)
>         End If

You should be calling t_Cost.Text.Replace("$", "") rather than Remove.

--
Jacob Grass
Microsoft .NET MVP
Check out http://windowsforms.net



Sun, 05 Dec 2004 00:00:35 GMT  
 You'd think this works, but it doesn't
From the help:

String.Remove Method

Deletes a specified number of characters from this instance beginning at a
specified position.

ByVal startIndex As Integer, ByVal count As Integer

Parameters
  startIndex
    The position in this instance to begin deleting characters.
  count
    The number of characters to delete.

"$" is not an integer value, which the first paramter of remove "startIndex"
must be, so the compiler gives you an error.  In addition, an empty string
"" is not a valid integer value, which the second paramter "count" must be.


Quote:
> I've been seeing this a lot in my code. I try to convert strings to
> some type of integer and it gives me trouble... Why should this code
> not work? Someone please tell me! The compiler breaks on the first
> line, the "If" line... now since the Remove function returns the
> string, and it is removing the $ from it (the only non-integer
> character), you'd think it'd nicely convert... But it doesnt.

>         If System.Convert.ToDecimal(t_Cost.Text.Remove("$", "")) > 0
> Then
>             Me.t_markup.Text =
> System.Math.Round(System.Convert.ToDecimal(t_Price.Text.Remove("$",
> "")), 2)
>         End If

> ?

> SX



Mon, 06 Dec 2004 22:58:36 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. fRefreshLinks Doesn't work if path doesn't exist

2. 'Next Comment' doesn't work

3. Code doesn't work (from Dev's site)

4. WebResponse Webbot Doesn't work with ASP's

5. ASP Returning a DIB Image (I think) from SQL , Netscape doesn't display it

6. DDE'd CloseDatabase of Access doesn't work

7. 2 apostrophe fix doesn't work for just 's

8. ASP Returning a DIB Image (I think) from SQL , Netscape doesn't display it

9. Mouse Wheel doesn't work in Visual Studio's IDE enviornment...HELP

10. regsvr32 doesn't think much of me

11. Can't delete record--Error #3200--handler doesn't work

12. VB6's MSComm doesn't work well with Vista

 

 
Powered by phpBB® Forum Software