Int.Parse And Int.TryParse Engine?

Sabarimahesh
Posted by Sabarimahesh under ASP.NET category on | Points: 40 | Views : 2601
Asp Source Code:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Label ID="Label4" runat="server" BorderColor="#FF3300"
BorderStyle="Groove" Font-Bold="True" Text=" int.Parse" Width="59px"></asp:Label>
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="Input Value"></asp:Label>
<br />
<asp:TextBox ID="TextBox1" runat="server" Height="21px" Width="128px"></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Convert To Int.parse" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="Convert To int.TryParse" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<br />
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Result"></asp:Label>
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />

</div>
</form>
</body>
</html>




C# Code

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string M = TextBox1.Text.ToString();

int result = Int32.Parse(M);

TextBox2.Text = result.ToString();

}


catch
{

string M = TextBox1.Text.ToString();
bool r1 = Convert.ToBoolean(float.Parse(M));
bool r2= Convert.ToBoolean(M = null);
bool r3 = Convert.ToBoolean(Convert.ToUInt32(M) );


if (r1==true)
{
TextBox2.Text = "ERROR:FormatException";

}
if (r2==true)
{
TextBox2.Text = "ERROR:ArgumentNullException";
}
if(r3==true)
{
TextBox2.Text = "ERROR:OverflowException";

}
}

}
protected void Button2_Click(object sender, EventArgs e)
{
string M = TextBox1.Text.ToString();
int result;

bool output = Int32.TryParse(M,out result);
TextBox2.Text = result.ToString();




}


}



If You What any Reference Regarding this
http://www.dotnetfunda.com/blogs/sabarimahesh/2152/difference-between-int32parse-int32tryparse-and-converttoint32

Comments or Responses

Posted by: Sangeetha Mani on: 3/1/2013 Level:Starter | Status: [Member] | Points: 10
ya its k to understand the tryparse but my suggestion is to explain the differentiation about parse and tryparse in text is easiy to recall for people who forgot about it.

Login to post response