Click here to Skip to main content
15,914,642 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I have a class called DDLImages.cs:
This is the code:

C#
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;
    }
}


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

C#
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);


}


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();


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

C#
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;
   }

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!
Posted
Updated 16-Apr-14 10:49am
v2
Comments
[no name] 16-Apr-14 15:13pm    
Because you would need an instance of the DDLImage class before you could call a method contained within the class.

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
 
Share this answer
 
v2
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900