Getting invalid token error but can't see why

T

Twanger

I'm getting a compiler error on my ASP.NET page and I can't see the
cause. I have a simple C# class compiled into a DLL and placed in my
bin directory which has a public property QuestionText. When I try
and set the property the compiler throws up

Compiler Error Message: CS1519: Invalid token '=' in class, struct, or
interface member declaration

If I don't try and set the property it prints out the default value of
the property fine.

my code is below

<%@ Page Language="C#" %>
<%@ import Namespace="MyNamespace" %>
<script runat="server">
PollQuestion pollQ = new PollQuestion();
pollQ.QuestionText = "My New Question";
</script>

<html>
<head>
</head>
<body>
<%=pollQ.QuestionText %>

</body>
</html>



The C# class is

[code:1:29c46e8760]
using System;

namespace MyNamespace
{
public class PollQuestion
{
// private members
string strQuestionText;

// empty constructor
public PollQuestion ()
{

}

// public accessors
public string QuestionText
{
get { return strQuestionText;}
set { strQuestionText = value; }
}
}
}

[/code:1:29c46e8760]

I've been trying to find the problem for a while now and so far
nobodys been able to help me.
 
M

mikeb

Twanger said:
I'm getting a compiler error on my ASP.NET page and I can't see the
cause. I have a simple C# class compiled into a DLL and placed in my
bin directory which has a public property QuestionText. When I try
and set the property the compiler throws up

Compiler Error Message: CS1519: Invalid token '=' in class, struct, or
interface member declaration

If I don't try and set the property it prints out the default value of
the property fine.

my code is below

<%@ Page Language="C#" %>
<%@ import Namespace="MyNamespace" %>
<script runat="server">
PollQuestion pollQ = new PollQuestion();
pollQ.QuestionText = "My New Question";
</script>

<html>
<head>
</head>
<body>
<%=pollQ.QuestionText %>

</body>
</html>

If you click the "Show Complete Compilation Source:" link on the error
page, you can see the C# source that the .aspx page is being parsed into
- this often helps with these kinds of problems.

In your case, the problem is that your script block gets parsed into the
..aspx page's class in the declarations block. So the first line (being
a declaration) is fine, but the next line is not.

Change you script block to something like:

<script runat="server">
PollQuestion pollQ = new PollQuestion();

public void Page_Load() {
pollQ.QuestionText = "My New Question";
}
</script>

so that the second line is inside the Page_Load() method.
 

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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top