Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
69 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 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