constructor - How to create strongly typed list class in vb.net -
i hate admit new object oriented programming in vb.net. have class object called subscriber.vb works ok i'd create "set" or list of these objects. please me leverage following code create list of subscribers "consumer" loop through list of subscribers? here have far:
public class subscriber public sub new(byval thesubscriberid int32) dim sconndatabase string = configurationmanager.connectionstrings("databaseconnstring").connectionstring dim connection new sqlconnection(sconndatabase) dim cmd sqlcommand try cmd = new sqlcommand("getsubscriberinfo_v", connection) cmd.commandtype = commandtype.storedprocedure cmd.parameters.addwithvalue("@subscriberid", thesubscriberid) connection.open() dim objreader sqldatareader = cmd.executereader() while objreader.read() setobjectdata(objreader) loop objreader.close() connection.close() catch ex exception throw end try end sub private sub setobjectdata(byval theobjreader sqldatareader) try me._id = convert.toint32(theobjreader("subscriberid")) me._nameforlogon = theobjreader("subscribername").tostring() me._nameinfull = theobjreader("subscribernamefull").tostring() me._daysuntilexpired = convert.toint32(theobjreader("daysuntilexpired")) me._signupdate = theobjreader("signupdate") me._expirationdate = theobjreader("expirationdate") me._subscriberphone = theobjreader("subscriberphone").tostring() me._mostrecentrenewal = theobjreader("mostrecentrenewal") me._cumulativerevenue = convert.todecimal(theobjreader("cumulativerevenue")) me._numberofrenewals = theobjreader("numberofrenewals") me._subscriptionstatuscode = theobjreader("subscriptionstatuscode") me._subscriptionstatus = theobjreader("subscriptionstatus").tostring() me._notificationstatuscode = theobjreader("notificationstatuscode") me._notificationstatus = theobjreader("notificationstatus") catch ex exception throw end try end sub end class
i did not show getters , setters. has restricted visual studio 2008 unfortunately. few reasons, cannot upgrade environment.
what best practice here? add public class subscriberlist subscriber.vb file or should separate file? more importantly, stuck on how take have create proper list. caller create instance of subscriberlist object. please me started. thanks.
edit: here came idea (i'm thinking of adding overloaded constructors might filter data various ways...would practice?):
public class subscriberlist public sub new() dim sconndatabase string = configurationmanager.connectionstrings("databaseconnstring").connectionstring dim connection new sqlconnection(sconndatabase) dim cmd sqlcommand dim osubscriberlist new list(of subscriber) cmd = new sqlcommand("getsubscriberinfo_v", connection) cmd.commandtype = commandtype.storedprocedure connection.open() dim objreader sqldatareader = cmd.executereader() while objreader.read() dim id integer = objreader("subscriberid") dim s subscriber = new subscriber(id) osubscriberlist.add(s) loop objreader.close() connection.close() end sub end class
new error trying use:
dim allsubscribers new subscriberlist each subscriber in allsubscribers ' allsubscribers not declared next
why not declared ? confused rookie mistake sure...
edit (number 2): changed name subscriberlist subscribers plural & got working (see below) - puzzled advice remove database connection , query constructor(s) , place in separate class(es). picturing adding overloaded constructors subscriber (and subscribers). cannot imagine how constructors of each respective data.
public class subscribers implements ienumerable(of subscriber) #region "properties" public list new list(of subscriber) #end region public function getenumerator() ienumerator(of subscriber) _ implements ienumerable(of subscriber).getenumerator return list.getenumerator() end function private function getenumerator1() ienumerator _ implements ienumerable.getenumerator return list.getenumerator() end function public sub new() dim sconndatabase string = configurationmanager.connectionstrings("databaseconnstring").connectionstring dim connection new sqlconnection(sconndatabase) dim cmd sqlcommand cmd = new sqlcommand("select * dbo.subscriber_v", connection) cmd.commandtype = commandtype.text connection.open() dim objreader sqldatareader = cmd.executereader() while objreader.read() dim id integer = objreader("subscriberid") dim s subscriber = new subscriber(id) list.add(s) loop objreader.close() connection.close() end sub end class
in vb can make list of custom object.
dim osubscriberlist new list(of subscriber)
then can instantiate new subscribers , add them list
osubscriberlist.add('add object here')
this simple, quick , dirty way handle it. can create separate class create collection of object. "best" practices, if want follow solid programming principles , use test driven development, point towards making separate collection class deal it, isnt necessary.
edit: per comment below
you dont need create subscriberlist class. create regular list of subscribers , add them list so. wanting create list (form load, event, etc.)
dim osubscriberlist new list(of subscriber) dim sconndatabase string = configurationmanager.connectionstrings("databaseconnstring").connectionstring dim connection new sqlconnection(sconndatabase) dim cmd sqlcommand cmd = new sqlcommand("select * dbo.subscriber_v", connection) cmd.commandtype = commandtype.text connection.open() dim objreader sqldatareader = cmd.executereader() while objreader.read() osubscriberlist.add(new subscriber(objreader("subscriberid")) end while 'additional cleanup steps here
then can iterate on list so:
for each sub subscriber in osubscriberlist 'do next
Comments
Post a Comment