Results 1 to 8 of 8

Thread: [RESOLVED] [02/03] protected vs Dim

  1. #1

    Thread Starter
    Fanatic Member vishalmarya's Avatar
    Join Date
    Feb 2001
    Location
    New Delhi , INDIA
    Posts
    858

    Resolved [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.
    Vishal Marya, MCP .net 3.5
    My Site
    http://www.vstoolsgallery.com/
    http://visualstudiogallery.msdn.micr...b-f87a909b9266





    Please indicate what version of vb you use.
    Please mark your thread resolved using the Thread Tools above.
    -----------------------------------------

  2. #2
    Hyperactive Member Coool's Avatar
    Join Date
    Feb 2006
    Location
    System.Coool
    Posts
    333

    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

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [02/03] protected vs Dim

    Dim's a legacy BASIC keyword. You should use the explicit scope modifiers.

  4. #4

    Thread Starter
    Fanatic Member vishalmarya's Avatar
    Join Date
    Feb 2001
    Location
    New Delhi , INDIA
    Posts
    858

    Re: [02/03] protected vs Dim

    i know the basic difference regarding inheritance .

    but that does not answer my question.
    Vishal Marya, MCP .net 3.5
    My Site
    http://www.vstoolsgallery.com/
    http://visualstudiogallery.msdn.micr...b-f87a909b9266





    Please indicate what version of vb you use.
    Please mark your thread resolved using the Thread Tools above.
    -----------------------------------------

  5. #5

    Thread Starter
    Fanatic Member vishalmarya's Avatar
    Join Date
    Feb 2001
    Location
    New Delhi , INDIA
    Posts
    858

    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.
    Vishal Marya, MCP .net 3.5
    My Site
    http://www.vstoolsgallery.com/
    http://visualstudiogallery.msdn.micr...b-f87a909b9266





    Please indicate what version of vb you use.
    Please mark your thread resolved using the Thread Tools above.
    -----------------------------------------

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [02/03] protected vs Dim

    Quote 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

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] [02/03] protected vs Dim

    Whoops. I meant at class level.

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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 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
  •  



Click Here to Expand Forum to Full Width