Hide a property in class for Form1?

Alexandre Bisewski 41 Reputation points
2021-11-07T19:07:14.8+00:00

Hey there!

I have this situation:
class A
dim test as B = new B()
test.open = true
test.color = color.black
end class

class B
public property open as boolean
public property color as color
end class

Very simple and common situation. But this property "color" is like a intern value, I dont want to show this info. But if I convert to private I cant anymore access this value in class A.
Is important to know that this class A is a custonControl in Form1. So is here where I want to hide. If in Form I create dim teste as B = new B() so I can access teste.color but will be very good to avoid problens hide only the color property. Hide to Form1 because in Class A I keeping need to read or qrite it.

Thank you

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,568 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 26,506 Reputation points Microsoft Vendor
    2021-11-08T08:55:15.453+00:00

    Hi @Alexandre Bisewski ,
    After setting the color as a private property in class B, add two public methods getColor() and setColor().
    In this way, the color property cannot be accessed directly.
    Then you can read and write the color attributes in class A through the getColor() and setColor() methods of class B.

    Class A  
        Dim test As B = New B  
        test.setColor(Color.Black)  
        Dim col = test.getColor  
    End Class  
      
    Class B  
        Public Property open As Boolean  
    Private Property color As Color  
      
        Public Function getColor()  
            Return Me.color  
        End Function  
      
        Public Sub setColor(ByVal color As Color)  
            Me.color = color  
        End SubEnd Class  
    End Class  
    

    Hope the code above could be helpful.
    If I have any misunderstanding, please let me know.

    Best Regards.
    Jiachen Li
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Viorel 112.1K Reputation points
    2021-11-08T09:54:06.303+00:00

    If B is a specific internal helper class that must not be used by other classes, then consider this approach:

    • Add a new project to your solution: “Class Library” (or “Windows Forms Control Library” if A is a User Control).
    • In your main project, add a project reference to new library.
    • Define A and B inside the new library; remove A and B from your main project.
    • Use the following definition of B:   Public Class B
         Public Property open As Boolean
         Friend Property color As Color
        End Class

    You can also use Friend for the whole class B. Check the usage of access modifiers: https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/declared-elements/access-levels.

    0 comments No comments