Solved

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

Posted on 2016-09-05
6
30 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 49

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 49

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 49

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now