Results 1 to 10 of 10

Thread: Protected ?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769

    Protected ?

    I see there is a use of [qote]
    Code:
    Protected sub ...
    [/quote]

    What does this mean? Is this the equiv of friend?

    What about Overloads and Overrides??
    What the hell are these?

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Protected is sort of like Public but means it can only be accessed from tis own or an inherited class.


    Overrides means that it replaces a sub/function of the same name and runs the overrides version when the orginal would be called.

    Overloads allows you to define multiple subs/functions of the same name but different parameters. .NET will know which one to access depending on the parameter type(s) you pass.

    I think that is the easiest way to expoain them. Got it?
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769
    SWEAT!

    Does this mean that I can write a procedure to replace the class' initialize event that accepts parameters?

  4. #4
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    yes - you can do that. Although you use the "constructor" (replaces class initialize).

    You can also overload it, so that it can accept different parameters

    for example,
    VB Code:
    1. Public Sub New(ByVal strName As String)
    2.      ...
    3.    End Sub
    4.    Public Sub New(ByVal blnSwitch1 As boolean, blnSwitch2 as Boolean)
    5.      ...
    6.    End Sub
    Last edited by Bananafish; Jan 30th, 2002 at 04:35 AM.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769
    Sorry, I haven't had time to play with this yet, but will the calling application see the change when creating new objects?

    for example:

    Previously we did this:
    Code:
    set obj = new Class
    What do we do now if our object creation has parameters?

  6. #6
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    You would now be able to do this...

    VB Code:
    1. Dim objA as New clsClassA(strParam)

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769
    I also notice that when adding a button to a form, the onclick event appears with a new guy, "Handles".

    What is the significance of this?

  8. #8
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    Its the "Handles" bit that actually tells you the event (or events) that is being handled. The fact that the sub is called button1_click (or whatever it is) is actually irrelevent - you could rename it and the program wouldnt change.

    If you wanted the same method to handle more than one event - ie this is how you would do it. (there are no longer control arrays in vb.Net).

    For example,
    VB Code:
    1. Private Sub cmdNextPrior_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click, cmdPrior.Click
    2.  
    3.       Dim oButton As System.Windows.Forms.Button
    4.       oButton = CType(sender, System.Windows.Forms.Button)
    5.  
    6.       If oButton.Name = "cmdNext" Then
    7.          If mintOffset < mcolErr.Count - 1 Then
    8.             mintOffset = mintOffset + 1
    9.             DisplayException()
    10.          End If
    11.       End If
    12.  
    13.       If oButton.Name = "cmdPrior" Then
    14.          If mintOffset > 0 Then
    15.             mintOffset = mintOffset - 1
    16.             DisplayException()
    17.          End If
    18.       End If
    19.  
    20.    End Sub

    This code handles the click event of two buttons. The "sender" argument is used to determine which of the two buttons were pressed.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769
    This is pretty cool.

    But if there are no control arrays anymore, how do you create controls dynamicaly? And then how do you receive an event from the new control?

    I also noticed the use of

    System.Windows.Forms.Button

    What is the difference between this and

    Button


  10. #10
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    First things first.

    There is no difference between Button and System.Windows.Form.Button - it's a namespace thing. If your assembly imports System.Windows.Form, then you have no need to type System.Windows.Form before Button.

    And now for something completely different.
    An example of dynamically creating a control :-

    VB Code:
    1. Dim oNewButton as New Button
    2.  
    3. 'Set it up on the form
    4. oNewButton.Location = New System.Drawing.Point(200, 152)
    5. oNewButton.Size = New System.Drawing.Size(80, 32)
    6. oNewButton.Text = "Press Me"
    7.  
    8. 'Add it to the Forms control collection
    9. Me.Controls.Add(oNewButton)
    10.  
    11. 'Hook it up to the event handler
    12. AddHandler oNewButton.Click AddressOf Me.Button_Click

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