Solved

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

Posted on 2016-09-05
6
37 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
  • 3
  • 2
6 Comments
 
LVL 50

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 50

Accepted Solution

by:
Ryan Chong earned 500 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 50

Expert Comment

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

825 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