Apr 9th, 2007, 03:32 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] [02/03] protected vs Dim
if i define a webusercontrol :
lets say ,
dim withevents Webusercontrol11 as Webusercontrol1
then use of any of its public property or method , generates an error
"Object reference not set to an instance of an object ."
if i use ,
protected withevents Webusercontrol11 as Webusercontrol1
it works fine
why ?
Last edited by vishalmarya; Apr 10th, 2007 at 08:58 AM .
Apr 9th, 2007, 05:12 AM
#2
Hyperactive Member
Re: [02/03] protected vs Dim
Dim means Private. And Protected mean variable can be accessible to It class and it's immediate child.
I am using .NET 2010 with Windows 7
Apr 9th, 2007, 05:28 AM
#3
Re: [02/03] protected vs Dim
Dim's a legacy BASIC keyword. You should use the explicit scope modifiers.
Apr 9th, 2007, 02:13 PM
#4
Thread Starter
Fanatic Member
Re: [02/03] protected vs Dim
i know the basic difference regarding inheritance .
but that does not answer my question.
Apr 9th, 2007, 02:40 PM
#5
Thread Starter
Fanatic Member
Re: [02/03] protected vs Dim
ok i got this one.
the answer lies in how aspx pages are processed.
http://msdn2.microsoft.com/en-us/lib...ontrols_topic4
When an aspx page is requested , a class is automatically created which is derived from the Web page's code-behind class (apx.vb).
this new class constructs the Web page's control hierarchy.
so any control which is declared as dim/private , will not appear in this hierarchy and hence reference to any such control will result in
"NullReferenceException" error .
so all controls on a web page needs to be declared protected ( at least )
Last edited by vishalmarya; Apr 9th, 2007 at 02:44 PM .
Apr 9th, 2007, 02:48 PM
#6
Re: [02/03] protected vs Dim
Originally Posted by
penagate
Dim's a legacy BASIC keyword. You should use the explicit scope modifiers.
it is??
then what do you use to declare a variable at the procedure level?
as in:
Code:
Private Sub test()
Dim A As String 'VALID
Private B As String 'ERROR
Friend C As String 'ERROR
Public D As String 'ERROR
Protected E As String 'ERROR
End Sub
Apr 9th, 2007, 02:54 PM
#7
Re: [RESOLVED] [02/03] protected vs Dim
Whoops. I meant at class level.
Apr 9th, 2007, 02:58 PM
#8
Re: [RESOLVED] [02/03] protected vs Dim
I guess you could consider it legacy at the class level, however I don't have any gripes about using Dim at the class level (others might)
When you use Dim at the class level, VB automatically assumes its private, infact if you mouse over it, you will see that it has a private modifier.
Attached Images
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width