c# - Nested class can't access "parent" variable -
please excuse lack of training/understanding in issue.
i have complex asp.net / c# package highly customized ecommerce store. i'll try simplify situation ease of understanding.
i have custom control , in control defined this:
public partial class conlib_custom_productdialog : system.web.ui.usercontrol
and within class this:
private class qtyboxtemplate : system.web.ui.itemplate
and within class addressing value of bool _eventselected with:
if (_eventselected)
which defined in outer class as:
public static bool _eventselected;
i don't want static because can cause "collisions" between website users since static variables global application. we've had problem before , take off static keyword , worked.
in case, if remove static error kinda understand:
cannot access non-static member of outer type conlib_custom_productdialog via nested type conlib_custom_productdialog.qtyboxtemplate
it seems since qtyboxtemplate nested can't access bool variable in parent class.
i found similar question here says , there 3 solutions:
1. make (method) variable static. 2. use inheritance instead of nested classes. 3. create instance of outer class.
the first problem since it's variable instead of method in original post, it's how i've been working far , didn't realize it.
as second, don't think inheritance option since qtyboxtemplate inheriting system itemplate.
lastly seems weird create instance of outer class access 1 variable , in inexperience don't know how work - when create instance, have pass around, etc. since it's custom control don't know if can done since isn't instantiated parent page on lives?
again, apologies asking question basic of part don't get. realize code complex , hope captured essence of enough make sense. appreciated.
update: trying add more info. have data grid each row product. inside grid row have drop down list quantity want purchase, 1 10. however, if have picked seats on tour , have selected date/time go can't change quantity or mess reservation system check _eventselected disable quantity ddl if case.
we using ddl quantity because it's mobile store , easier up/down arrows in original store. complex - commercial store package , 2 different contractors have worked on tie reservation system , have understand , make work. wish knew more .net/c#! trudging through explanation try help.
here's qtyboxtemplate class:
private class qtyboxtemplate : system.web.ui.itemplate { private datacontrolrowtype templatetype; private string columnname; public qtyboxtemplate(datacontrolrowtype type, string colname) { templatetype = type; columnname = colname; } public void instantiatein(system.web.ui.control container) { literal lc = new literal(); switch (templatetype) { case datacontrolrowtype.header: // build header column //lc.text = "<b>" + breakcamelcase(columnname) + "</b>"; lc.text = "<b>" + columnname + "</b>"; container.controls.add(lc); break; case datacontrolrowtype.datarow: //--plugables: need drop down list instead of quantity box dropdownlist ddlist = new dropdownlist(); ddlist.items.add(new listitem("0", "0")); ddlist.items.add(new listitem("1", "1")); ddlist.items.add(new listitem("2", "2")); ddlist.items.add(new listitem("3", "3")); ddlist.items.add(new listitem("4", "4")); ddlist.items.add(new listitem("5", "5")); ddlist.items.add(new listitem("6", "6")); ddlist.items.add(new listitem("7", "7")); ddlist.items.add(new listitem("8", "8")); ddlist.items.add(new listitem("9", "9")); ddlist.items.add(new listitem("10", "10")); ddlist.items.add(new listitem("20", "20")); ddlist.items.add(new listitem("40", "40")); ddlist.id = "quantity"; ddlist.font.size = fontunit.parse("18px"); if (_eventselected) ddlist.enabled = false; container.controls.add(ddlist); break; default: break; } } }
and lives inside class dozens of methods, classes, etc... way post here (2500+ lines) starts like:
public partial class conlib_custom_buytourproductdialoggalaxy : system.web.ui.usercontrol { #region class variables int _productid = 0; product _product = null; list<int> _selectedkitproducts = null; productvariantmanager _variantmanager; persistentcollection<productvariant> _availablevariants; datatable _datatable; productoptioncollection _prodoptions; private bool _showavailableseats = true; public datetime _selecteddate; public int _eventtypeid; public bool _collectrosterdata; public envelope _inventory; private string _dtformat = "yyyy-mm-dd hh:mm:ss"; public bool _eventselected;
since qtyboxtemplate
private can created inside conlib_custom_productdialog
class.
suggest add conlib_custom_productdialog
parameter qtyboxtemplate
constructor , set local variable. can access _eventselected
using reference.
public partial class conlib_custom_productdialog : system.web.ui.usercontrol { public bool _eventselected; public void somefunctionthatcreatesqtyboxtemplate() { var q = new qtyboxtemplate(this); //include reference q.seteventselected(); } private class qtyboxtemplate : system.web.ui.itemplate { private conlib_custom_productdialog _ccp; public qtyboxtemplate(conlib_custom_productdialog ccp) { _ccp = ccp; } public void seteventselected() { _ccp._eventselected = true; //access "parent" eventselected } } }
Comments
Post a Comment