C# switch statement

G

Guest

Hi All... I am a beginning .NET developer and am using C# with ASP.NET. I am
having trouble with switch statements, can anyone help?

When I run the below code, I receive an error "Compiler Error Message:
CS1519: Invalid token 'switch' in class, struct, or interface member
declaration"

<%@ Page Language="C#" %>

<script runat="server">
void Page_Load(Object Sender, EventArgs e);
string FirstName = "David";

switch (FirstName) {
case "David":
Response.Write("David");
break;
case "Michael":
Response.Write("Michael");
break;
default:
Response.Write("Not David or Michael");
break;
}
}
</script>

<html><body>
</html></body>
 
D

David Browne

David Whitfield said:
Hi All... I am a beginning .NET developer and am using C# with ASP.NET. I
am
having trouble with switch statements, can anyone help?

When I run the below code, I receive an error "Compiler Error Message:
CS1519: Invalid token 'switch' in class, struct, or interface member
declaration"

<%@ Page Language="C#" %>

<script runat="server">
void Page_Load(Object Sender, EventArgs e);

Page_Load was ended by the ';'. so FirstName is a class-level variable, and
'switch' is in the class declaration, where it is invalid.

You really should use code-behind your C# code, so your IDE can help you
with the language. Integrating server-side source code in the .aspx file is
supported, but should generally be avoided.

string FirstName = "David";

switch (FirstName) {
.. . .

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top