Solved

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

Posted on 2016-09-05
6
41 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 51

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 51

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 51

Expert Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Input parameteres to DragOver 2 37
I need help adding validation to my MVC.Net view 6 23
How to force output to ascii 2 40
Set custom font WPF 2 21
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

733 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