vb.net - How to use instance of New Object in With... Block -


dim objects new list(of object) new object    .prop1 = "property 1"    .prop2 = "property 2"     objects.add(.instance) 'i mean instance of new object end 

is possible.

i ask new question because last question has mislead information , don't give right answer. here code.

no not possible. with statement creates implicit variable. can variable access members , there no member returns reference object itself.

if want succinct code create, populate , add object list this:

mylist.add(new sometype {.someproperty = somevalue,                               .someotherproperty = someothervalue}) 

interestingly, can make work way wanted if create own extension method. under impression not extend object class either wrong or has changed because tried in vb 2013 , worked. can write method this:

imports system.runtime.compilerservices  public module objectextensions      <extension>     public function self(of t)(source t) t         return source     end function  end module 

and this:

with new sometype     .someproperty = somevalue     .someotherproperty = someothervalue     mylist.add(.self()) end 

i'm not sure that provides benefit though, given availability of object initialiser syntax demonstrated first.

hmmm... realised that's not extending object class. original intention try realised generic method better because return same type call on. did test non-generic method extending type object , did still worked though.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -