Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从公共静态类调用函数_C#_Asp.net_Static Methods - Fatal编程技术网

C# 从公共静态类调用函数

C# 从公共静态类调用函数,c#,asp.net,static-methods,C#,Asp.net,Static Methods,我是ASP.NET新手 我真的需要帮助,因为我被卡住了。我在谷歌上读了很多书,不知道我做错了什么 错误:CS0103:名称“MyClass”在当前上下文中不存在 我有一个MyClass.cs文件,我喜欢在任何页面上重用该文件中的函数 using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MyNameSpace { public static class

我是ASP.NET新手

我真的需要帮助,因为我被卡住了。我在谷歌上读了很多书,不知道我做错了什么

错误:CS0103:名称“MyClass”在当前上下文中不存在

我有一个MyClass.cs文件,我喜欢在任何页面上重用该文件中的函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyNameSpace
{
    public static class MyClass
    {

        //
        // TODO: Add constructor logic here
        //

        public static string FormatDateNoTime(string input)
        {
            string thedate;
            DateTime strDate = DateTime.Parse(input);
            thedate = strDate.ToString("MM/dd/yyyy");
            return thedate;
        }

        public static string test()
        {
            return "1234";
        }

    }
}
另一个Page2.aspx.cs调用此函数,但出现错误

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

using MyNameSpace;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(MyClass.FormatDateNoTime("02/01/2012"));
    }
}
有人能帮忙吗,我只是想在任何页面上重复使用这个功能。

以下是我的建议

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyNameSpace
{
    public static class MyClass
    {

        //
        // TODO: Add constructor logic here
        //

        public static string FormatDateNoTime(string input)
        {
            string thedate;
            DateTime strDate = DateTime.Parse(input);
            thedate = strDate.ToString("MM/dd/yyyy");
            return thedate;
        }

        public static string test()
        {
            return "1234";
        }

    }
}
  • 您需要导入
    MyClass
    的命名空间,方法是包括
    使用namespaceOfMyClass
    或者调用方类和被调用的类都应属于同一命名空间

  • 有时,默认情况下不会编译该类。在解决方案资源管理器中的文件上单击鼠标右键,然后单击“属性”。检查
    构建操作
    属性,确保其显示“编译”

  • 从最顶层构建项目,例如解决方案

在这方面:

Response.Write(MyClass.FormatDateNoTime("02/01/2012"));
右键单击
MyClass
并选择“resolve”,其中应包含添加名称空间或使用类型的完全限定名的选项


如果“解析”不存在,请从包含Page2.aspx.cs的程序集添加对包含MyClass.cs的程序集的引用。

我遇到了相同的问题。我解决了重新启动VS的问题。重新启动后,带有问题类的cs文件在我的项目中不可见。我在解决方案资源管理器中将该文件再次添加到项目中,并构建了完整的项目

您是否删除了
\u Default
类应该位于的名称空间,或者这只是一个复制/粘贴错误?您在哪里声明了类?是在App_代码中还是在哪里?是的,我创建的MyClass.cs在App_代码文件夹中。我在一个示例项目中实现了你的代码,它工作得非常好。尝试清理和重建您的解决方案。还要注意您的输出窗口。这也是我被卡住的地方。我在解决方案中的文件上单击鼠标右键,然后选择“属性”。这里只有两个选项,一个是FileName=“MyClass.cs”,另一个是Full Path=“The Path”,我没有看到任何构建操作或编译选项。谢谢这听起来像是我遇到的问题“如果解决方案不存在,请添加对程序集的引用”。你能告诉我怎么做吗?对不起,我有一只猫。