vb.net - Using a IO variable before it has been assigned a value in a Try...Catch...Finally -



vb.net - Using a IO variable before it has been assigned a value in a Try...Catch...Finally -

i'm trying utilize io object in vb.net i'm not initialising declaration point. utilize object in several places in try..catch block , utilize 1 time again in block. purpose of question can ignore method calls of code. vs showing warning: variable 'objsourcedirectory' used before has been assigned value. null reference exception result @ runtime.

i believe having safe checks in places in code null reference shouldn't happen, how rid of warning? initialise variable empty @ declaration point can't possible io.directoryinfo, doesn't have construction without parameters. how can prepare this?

dim object1 io.directoryinfo seek mlstprocessedfiles = new global.system.collections.generic.list(of string) ' gather info import source object1 = new io.directoryinfo(object2.location) methodd(object2.location) methoda(object1) methodc(object2.location) ' work through files found in source. observe if file moved , add together manual processing each objfile in object1.getfiles() seek methodb(objfile, object2) objfile = nil grab objfileex filenotfoundexception grab objdirex directorynotfoundexception methodx(mstrerror_11_moved, objdirex.message) go on end seek next grab objectexception exception throw 'make sure processed files have been moved folders method1() method2() method3(object1) end seek

you haven't shown objsourcedirectory in code. assume object1 used in finally.

yes, cannot utilize variable when possible variable not assigned value. using in finally executed always, even on error.

since haven't assigned default value possible exception causes remain unassigned. compiler error prevents careless mistakes.

to prepare assign nothing:

dim object1 io.directoryinfo = nil

but should check if it's nothing in finally or in method3.

in case move initilization declaration makes assigned:

dim object1 io.directoryinfo = new io.directoryinfo(object2.location)

note compiler not smart plenty check if possible errors raised before variable initialized. looks @ try , "thinks": it's possible here comes error.

vb.net io try-catch

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -