Here's my first beginner attempt at wrapping a normal zero-based list inside of a non-zero based list class. You more experienced programmers would be amazed at how long this took me to write, but I'm slowly learning to be able to write Cobra/.net code that will at least compile. (Think in time units of entire geological eras.) Note to nerdzero: writing in MD is a fantastic upgrade from a command line and Notepad++, even if breakpoints are ignored still.
I didn't see any point in using inheritance, since the normal list members would have to be scrutinized for NZB (non zero-based) compliance before being exposed, and the normal "[ ]" index brackets had to be rejected in favor of method calls, anyway, since they presumably can't accept negative index numbers. (Yes, I need negative non-zero-based index numbers.) And they can't be intercepted by a property in order to offset them back to their zero-based equivalent for processing inside the class.
Any comments? I haven't fully checked or debugged the logic yet, but the entire thing is obviously very simple and it does compile last I checked.
Also, is there a way to make the class more generic? It's a pain to rewrite the class for every data type that the list might hold.
Thanks for any unvarnished comments. I'm trying to learn, so go for it.
Note to Charles: nil-tracking is incredibly handy, and replaces all types of kludgy devices that I used to create. I use it all the time. Same with the meaningful error messages.
- Code: Select all
class NZBList # NZB or nzb = non-zero based
cue init( nzbBottomIndex as int, nzbTopIndex as int )
base.init
assert nzbBottomIndex <= nzbTopIndex
_offset = nzbBottomIndex
.populateList( nzbBottomIndex, nzbTopIndex )
var _nzbList = List<of int?>()
var _offset as int
def populateList( nzbBottomIndex as int, nzbTopIndex as int )
myCount = nzbTopIndex - nzbBottomIndex + 1
while myCount > _nzbList.count
_nzbList.add(nil)
assert myCount == _nzbList.count
def count as int
return _nzbList.count
def setValue(nzbIndex as int, value_ as int?)
_nzbList[ nzbIndex + _offset ] = value_
def getValue(nzbIndex as int) as int?
return _nzbList[ nzbIndex + _offset ]