.net - How to properly structurate the visibility of this Class? -
i'm trying improve old class i've wrote manage ini file, class contains 3 sub-classes (file
, key
, section
) separate , organize procedures (procs ini in general, procs manage keys/values, , procs manage section names).
well, problem have in old class members shared (props/vars/objects/methods) , obviouslly result in disimbiguation, seek perfectionate visibility of members, , there i'm stuck.
the current usage of class this:
inifilemanager.filepath = "ini filepath" dim iniexist boolean = inifilemanager.file.exist
and usage should this:
dim ini new inifilemanager("ini filepath", textencoding) dim iniexist boolean = ini.file.exist dim another_ini new inifilemanager("another ini filepath without kind of conflict first instance", textencoding) dim another_iniexist boolean = another_ini.file.exist
below relevant code example, i'm stuck on exist
method of file
class 'cause cannot access filepath
variable @ top-level class since don't set variable , exist
method both shared
did on old class version...
...so how can improve this?
note: please maintain in mynd other 2 sub-classes should have method named exist
, other methods equal names such "[get]
", not in file
class (i don't know if problem need more retouches).
''' <summary> ''' manages ini file , it's sections load/save values. ''' </summary> public class inifilemanager #region " properties " ''' <summary> ''' indicates initialization file location. ''' </summary> private property filepath string = string.empty ''' <summary> ''' indicates initialization file encoding read/write. ''' </summary> private property textencoding system.text.encoding = system.text.encoding.default #end part #region " constructors " ''' <summary> ''' initializes new instance of <see cref="inifilemanager" /> class. ''' </summary> ''' <param name="inifile"> ''' indicates initialization file location. ''' </param> ''' <param name="textencoding">indicates textencoding read/write iniinitialization file.</param> public sub new(optional byval inifile string = nothing, optional byval textencoding system.text.encoding = nothing) if not string.isnullorempty(inifile) me.filepath = inifile else me.filepath = io.path.combine(application.startuppath, process.getcurrentprocess().processname & ".ini") end if if not textencoding nil me.textencoding = textencoding end if end sub #end part ''' <summary> ''' contains set of procedures manage ini file in general way. ''' </summary> private class [file] ''' <summary> ''' checks whether initialization file exist. ''' </summary> ''' <returns>true if initialization file exist, otherwise false.</returns> public function exist() boolean homecoming io.file.exists(mybase.filepath) end function ' more irrelevant methods here need access props , vars of top-level class... end class ' class here... ' , class here... end class
i think part of problem revealing rather handling much detail of hierarchy thru class. other part clinging 1980's 16bit windows tech when there much more robust mechanisms around today.
you have 4 classes (inimanager, file, section, key) manage 2 bits of info (key , value). since inimanager "house" others, can combined file - since there arent many file level operations. not need [sections]. existed store repeated attributes similar items like:
[maindb] path = file = foo = [report db] path = file = foo =
these intended provide initialization values in way loop thru collection of strings file1, file2... , read multiple sections in loop. simple set of settings, utilize single [default] section simplify class , use. then, downwards inimanager , keys. purposely exposing underlying hierarchy doesnt lend usability, imo.
so want, need sections property on inimanager
exposes section related stuff. back upwards that, need inisection
class (mainly methods sections) , inisections
collection class. (some comments lead me surmise want load sections time , keys can deleted etc).
if really want sections().keys().method
, have add together key
class , keys collection class
on inisection
. bring grand total 5 classes manage 2 pieces of information. of course, can done half code , 1 class. of fluff there expose inner workings way mentioned. you'll have fun public vs friend maintain revealing things dont want to.
i dont have in code pinvokes; question deals class construction not ini management. methods empty , exist see how end poor user.
public class inimanager ' gory pinvokes go here friend cfgfile string public property iniexists boolean ' bit seemed missing ' collection property exposed @ inimgr level ' containing collection of sections. matryoshka dolls, within ' each collection of keys , values public property sections inisections ' no reason ini mgr exist without file public sub new(inifile string) cfgfile = inifile _iniexists = system.io.file.exists(cfgfile) _sections = new inisections(cfgfile) end sub ' worthwhile thing can think of "file" ' class ever do. public sub removefile() end sub public sub save() ' think need delete file first ' deleted sections disappear. of course of study sections code ' inquire doesnt harm either ' iterate inisections phone call save there, ' iterates keys 1 1 save them sections.save(cfgfile) end sub ' ****** inisections class collection public class inisections 'inherits collection(of inisection) private items collection(of inisection) private cfgfile string friend sub new(file string) cfgfile = file ' assuming comments ' loading entire file manage it. that: if system.io.file.exists(cfgfile) ' load getprivateprofilesectionnames collection ' mybase.items.add(section_name)...then each s inisection in items s.loadkeyvalues(cfgfile) next end if end sub ' friend! friend sub save(cfgfile string) each s inisection in items ' instruct each section write kvps s.save(cfgfile) next end sub ' dont know why empty accessor showing in intellisense default public readonly property item(name string) inisection if indexofsection(name) = -1 items.add(new inisection(name)) end if homecoming items(indexofsection(name)) end end property ' add together section public function add(name string) inisection dim sec new inisection(name) items.add(sec) homecoming sec end function ' remove section public sub remove(name string) items.removeat(indexofsection(name)) ' real way remove section rewrite file! ' back upwards method have load sections , keys ' time if dont need them can write ' out whole file omitting removed keys , sections. ' ' sir, kind of junk went dustbin rubik's cubes end sub public function exists(secname string) homecoming indexofsection(secname) <> -1 end function private function indexofsection(name string) integer n integer = 0 items.count - 1 ' s/b tolowerinvariant - makes screen scroll if items(n).sectionname.tolower = name.tolower homecoming n end if next homecoming -1 end function end class end class ' ************** inisection item class public class inisection ' methods go here sections, ' "host" keys collections private mykeys dictionary(of string, string) ' .keys collection (why calling code want ' mess whole collection???), alter add together key class ' , keys collection ' interface keys public property keys(name string) string if mykeys.containskey(name) homecoming mykeys(name) else homecoming "" end if end set(value string) if mykeys.containskey(value) mykeys(value) = value else mykeys.add(value, value) end if end set end property public property sectionname string public sub new(name string) sectionname = name mykeys = new dictionary(of string, string) end sub public sub removekey(name string) if mykeys.containskey(name) mykeys.remove(name) end if end sub friend sub save(inifile string) ' iterate keys writitng kvps ini end sub ' note friend called inisection class not user friend function loadkeyvalues(inifile string) integer ' presumably phone call getprivateprofilesection ' sectionname , parse ' current key=value pairs mykeys homecoming mykeys.count end function end class
sample syntax:
ini = new inimanager("c:\temp\ziggy.ini") dim foo string = ini.sections("foo").keys("bar") ini.sections("ziggy").keys("foo") = "zoey" ini.sections("ziggy").removekey("zacky")
these dont match syntactically because didnt create key class , keys collection class (5 classes 2 bits of info insane). alter setter matches, remove keys accessor , add together .readkey()
, setkey
matches syntactically , maintain keys collection internal. you'll end with:
ini.sections("ziggy").removekey("zacky") ini.sections("ziggy").readkey("ziggy") ini.sections("ziggy").setkey(keyname, "zoey")
at to the lowest degree match syntactically
ini.sections.add("ziggy") ini.sections.remove("zoey") if ini.sections.exists("zacky") console.beep() end if ' causes cascade ini -> sections -> keys save ini.save()
.net vb.net class class-structure
No comments:
Post a Comment