Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. [VB10] Don't allow access to Form properties from Class

[VB10] Don't allow access to Form properties from Class

Scheduled Pinned Locked Moved Visual Basic
questionvisual-studiotutorial
14 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    Make it private. That would still mean that users can access the property, just makes it a bit harder; one would have to resort to reflection.

    Bastard Programmer from Hell :suss:

    T Offline
    T Offline
    The Mighty Atom
    wrote on last edited by
    #3

    Lol, you're Dutch too! :D Anyway, just to make sure i understand, i need to change:

    Public MyDialogClass

    To:

    Private MyDialogClass

    VS2010 does'nt like it that way, it's whining about 'Types declared 'Private' must be inside another type' Im not sure what that means, im quite new to writing my own classes.

    Virtual Space Shuttle Astronaut

    L 1 Reply Last reply
    0
    • T The Mighty Atom

      Let's say i have class called MyDialogClass. This class uses a form with it's own set of custom properties and methods. To declare one, you do this:

      Dim MyDialogInstance As New MyDialogClass

      How can i prevent my users from accessing the default properties that belongs with the form? For example, users can change the backcolor of the form by typing this:

      MyDialogInstance.BackColor = Color.Red

      I don't want that. I only want the MyDialogInstance to show my custom properties and methods in the IntelliSense dropdown menu and omit all the default properties.

      Virtual Space Shuttle Astronaut

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #4

      If depends on what you've done. I'll assume you just created your own dialog by inheriting from Form. You cannot change the access level of the properties and methods by overriding them and it's just too much work to cover all of them anyway. I'd probably hide the Dialog class you made by declaring the class as Friend instead of public, then create a wrapper class that creates it's own internal instance of your Dialog class. Expose only the methods and properties you want through the wrapper class. For example, when you need to show the dialog, that's going to be a method exposed by your wrapper class that calls the internal Dialogs ShowDialog method, waiting for it to return. Then the wrapper class just returns whatever ShowDialog returned.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      T L 2 Replies Last reply
      0
      • D Dave Kreskowiak

        If depends on what you've done. I'll assume you just created your own dialog by inheriting from Form. You cannot change the access level of the properties and methods by overriding them and it's just too much work to cover all of them anyway. I'd probably hide the Dialog class you made by declaring the class as Friend instead of public, then create a wrapper class that creates it's own internal instance of your Dialog class. Expose only the methods and properties you want through the wrapper class. For example, when you need to show the dialog, that's going to be a method exposed by your wrapper class that calls the internal Dialogs ShowDialog method, waiting for it to return. Then the wrapper class just returns whatever ShowDialog returned.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        T Offline
        T Offline
        The Mighty Atom
        wrote on last edited by
        #5

        What exactly is a wrapper? A class inside a class? Are you willing to make an example for me with comments between the code?

        Virtual Space Shuttle Astronaut

        D 1 Reply Last reply
        0
        • T The Mighty Atom

          Lol, you're Dutch too! :D Anyway, just to make sure i understand, i need to change:

          Public MyDialogClass

          To:

          Private MyDialogClass

          VS2010 does'nt like it that way, it's whining about 'Types declared 'Private' must be inside another type' Im not sure what that means, im quite new to writing my own classes.

          Virtual Space Shuttle Astronaut

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          The Mighty Atom wrote:

          Lol, you're Dutch too!

          Yup :)

          The Mighty Atom wrote:

          Anyway, just to make sure i understand, i need to change

          That makes the complete class "private"; you could do that if you wanted to hide the class entirely (hiding it in a public class, for example) You'd want to hide a property, so you'll have to make that property private. That would look roughly like this;

          Private Property SomeColor As Color
          Get
          Return Me._someColor
          End Get
          Set
          Me._someColor = value
          End Set
          End Property

          Now, if you try to look for the "BackColor" propery, then it won't be there in your class. I'm guessing that it's a property that's coming from a control that you're inheriting (Form is a control, and it has a backcolor) In that case, you could try to "overwrite" it by creating your own readonly-property that Shadows the inherited property, and hide it using attributes like BrowsableAttribute and EditorBrowsableAttribute. Here's a complete example project, inheriting from a panel, but works the same when inheriting from a form;

          Public Class Form1
          Private MyPanel As New MyPanelClass ' we create a new variable from our class
          Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
          MyPanel.Parent = Me
          MyPanel.Dock = DockStyle.Fill
          MsgBox(MyPanel.BackColor.ToString) ' Exception, since MyPanel.BackColor returns Nothing
          End Sub
          End Class
          Public Class MyPanelClass
          Inherits Panel ' let's extend the panel

           \_
           \_
          Shadows ReadOnly Property BackColor
              Get
                  Return Nothing
              End Get
          End Property
          

          End Class

          The new property effectively hides the old one that gets inherited from the Panel class. The BrowseAble attribute hides in the designer, the EditorBrowsable hides it from intellisense. It still compiles if you use the property (you can't cut remove it), but it's hidden and cannot be set anymore - and would only return "Nothing".

          :suss:

          1 Reply Last reply
          0
          • D Dave Kreskowiak

            If depends on what you've done. I'll assume you just created your own dialog by inheriting from Form. You cannot change the access level of the properties and methods by overriding them and it's just too much work to cover all of them anyway. I'd probably hide the Dialog class you made by declaring the class as Friend instead of public, then create a wrapper class that creates it's own internal instance of your Dialog class. Expose only the methods and properties you want through the wrapper class. For example, when you need to show the dialog, that's going to be a method exposed by your wrapper class that calls the internal Dialogs ShowDialog method, waiting for it to return. Then the wrapper class just returns whatever ShowDialog returned.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #7

            +5, that's the cleaner and recommended solution :)

            Bastard Programmer from Hell :suss:

            D 1 Reply Last reply
            0
            • T The Mighty Atom

              What exactly is a wrapper? A class inside a class? Are you willing to make an example for me with comments between the code?

              Virtual Space Shuttle Astronaut

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #8

              ...sigh... This would be your custom dialog form:

              Friend Class MyDialogForm
              Inherits Form
              .
              . yada, yada, yada
              .
              End Class

              Public Class MyDialog
              Implements IDisposable

              ' This keeps an instance of your dialog internal to
              ' this class, hiding EVERYTHING about it from the consumer.
              
              Private \_dialogForm As New MyDialogForm
              
              ' Now you just have to give the consumer exactly the things
              ' you want to expose.
              
              Public Function ShowDialog() As DialogResult
                  Return \_dialogForm.ShowDialog()
              End Function
              
              ' You can expose any other properties you want.  You can even
              ' use the internal form object as a backing field for your
              ' properties, where appropriate.
              
              Public Property BackColor As Color
                  Get
                      Return \_dialogForm.BackColor
                  End Get
                  Set(value as Color)
                      \_dialogForm.BackColor = Value
                  End Set
              End Property
              
              ' A form shown with ShowDialog has to be Disposed when you're
              ' done with it, so in the Dispose code, just look for the line
              ' that says "TODO: dispose managed state (managed objects)." and
              ' put this line under it:
              \_dialogForm.Dispose
              

              End Class

              To use the new dialog/wrapper, the consumer only has to create an instance of the wrapper class:

              Dim d As New MyDialog
              Dim result As DialogResult
              
              result = d.ShowDialog()
              

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak

              1 Reply Last reply
              0
              • L Lost User

                +5, that's the cleaner and recommended solution :)

                Bastard Programmer from Hell :suss:

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #9

                I've caught myself overriding all the crap in a subclass before, then it dawned on me that this would be quicker to code and easier to debug when you don't have to wade through all the overrides.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak

                T 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  I've caught myself overriding all the crap in a subclass before, then it dawned on me that this would be quicker to code and easier to debug when you don't have to wade through all the overrides.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak

                  T Offline
                  T Offline
                  The Mighty Atom
                  wrote on last edited by
                  #10

                  Awesome, but i think im doing it wrong. I still have access to all the form properties, but my custom property named 'CustomProperty' isn't available. Here's how my setup looks like: [BaseDialogForm.vb]:

                  Friend Class BaseDialogForm

                  Form Designer code here, nothing else

                  End Class

                  [DialogForm.vb]:

                  Public Class DialogForm
                  Implements IDisposable

                  Private \_dialogForm As New BaseDialogForm
                  
                  Public Function ShowDialog() As DialogResult
                      Return \_dialogForm.ShowDialog()
                  End Function
                  
                  Public Property CustomProperty As String
                      Get
                          Return \_dialogForm.Text
                      End Get
                      Set(ByVal value As String)
                          \_dialogForm.Text = value
                      End Set
                  End Property
                  

                  IDisposable Support code here

                  End Class

                  :confused:

                  Virtual Space Shuttle Astronaut

                  D 1 Reply Last reply
                  0
                  • T The Mighty Atom

                    Awesome, but i think im doing it wrong. I still have access to all the form properties, but my custom property named 'CustomProperty' isn't available. Here's how my setup looks like: [BaseDialogForm.vb]:

                    Friend Class BaseDialogForm

                    Form Designer code here, nothing else

                    End Class

                    [DialogForm.vb]:

                    Public Class DialogForm
                    Implements IDisposable

                    Private \_dialogForm As New BaseDialogForm
                    
                    Public Function ShowDialog() As DialogResult
                        Return \_dialogForm.ShowDialog()
                    End Function
                    
                    Public Property CustomProperty As String
                        Get
                            Return \_dialogForm.Text
                        End Get
                        Set(ByVal value As String)
                            \_dialogForm.Text = value
                        End Set
                    End Property
                    

                    IDisposable Support code here

                    End Class

                    :confused:

                    Virtual Space Shuttle Astronaut

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #11

                    Which one of these classes is your code creating an instance of?? From your description of the problem, it's the wrong one.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak

                    T 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      Which one of these classes is your code creating an instance of?? From your description of the problem, it's the wrong one.

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak

                      T Offline
                      T Offline
                      The Mighty Atom
                      wrote on last edited by
                      #12

                      Oh fail... I was still instancing my BaseDialogForm class in my test form code, not the DialogForm class. :doh: Now, to make sure i set this up correctly anyway, could you review my code? Here are my vb files: Friend Class BaseDialogForm[^] Public Class DialogForm[^] Test Form[^]

                      Virtual Space Shuttle Astronaut

                      D 1 Reply Last reply
                      0
                      • T The Mighty Atom

                        Oh fail... I was still instancing my BaseDialogForm class in my test form code, not the DialogForm class. :doh: Now, to make sure i set this up correctly anyway, could you review my code? Here are my vb files: Friend Class BaseDialogForm[^] Public Class DialogForm[^] Test Form[^]

                        Virtual Space Shuttle Astronaut

                        D Offline
                        D Offline
                        Dave Kreskowiak
                        wrote on last edited by
                        #13

                        It's OK. it's not really YOUR code in those considering it's nothing but a copy'n'paste job, but it's OK.

                        A guide to posting questions on CodeProject[^]
                        Dave Kreskowiak

                        T 1 Reply Last reply
                        0
                        • D Dave Kreskowiak

                          It's OK. it's not really YOUR code in those considering it's nothing but a copy'n'paste job, but it's OK.

                          A guide to posting questions on CodeProject[^]
                          Dave Kreskowiak

                          T Offline
                          T Offline
                          The Mighty Atom
                          wrote on last edited by
                          #14

                          Well of course it's copy'n'paste. :) Anyway, looks like it's working the way i wanted. Thanks, Dave. Have some extra free Internets. :thumbsup:

                          Virtual Space Shuttle Astronaut

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups