Greetings
I read (somewhere) that when you see several instances of the SAME code it is agood indication that it can be wrapped up in a function (?)...here is an example from my study materials, how can I reduce this code? Please provide (at least a) breif explanation. Thanks in advance.
<%@ Page language="vb" Explicit="true" strict="true"%>
<script runat="server" >
dim varNum1 as double
dim varNum2 as double
dim results as double
sub btnAdd_Click(sender as object, e as eventargs)
varNum1 = Cint(txtNum1.Text)
varNum2 = Cint(txtNum2.Text)
results = varNum1 + varNum2
lblShowResults.text = results.toString()
end sub
sub btnSub_Click(sender as object, e as eventargs)
varNum1 = Cint(txtNum1.Text)
varNum2 = Cint(txtNum2.Text)
results = varNum1 - varNum2
lblShowResults.text = results.toString()
end sub
sub btnMult_Click(sender as object, e as eventargs)
varNum1 = Cint(txtNum1.Text)
varNum2 = Cint(txtNum2.Text)
results = varNum1 * varNum2
lblShowResults.text = results.toString()
end sub
sub btnDiv_Click(sender as object, e as eventargs)
varNum1 = Cint(txtNum1.Text)
varNum2 = Cint(txtNum2.Text)
results = varNum1 / varNum2
lblShowResults.text = results.toString()
end sub
</script>
<!-- ##########################
### -->
<!-- [ begin user interface ] -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>TYSASP.NET: Day_01 - Calculator App</title>
</head>
<body>
<!-- begin web form -->
<form runat="server">
<table align="center" cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
<td colspan="2" align="left" valign="top" width="100%" id="tblHDR_id">
<!-- #include virtual="/pkdnotepad/inclu
des/header
.inc" -->
</td>
</tr>
<tr>
<td align="left" valign="top" width="20%">
<!-- #include virtual="/pkdnotepad/inclu
des/leftna
v.inc" -->
</td>
<td align="left" valign="top" width="80%">
<h2>Day: 02 - Exercises 1,2</h2>
<p class="intructions">Instru
ctions:</p
>
<p>1.) Create a simple page which accepts 2 number inputs
from the user and displays their product when the
submit button is clicked. Use 2 textbox controls,
a label control and a Submit button control.
Also, use the standard event handler code. Try using
the CInt function to convert the text in the textboxes
to Integers.</p>
<p>2.) Build upon the previous exercise by providing multiple Submit buttons that perform the baisc arithmetic operations, essentially creating a calculator.</p>
<!-- [ controls interface ] -->
<table align="left" border="0" cellspacing="1" cellpadding="1">
<tr>
<td>First Number:</td>
<td><asp:textbox id="txtNum1" width="30" runat="server" /></td>
</tr>
<tr>
<td>Second Number:</td>
<td><asp:textbox id="txtNum2" width="30" runat="server" /></td>
</tr>
<tr>
<td></td>
<td>
<asp:button id="btnAdd" size="20" text=" + " runat="server" onClick="btnAdd_Click" />
<asp:button id="btnSub" size="20" text=" - " runat="server" onClick="btnSub_Click" />
<asp:button id="btnMult" size="20" text=" * " runat="server" onClick="btnMult_Click" />
<asp:button id="btnDiv" size="20" text=" / " runat="server" onClick="btnDiv_Click" />
</td>
</tr>
<tr>
<td>Results:</td>
<td><asp:label id="lblShowResults" runat="server" font-size="20pt" /></td>
</tr>
</table>
<!-- [/ controls interface ] -->
</td>
</tr>
<tr>
<td colspan="2" align="left" valign="top" width="100%" id="tblFTR_id">
<!-- #include virtual="/pkdnotepad/inclu
des/footer
.inc" -->
</td>
</tr>
</table>
</form>
<!-- [ end user interface ] -->
<!-- ##########################
### -->
</body>
</html>