|
 |
aspx_beginners thread: Type conversion fails in C#, work in vb?
Message #1 by "J House" <jesse@s...> on Sun, 18 Nov 2001 22:47:33
|
|
here is a simple form with 2 input fields, the code accepts these
parameters(which seem to always be strings), then converts them to
integers and adds them together.
for some reason if the code is in C# it throws the error:
Compiler Error Message: CS0030: Cannot convert type 'string' to 'int'
it works fine in vb.
on page 130 of professional asp.net there is a simple example
Age = (int)AgeString; which i assume works?
Can anyone tell me what is wrong?
//***************************************************************
//* C# code that fails, see vb code below that works
<%@ Page Language="c#" Debug="true" %>
<html>
<head>
<title>Test</title>
<script language="c#" runat="server">
// global vars
int sResult;
void Page_Load(Object Source, EventArgs E)
{
if (IsPostBack)
{
string iNum_1;
string iNum_2;
iNum_1 = Request.Params["num_1"];
iNum_2 = Request.Params["num_2"];
sResult = (int)iNum_1 + (int)iNum_2; // error is here
}
}
</script>
</head>
<body>
<br>
<form runat="server">
<b>Number 1:</b> <input type="text" name="num_1"><br>
<b>Number 2:</b> <input type="text" name="num_2"><br>
<input type="submit"><br>
<b>Result:</b> <% Response.Write (sResult); %>
</form>
</body>
</html>
// ****** End C# code
// ************************************************************
' *************************************************************
' VB Code
<%@ Page Language="vb" Debug="true" %>
<html>
<head>
<title>Test</title>
<script language="vb" runat="server">
' global vars
dim sResult as integer
Sub Page_Load(source as object, e as eventargs)
if (IsPostBack) then
dim iNum_1 as string
dim iNum_2 as string
iNum_1 = Request.Params("num_1")
iNum_2 = Request.Params("num_2")
sResult = CType(iNum_1, Integer) + CType(iNum_2, Integer)
end if
End Sub
</script>
</head>
<body>
<br>
<form runat="server">
<b>Number 1:</b> <input type="text" name="num_1"><br>
<b>Number 2:</b> <input type="text" name="num_2"><br>
<input type="submit"><br>
<b>Result:</b> <% Response.Write (sResult) %>
</form>
</body>
</html>
' End VB Code
' ******************************************************************
Message #2 by "Mitch Denny" <mitch.denny@w...> on Mon, 19 Nov 2001 21:58:13 +1100
|
|
J,
The Int32 class doesn't implement an explicit casting
operator for strings which is what the following
line implies:
(int)someString;
The Int32 class does however implement a convienient
parsing mechanism that will also account for varying
string encoding mechanisms like two byte character sets.
You can change the line to:
sResult = Int32.Parse(iNum_1) + Int32.Parse(iNum_2);
Tested and verified. Judging by the publish date of
the book, it might have been on the verge of when we
flipped over from BETA 1 to BETA 2 - I can't recall
the actual dates off the top of my head.
If that is the case, it might be possible that some
of the explicit casting allowed in BETA 1 was removed
in BETA 2. I do know that the above method is now
the prefered approach for most operations of this nature.
Hope this helps.
----------------------------------------
- Mitch Denny
- http://www.warbyte.com
- mitch.denny@w...
- +61 (414) 610-141
-
> -----Original Message-----
> From: J House [mailto:jesse@s...]
> Sent: Sunday, 18 November 2001 10:48 PM
> To: aspx_beginners
> Subject: [aspx_beginners] Type conversion fails in C#, work in vb?
>
>
> here is a simple form with 2 input fields, the code accepts these
> parameters(which seem to always be strings), then converts them to
> integers and adds them together.
> for some reason if the code is in C# it throws the error:
> Compiler Error Message: CS0030: Cannot convert type 'string' to 'int'
> it works fine in vb.
>
> on page 130 of professional asp.net there is a simple example
> Age = (int)AgeString; which i assume works?
>
> Can anyone tell me what is wrong?
> //***************************************************************
> //* C# code that fails, see vb code below that works
>
> <%@ Page Language="c#" Debug="true" %>
> <html>
> <head>
> <title>Test</title>
> <script language="c#" runat="server">
>
> // global vars
> int sResult;
>
> void Page_Load(Object Source, EventArgs E)
> {
> if (IsPostBack)
> {
> string iNum_1;
> string iNum_2;
>
> iNum_1 = Request.Params["num_1"];
> iNum_2 = Request.Params["num_2"];
>
> sResult = (int)iNum_1 + (int)iNum_2; // error is here
> }
> }
> </script>
> </head>
> <body>
> <br>
> <form runat="server">
>
> <b>Number 1:</b> <input type="text" name="num_1"><br>
> <b>Number 2:</b> <input type="text" name="num_2"><br>
> <input type="submit"><br>
> <b>Result:</b> <% Response.Write (sResult); %>
> </form>
> </body>
> </html>
> // ****** End C# code
> // ************************************************************
>
> ' *************************************************************
> ' VB Code
> <%@ Page Language="vb" Debug="true" %>
> <html>
> <head>
> <title>Test</title>
> <script language="vb" runat="server">
>
> ' global vars
> dim sResult as integer
>
> Sub Page_Load(source as object, e as eventargs)
> if (IsPostBack) then
> dim iNum_1 as string
> dim iNum_2 as string
>
> iNum_1 = Request.Params("num_1")
> iNum_2 = Request.Params("num_2")
>
> sResult = CType(iNum_1, Integer) +
> CType(iNum_2, Integer)
> end if
> End Sub
> </script>
> </head>
> <body>
> <br>
> <form runat="server">
>
> <b>Number 1:</b> <input type="text" name="num_1"><br>
> <b>Number 2:</b> <input type="text" name="num_2"><br>
> <input type="submit"><br>
> <b>Result:</b> <% Response.Write (sResult) %>
> </form>
> </body>
> </html>
>
> ' End VB Code
> ' ******************************************************************
>
> ---
> The Antechinus C# Editor
>
> Enables you to design, compile and run C# applications
> from the integrated environment. Version 4.1 has been
> upgraded for seamless integration with Microsoft's
> Beta 2 version of the .NET environment. Antechinus
> enables project generation and maintenance for the
> compilation of console and Windows programs, libraries
> and add-on modules and includes syntax coloring and
> syntax error navigation after compiling.
> http://adtracking.wrox.com/track.asp?x=NEWS&url=www.c-point.com
> ---
> You are currently subscribed to aspx_beginners
> as: mitch.denny@w...
> $subst('Email.Unsub')
>
Message #3 by "J House" <jesse@s...> on Mon, 19 Nov 2001 17:15:41
|
|
Thank you very much!
(int)someString;
You can change the line to:
sResult = Int32.Parse(iNum_1) + Int32.Parse(iNum_2);
Message #4 by "Al LeMay" <alemay@d...> on Mon, 19 Nov 2001 09:44:35 -0800
|
|
I would suggest using Convert function.
Convert.ToInt32 or Convert.ToInt16 (depending on the size of the
number).
Convert has most anything you will need.
Al LeMay
Virtuoso, Ltd.
Quality Assurance Manager
alemay@d...
-----Original Message-----
From: J House [mailto:jesse@s...]
Sent: Monday, November 19, 2001 9:16 AM
To: aspx_beginners
Subject: [aspx_beginners] RE: Type conversion fails in C#, work in vb?
Thank you very much!
(int)someString;
You can change the line to:
sResult =3D Int32.Parse(iNum_1) + Int32.Parse(iNum_2);
---
The Antechinus C# Editor
Enables you to design, compile and run C# applications
from the integrated environment. Version 4.1 has been
upgraded for seamless integration with Microsoft's
Beta 2 version of the .NET environment. Antechinus
enables project generation and maintenance for the
compilation of console and Windows programs, libraries
and add-on modules and includes syntax coloring and
syntax error navigation after compiling.
http://adtracking.wrox.com/track.asp?x=3DNEWS&url=3Dwww.c-point.com
---
You are currently subscribed to aspx_beginners
as: alemay@d...
$subst('Email.Unsub')
|
|
 |