vb.net arraylist of objects -
i trying create arraylist of objects, , working fine except value of 11 objects same. i've have tried multiple ways of writing code, same outcome everytime.
arrfullprodlist collects name of products in database. working properly. arrproducts having issue objects being same.
what doing wrong?
declared
private objreader sqldatareader private objproducts new cproducts private arrfullprodlist arraylist = new arraylist public arrprodcuts arraylist = new arraylist
class cproduct
public class cproduct private _pstrprodid string private _pstrproddesc string private _psngwhcost single private _psngretprice single private _pblntaxable boolean private _isnewprod boolean public sub new() '_pstrprodid = "" '_pstrproddesc = "" '_psngwhcost = 0 '_psngretprice = 0 '_pblntaxable = false '_isnewprod = false end sub public property strprodid() string return _pstrprodid end set(strval string) _pstrprodid = strval end set end property public property strproddesc() string return _pstrproddesc end set(strval string) _pstrproddesc = strval end set end property public property sngwhcost() single return _psngwhcost end set(sngval single) _psngwhcost = sngval end set end property public property sngretprice() single return _psngretprice end set(sngval single) _psngretprice = sngval end set end property public property blntaxable() boolean return _pblntaxable end set(blnval boolean) _pblntaxable = blnval end set end property public property isnewprod() boolean return _isnewprod end set(blnval boolean) _isnewprod = blnval end set end property public readonly property getsaveparameters() arraylist dim paramlist new arraylist paramlist.add(new sqlclient.sqlparameter("prodid", _pstrprodid)) paramlist.add(new sqlclient.sqlparameter("proddesc", _pstrproddesc)) paramlist.add(new sqlclient.sqlparameter("whcost", _psngwhcost)) paramlist.add(new sqlclient.sqlparameter("retprice", _psngretprice)) paramlist.add(new sqlclient.sqlparameter("taxable", _pblntaxable)) return paramlist end end property public function save() integer 'return -1 if id exists , can't create new record if _isnewprod dim strres string = mydb.getsinglevaluefromsp("sp_checkprodidexists", _ new sqlclient.sqlparameter("prodid", _pstrprodid)) if not strres = 0 return -1 'id not unique!! end if end if 'if not new member or new , unique, save (update or insert) return mydb.execsp("sp_saveproduct", getsaveparameters) end function end class
class cproducts
imports system.data.sqlclient public class cproducts 'this class represents members table , associated business rules private _product cproduct 'constructor public sub new() 'instantiate cmember object _product = new cproduct end sub public readonly property currentobject() cproduct return _product end end property public sub clear() _product = new cproduct end sub public sub createnewproduct() 'call me when clearing screen create new member clear() _product.isnewprod = true end sub public function save() integer return _product.save end function public function getproductlist() sqldatareader return mydb.getdatareaderbysp("dbo.sp_getproductlist") end function public function getproducidlist() sqldatareader return mydb.getdatareaderbysp("dbo.sp_getproductidlist") end function public function getproductbyname(strproddesc string) cproduct dim params new arraylist dim param1 new sqlparameter("proddesc", strproddesc) params.add(param1) fillobject(mydb.getdatareaderbysp("dbo.sp_getproductbyname", params)) return _product end function public function getproductbyid(strprodid string) cproduct dim aparam new sqlparameter("prodid", strprodid) fillobject(mydb.getdatareaderbysp("dbo.sp_getproductbyid", aparam)) return _product end function public function fillobject(sqldr sqldatareader) cproduct using sqldr if sqldr.read _product .strprodid = sqldr.item("prodid") & "" .strproddesc = sqldr.item("proddesc") & "" .sngwhcost = sqldr.item("whcost") & "" .sngretprice = sqldr.item("retprice") & "" .blntaxable = sqldr.item("taxable") & "" end else 'failed reason end if end using return _product end function '----------start alex's code--------- public function getproductbydesc(strproddesc string) sqldatareader dim aparam new sqlparameter("proddesc", strproddesc) return mydb.getdatareaderbysp("dbo.sp_getproductbydesc", aparam) end function end class
main
private sub loadproducts() arrprodcuts.clear() objreader = objproducts.getproductbydesc(txtsearch.text) while objreader.read arrfullprodlist.add(objreader.item("proddesc")) end while objreader.close() = 0 arrfullprodlist.count - 1 dim anewprod cproduct anewprod = objproducts.getproductbyname(arrfullprodlist.item(i).tostring) arrprodcuts.add(anewprod) next end sub
Comments
Post a Comment