0

I defined a class in the "Datasource.cs" file. In my another asp.net file "FineUIGrid.aspx.cs" I need to quote the class's method I defined in the Datasource.cs. But when I quote the method there is an error "the name 'class1' does not exist in the current context". How can solve this problem? Datasource.cs code:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

namespace FineUI.Examples
{

    public class Class1
    {
        public Class1()
        {
        }
       public string write() 
        { 
            string AD = "This is a FineUI DataGrid Example";
            return AD;
         }
     }
}

FineUIGrid.aspx.cs code:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace FineUIProject
{

    protected void page_load(object sender, EventArgs e) 
    {
        string information = Class1.write();
     }
}
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Michelle
  • 77
  • 4
  • If the string is used to describe the class, then instead of creating your own `write` method, you might consider overriding the `ToString` method in your class instead. This method is common to all .NET objects and is the preferred way to represent a class as a string. For example: `public override string ToString() { return "This is a FineUI DataGrid Example"; }`. Of course this is an instance method, so the clients would need to have an instance of your class in order to call it. – Rufus L Feb 07 '18 at 22:40
  • What does "quote the method" mean? – Scott Hannen Feb 08 '18 at 00:07

2 Answers2

1

You need to add a reference to FineUI inside FineUIProject then include the using statement below. After that you need to instantiate class1 and call it on the instanciated object

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text;
    using FineUI.Examples;

    namespace FineUIProject
    {

        protected void page_load(object sender, EventArgs e) 
        {
            Class1 classOne = new Class1();
            string information = classOne.write();
         }
    }
Muckeypuck
  • 549
  • 2
  • 14
1

Either instantiate the class or make the method static.

Instantiated:

var class = new Class1();
var information = class.write();

Static:

//Class1
public static string write() 
{ 
  string AD = "This is a FineUI DataGrid Example";
  return AD;
}

// no change
string information = Class1.write();

Since there is no member variables being used by Class1 used to return the result, a static method would most likely be the best course of action. You may want to read C# static vs instance methods.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150