?
Solved

C# class - every time I add one to my code I get a "CS1513: } expected" error

Posted on 2016-09-05
6
Medium Priority
?
50 Views
Last Modified: 2016-10-23
I have a project that I'm working on that basically requires me to use C# code like PHP or classic ASP as opposed to using visual studio - part of that project includes adding a class - whenever I add any classes, I get "CS1513: } expected"

Guessing this is just an order / syntax thing - any advice you can provide would be greatly appreciated!

Thanks!

<%@ Page Language="C#" debug="true" %>
<%@ Import Namespace="System" %>
<%
class PointTest
{
    public int x; 
    public int y;
}

class MainClass4
{
    static void Main() 
    {
        PointTest p = new PointTest();
        // Direct access to public members:
        p.x = 10;
        p.y = 15;
        Response.Write("x = {0}, y = {1}", p.x, p.y); 
    }
}
// Output: x = 10, y = 15

// additional C# code here to do stuff 

%>

Open in new window

0
Comment
Question by:RSPANGLER_AVTIA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 41785506
you can try to have a frontend and code behind structure instead.

frontend:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="WebApplication_C.test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

Open in new window

code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication_C
{
    class PointTest
    {
        public int x;
        public int y;
    }

    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PointTest p = new PointTest();
            // Direct access to public members:
            p.x = 10;
            p.y = 15;
            Response.Write(String.Format("x = {0}, y = {1}", p.x, p.y)); 
        }
    }
}

Open in new window

0
 

Author Comment

by:RSPANGLER_AVTIA
ID: 41785541
Thanks Ryan,

Really appreciate it! I think I'm getting closer on this with the .cs page, but I'm confused as to how to structure the page to call the code appropriately.

Here's the actual code I'm calling in my .cs page now:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace calcdistance {

public static class GeoCodeCalc
{
    public const double EarthRadiusInMiles = 3956.0;
    public const double EarthRadiusInKilometers = 6367.0;

    public static double ToRadian(double val) { return val * (Math.PI / 180); }
    public static double DiffRadian(double val1, double val2) { return ToRadian(val2) - ToRadian(val1); }

    public static double CalcDistance(double lat1, double lng1, double lat2, double lng2) 
    {
        return CalcDistance(lat1, lng1, lat2, lng2, GeoCodeCalcMeasurement.Miles);
    }

    public static double CalcDistance(double lat1, double lng1, double lat2, double lng2, GeoCodeCalcMeasurement m) 
    {
        double radius = GeoCodeCalc.EarthRadiusInMiles;

        if (m == GeoCodeCalcMeasurement.Kilometers) { radius = GeoCodeCalc.EarthRadiusInKilometers; }
        return radius * 2 * Math.Asin( Math.Min(1, Math.Sqrt( ( Math.Pow(Math.Sin((DiffRadian(lat1, lat2)) / 2.0), 2.0) + Math.Cos(ToRadian(lat1)) * Math.Cos(ToRadian(lat2)) * Math.Pow(Math.Sin((DiffRadian(lng1, lng2)) / 2.0), 2.0) ) ) ) );
    }
}

public enum GeoCodeCalcMeasurement : int
{
    Miles = 0,
    Kilometers = 1
}

}

Open in new window


and here's what I have on the top of my main page (mainpage.aspx) that calls the .cs page (mainpage.aspx.cs)

<%@ Page Language="C#" debug="true" src="mainpage.aspx.cs" Inherits="calcdistance.GeoCodeCalcMeasurement" AutoEventWireup="true"%>

Open in new window


Right now it seems close, but I'm getting the following error:
Parser Error Message: 'calcdistance.GeoCodeCalcMeasurement' is not allowed here because it does not extend class 'System.Web.UI.Page'.

Thanks so much!!
0
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 2000 total points
ID: 41785572
no problem with an independent class with a different namespace, what you need is just to import and declare it accordingly in the code behind.

try this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="calcdistance.test" %>

Open in new window


then in code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace calcdistance
{
    class PointTest
    {
        public int x;
        public int y;
    }

    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PointTest p1 = new PointTest();
            // Direct access to public members:
            p1.x = 10;
            p1.y = 15;

            PointTest p2 = new PointTest();
            // Direct access to public members:
            p2.x = 110;
            p2.y = 27;
            
            Response.Write(String.Format("x1 = {0}, y1 = {1}<br>", p1.x, p1.y));
            Response.Write(String.Format("x2 = {0}, y2 = {1}<br>", p2.x, p2.y));

            Response.Write(String.Format("CalcDistance = {0}", GeoCodeCalc.CalcDistance(p1.x, p1.y, p2.x, p2.y)));
        }
    }
}

Open in new window

0
 

Author Comment

by:RSPANGLER_AVTIA
ID: 41789377
Thanks Ryan,

I'm still playing with a few variations (getting an error) - will report back today. Really appreciate your assistance!!

Cheers!
0
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 41790633
ok coool, let us know if you need further assistance cheers
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question