访问类中的方法 [英] accessing a method inside a class

查看:102
本文介绍了访问类中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为DDLImages.cs的课程:

这是代码:



Hi, I have a class called DDLImages.cs:
This is the code:

public class DDLImage
{
    public string ddlImgPath;
    public string ddlText;
    public int ddlId;

    public List<DDLImage> GetDDLImage()
    {
        List<DDLImage> lstDDLImage = new List<DDLImage>();
        DDLImage objDDLImage;

        return lstDDLImage;
    }
}





这个名为ddlImages.aspx.cs的aspx文件:

这是代码隐藏文件:





and this an aspx file called ddlImages.aspx.cs:
This is the code behind file:

protected void Page_Load(object sender, EventArgs e)
{

    Literal ltr = new Literal();
    List<DDLImage> lstDDLImage = GetDDLImage();
    for (int i = 0; i < lstDDLImage.Count; i++)
    {
        ltr.Text = ltr.Text + "<span class='ddlText' id='" + lstDDLImage[i].ddlId + "' onclick='GetSelectedValue(this);'>"
            + lstDDLImage[i].ddlImgPath + lstDDLImage[i].ddlText + "<s/span>" + "<br/>";
    }
    effect.Controls.Add(ltr);


}





现在我收到一个错误,上面写着这个名字GetDDLImage在当前上下文中不存在。

从这行代码中引发错误



Now I am getting an error that says the name GetDDLImage doesnot exists in the current context.
The error is raised from this line of code

List<DDLImage> lstDDLImage = GetDDLImage();





但是当我从同一个文件(aspx.cs)中调用GetDDLImage时:





But when I call the GetDDLImage from the same file(aspx.cs) like this:

protected void Page_Load(object sender, EventArgs e)
   {

       Literal ltr = new Literal();
       List<DDLImage> lstDDLImage = GetDDLImage();
       for (int i = 0; i < lstDDLImage.Count; i++)
       {
           ltr.Text = ltr.Text + "<span class='ddlText' id='" + lstDDLImage[i].ddlId + "' onclick='GetSelectedValue(this);'>"
               + lstDDLImage[i].ddlImgPath + lstDDLImage[i].ddlText + "<s/span>" + "<br/>";
       }
       effect.Controls.Add(ltr);


   }
   public List<DDLImage> GetDDLImage()
   {
       List<DDLImage> lstDDLImage = new List<DDLImage>();
       DDLImage objDDLImage;

       return lstDDLImage;
   }



工作正常!!





现在我的kestion是为什么我不能直接从那个类访问它?而不是将GetDDLImage放在与aspx.cs文件相同的文件中。

我想直接访问它!


It is working!!


now my kestion is why I can't access it directly from that class? instead of placing the GetDDLImage in the same file as aspx.cs file.
I want to access it directly!

推荐答案

你只需要不知道典型的OOP语言的语法是什么样的,忘记它的语义。从一开始就学习C#。非常简单:它始终是 someType.methodName()(对于静态方法) someObject.methodName()(对于实例方法,如你的情况),但*。声明类的代码中隐含了前缀。对于实例方法,它意味着这个。



但是,你创建的实例方法完全没有意义,它应该是是静止的。而你的方法 GetDDLImage 没有任何用处,完全是多余的。此外,声明 DDLImage objDDLImage 永远不会被使用。



帮助你的唯一方法是说服你从一开始就开始学习一些编程。你准备好了吗?



-SA
You simply have no idea how syntax of a typical OOP language looks like, forget its semantics. Learn C# from the very beginning. Very briefly: it's always either someType.methodName() (for static methods) of someObject.methodName() (for instance method, as in your case), but "*." prefix is implied in the code of declaring class. For an instance method, it means this.

However, instance method you created is totally pointless, it should be static. And your method GetDDLImage does nothing useful, is totally redundant. Besides, the declaration DDLImage objDDLImage is never used.

The only way to help you is to convince you to start learning some programming from the very beginning. Are you ready?

—SA


您必须从中实例化DDLImage类DDLImage.aspx.cs。这样页面就可以看到该类并可以访问其方法。
You would have to instantiate the DDLImage class from you DDLImage.aspx.cs. That way the page can see that class and have access to its methods.


这篇关于访问类中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆