1

So I have two functions and I'm getting an interesting problem. Essentially I'm aiming to make my code more portable in an easily includeable cs file.

Here's said cs file:

namespace basicFunctions {
public partial class phpPort : System.Web.UI.Page {
    public string includer(string filename) {
        string path = Server.MapPath("./" + filename);
        string content = System.IO.File.ReadAllText(path);
        return content;
    }
    public void returnError() {
        Response.Write("<h2>An error has occurred!</h2>");
        Response.Write("<p>You have followed an incorrect link. Please double check and try again.</p>");
        Response.Write(includer("footer.html"));
        Response.End();
    }
}
}

Here is the page that is referencing it:

<% @Page Language="C#" Debug="true" Inherits="basicFunctions.phpPort" CodeFile="basicfunctions.cs" %>
<% @Import Namespace="System.Web.Configuration" %>

<script language="C#" runat="server">
void Page_Load(object sender,EventArgs e) {
    Response.Write(basicFunctions.phpPort.includer("header.html"));
    //irrelevant code
    if ('stuff happens') {
        basicFunctions.phpPort.returnError();
    }
    Response.Write(basicFunctions.phpPort.includer("footer.html"));
}
</script>

The error I'm getting is the one listed above, namely:

Compiler Error Message: CS0120: An object reference is required for the non-static field, method, or property 'basicFunctions.phpPort.includer(string)'

2 Answers2

2

You need an instance of your phpPort class as it and all the methods you have defined on it are not static.

Since you are on the aspx page that inherits from this class, when it loads it already is an instance of the class and you can call the methods on it directly.

You need to modify your code to use the functions directly:

void Page_Load(object sender,EventArgs e) {
    Response.Write(includer("header.html"));
    //irrelevant code
    if ('stuff happens') {
        returnError();
    }
    Response.Write(includer("footer.html"));
}
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • The OP has an instance- `this` – Chris Shain May 08 '12 at 19:12
  • basicFunction seems namespace in this case. – Tilak May 08 '12 at 19:13
  • This has fixed it (I'll mark it correct when it allows me to). Now I'm having the issue of `An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Server.get'` on Server.MapPath. Any ideas? –  May 08 '12 at 19:17
  • @user798080 - From what I can see, that should simply work (since your class inherits from `System.Web.UI.Page`). – Oded May 08 '12 at 19:29
  • I made a new page for the new code and error [link]http://stackoverflow.com/questions/10505187/an-object-reference-is-required-for-the-non-static-field-method-or-property-s[/link]. Thanks! –  May 08 '12 at 19:30
0

If you want to call basicFunctions.phpPort.includer as a static method you need the static keyword on it as follows:

public static void returnError
public static string includer

If you're not doing a static call, your base page needs to call from "this"

if ('stuff happens') {
    this.returnError();
}
Thinking Sites
  • 3,494
  • 17
  • 30