access and set variables in a class from another class

限于喜欢 提交于 2019-12-02 09:33:36

I think it should be the other way round

protected void Page_Load(object sender, EventArgs e)
{
   SpCart cart = new SpCart();
   cart.updateCart(124, 4);

   tax = cart.getComputedTax();
   subTotal = cart.getSubTotal();
   ...
}

The idea is those variables should independent of your SpCart code.

public class Spcart
{     
     public void updatecart(int pid,int qty)
     {
         ---------/////some code
     }

     public int getComputedTax()
     {
       //can compute tax here
       int tax = whatever;
       return tax;
     }
}

The computation logics can still be separated into some other class

I think you are trying to access "tax" property declared in "Ui_ShoppingCart" from "Spcart" class. It is not possible to do it. Instead you have to pass them as additional parameters to updatecart method.

Spcart cart = new Spcart();
cart.updatecart(pid,qty,tax);

Or if tax is used in other methods of the "spcart" class, initialize it in the contructor.

public class Spcart
{     
 private int _tax = 0;
 public Spcart(int tax)
 {
   _tax = tax;
 }
 public void updatecart(int pid,int qty)
 {
    int amount = qty + _tax;
 }
}

And call using

Spcart cart = new Spcart(tax);
cart.updatecart(pid,qty);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!