[VB10] Don't allow access to Form properties from Class
-
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
-
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
-
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:
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
-
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
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 -
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 KreskowiakWhat 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
-
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
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 PropertyNow, 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 likeBrowsableAttribute
andEditorBrowsableAttribute
. 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. TheBrowseAble
attribute hides in the designer, theEditorBrowsable
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:
-
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 -
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
...sigh... This would be your custom dialog form:
Friend Class MyDialogForm
Inherits Form
.
. yada, yada, yada
.
End ClassPublic 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 -
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 -
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 KreskowiakAwesome, 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 IDisposablePrivate \_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
-
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 IDisposablePrivate \_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
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 -
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 KreskowiakOh 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
-
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
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 -
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 KreskowiakWell 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