Getting invalid token error but can't see why

  • Thread starter Thread starter Twanger
  • Start date Start date
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.
 
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.
 
Back
Top