![]() |
ASP.Net Tutorial #5
Welcome to the fifth ASP.Net tutorial (I really must find a better way to start these tutorials). I want to start with something that is very useful which is called case...switch (if you are familiar with Visual Basic or older versions of BASIC then you will know it as case...select). It allows a decision to be made without using a whole lot of if...else statements as follows:-
using System;<%@ Import namespace="System" %>
<html>
<head>
<title>Switch</title>
</head>
<body>
<H2>Switch</H2>
</body>
</html>
<script language="c#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
int i=4;
switch(i)
{
case 1:
Response.Write("The value is 1" + "<BR>");
break;
case 2:
Response.Write("The value is 2" + "<BR>");
break;
case 3:
Response.Write("The value is 3" + "<BR>");
break;
default:
Response.Write("The value isn't 1, 2 or 3" + "<BR>");
break;
}
}
</script>
C# differs from earlier languages in that it insists on the use of the break keyword in each case statement. So how does it work? An integer value is set up which is coded to conatain the number 4. The case statements will therefore branch at the default statement and writeline the relevent value - if it had been set to 1, 2 or 3 then the corresponding writeline will be used and the program will end. Try changing the value of i and recompile and see what happens. This can also be used with strings or characters as follows:-
<%@ Import namespace="System" %>
<html>
<head>
<title>Switch 2</title>
</head>
<body>
<H2>Switch 1</H2>
</body>
</html>
<script language="c#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
char c='H';
switch(c)
{
case 'h':
Response.Write("The value is h" + "<BR>");
break;
case 'H':
Response.Write("The value is H" + "<BR>");
break;
case 'e':
Response.Write("The value is e" + "<BR>");
break;
default:
Response.Write("The value isn't h, H or e" + "<BR>");
break;
}
}
</script>
Note that the test is case sensitive (although using toUpper or toLower would cure the problem). As I said, a very useful construct. Now on to two different concepts that may at first seem unrelated but they do lead on into the next tutorial. The first is struct as folows:-
<%@ Import namespace="System" %>
<html>
<head>
<title>Struct</title>
</head>
<body>
<H2>Struct</H2>
</body>
</html>
<script language="c#" runat="server">
public struct MyPerson
{
public String first;
public String last;
}
private void Page_Load(object sender, System.EventArgs e)
{
MyPerson personName;
personName.first = "Nick";
personName.last = "James";
Response.Write(personName.first + "<BR>");
Response.Write(personName.last + "<BR>");
}
</script>
A struct (shorthand for structure) is not unlike an array in that it can contain data which can then be added to or accessed. This first example creates a structure containing 2 variables then can then be accessed from the main program. The name of the structure is used to create a reference to it (no such things as pointers in C# apparently...mmmm...) which is called personName which creates an instance of the structure in memory. The individual elements of the structure are accessed using a dot followed by the name of the element. So to update the first name we use personName.first followed by the string to enter into it.
Now it gets interesting. I wanted to create a structure like I have in the past using standard C as follows:-
struct MyPerson{
char first[25];
char last[25];
} personName[50];
This creates an array of fifty structures with each element fixed to 25 characters each which can be accessed using:-
personName[i].first="Nick"
so we can have a database of 50 people to call on. Seems quite reasonable that C# (which is based largely on C) would be the same. It isn't. Take a look at this:-
<%@ Import namespace="System" %>
<%@ Import namespace="System.Runtime.InteropServices" %>
<html>
<head>
<title>Struct 2</title>
</head>
<body>
<H2>Struct 2</H2>
</body>
</html>
<script language="c#" runat="server">
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct MyPerson
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 25)] public String first;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 25)] public String last;
}
private void Page_Load(object sender, System.EventArgs e)
{
MyPerson[] personName = new MyPerson[50];
personName.Initialize();
personName[1].first = "Nick";
personName[1].last = "James";
Response.Write(personName[1].first + "<BR>");
Response.Write(personName[1].last + "<BR>");
}
</script>
This C# program does the same as the C program above it. What is all this stuff? We need the interop service so we can use the MarshalAs command further down. The layout of the structure is then defined although in some case it may be alright to remove it all. Now we want to fix the size of each array element to 25 characters. This is achieved using the MarshalAs code which sets the type of string and the size which strikes me as being a bit ungainly. To be fair, it has been changed in C# 2.0 (that comes with version 2.0 of the .Net Framework) to make the assigning of fixed length strings much easier but I haven't tried it as I'm still using Framework version 1.1 but you use it with a command such as:-
public struct MyPerson
{
public String first[25];
public fixed String last[25];
}
although it will have to be marked as unsafe code. I'll try to do an update on this when I get the Framework 2.0 installed and running. On now to functions.
<%@ Import namespace="System" %>
<%@ Import namespace="System.Runtime.InteropServices" %>
<html>
<head>
<title>Function</title>
</head>
<body>
<H2>Function</H2>
</body>
</html>
<script language="c#" runat="server">
public static int AddItems(int Value1, int Value2)
{
int ReturnValue;
ReturnValue=Value1 + Value2;
return ReturnValue;
}
private void Page_Load(object sender, System.EventArgs e)
{
int k;
k=AddItems(1,2);
Response.Write(k + "<BR>");
}
</script>
Functions were what made the C programming language one of the most useful. A function is a little like a subroutine except that information can be passed to it and information can be passed out of it again back to calling program. So here we have a function that will add 2 numbers and return the result of the calculation. Note that while Value1 and Value2 are part of the function decalration, it isn't necessary to declare them inside the function. However, within the function is the ReturnValue which we need to pass back to the calling program so it is declared.
So thats it with functions and structures. But it is possible to combine them both into...Classes...see you next time...