.net - Why reference to a non-shared member of a Windows Form is allowed? -
we came across during problem determination within our code. we've fixed bug, question remains:
why reference non-shared member of "windows form" allowed?
in order replicate behaviour, create new windows forms application (i'm using vs2012, .net4.0), populate default form, form1.vb, with:
option strict on option explicit on public class form1 private _private string public readonly property notsharedproperty string return _private end end property end class
then add new class , call propertclass, fill with:
option strict on option explicit on public class properclass private _private string public readonly property notsharedproperty string return _private end end property end class
then add class, call exampleclass , fill it:
option strict on option explicit on public class exampleclass public sub new() if form1.notsharedproperty nothing '..................... end if if properclass.notsharedproperty nothing '..................... end if end sub end class
in exampleclass, properclass.notsharedproperty
gives me "reference non-shared member requires object reference." 1 expect. form1.notsharedproperty
not. why?
this 1 of many visual basic.net compatibility features.
when refer form type in old-school visual basic, reference default instance of form. that's how vb development done in past (and vb wasn't environment used convention). of time, had 1 instance of each form.
since you're not exploiting feature on purpose, never used default instance, , create own instances needed instead. in case, form1.notsharedproperty
return whatever default is.
Comments
Post a Comment