如何将类的对象创建为文件后面的代码 [英] How to create the object for class into code behind file

查看:81
本文介绍了如何将类的对象创建为文件后面的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海,


我有在线购物应用程序.我有3个类class1,class2,captchaimage.我必须为此类创建对象到我的后端代码中.我试图创建对象.但是错误
即将出现找不到类型或名称空间名称"Class1"(您是否缺少using指令或程序集引用?).我该如何解决.下面是文件后面的代码.请帮助我

Hai,


I am having online shopping application.I have 3 classes class1,class2,captchaimage.I have to create object for this classes into my backend code.I tried to create object.But the error
"The type or namespace name ''Class1'' could not be found (are you missing a using directive or an assembly reference?)" is coming.How can I solve this.Below is a code behind file.Please help me

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Default2 : System.Web.UI.Page
{
    int flag = 0;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        bind_cart();
       // Response.Write(Session["category"]);
    }
    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void LinkButton6_Click(object sender, EventArgs e)
    {

    }

    protected void DataList1_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        LinkButton lbu = (LinkButton)e.CommandSource;      
        Response.Redirect("products.aspx?catname=" + lbu.Text);       
    }

    public void bind_cart()
    {
        Class1 ob = new Class1();----->I am getting error in this line
        ob.dt = (DataTable)Session["cart"];
        string str = "";

        if (ob.dt.Rows.Count == 0)
        {
            str = "_______________________________";

            ListBox1.Items.Add(str);
            str = "No Item Selected";
            ListBox1.Items.Add(str);
            str = "_______________________________";
            ListBox1.Items.Add(str);
        }
        else
        {
            str = "    " + "Product  " + "Quantity";
            ListBox1.Items.Add(str);
            str = "_______________________________";
            ListBox1.Items.Add(str);
            int index = 1;

            for (int j = 0; j <= ob.dt.Rows.Count - 1; j++)
            {
                DataRow dr = ob.dt.Rows[j];
                str = Convert.ToString(index) + ". " + Convert.ToString(dr["pname"]) + "  " + Convert.ToString(dr["qty"]);
                ListBox1.Items.Add(str);
                index++;

            }
            int total = Class2.gettotalprice();---->Getting error this line

            str = "_______________________________";
            ListBox1.Items.Add(str);
            str = "Total Amount=  " + total.ToString();

            ListBox1.Items.Add(str);
        }

    }
    
}

推荐答案

如果您的类具有其他名称空间,则此名称将无效.您需要做的是在代码背后引用类的名称空间.请考虑以下示例.

If your class has a different namespace, then this would not work. What you need to do is to reference the namespace of your class on your codebehind. Consider the following example.

using MyApp.Objects1; //take note that I referenced the namespace in order to have access to Class1

namespace MyApp.Objects2
{
   public class Class2
   {
      private Class1 _myClass1;
      public Class2()
      {
         _myClass1 = new Class1(); 
      }
   }
}

//Assuming this is the structure of Class1
namespace MyApp.Objects1 //take note of the namespace
{
   public class Class1
   {
      public Class1(){ }
   }
}


如果Class1在另一个程序集中,则首先需要在Web应用程序中添加对该类的引用.如果它在另一个名称空间中,请为这些名称空间添加"using"语句.
If Class1 is in another assembly, you will first need to add reference to that in your web application. If it is in another namespace, add a "using" statement for those namespaces.


这篇关于如何将类的对象创建为文件后面的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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