Question on private class visibility rules

R

Robert Styma

I have a question about private class visibility in
visual basic .net. I am using Visual Studio 2008.

I have a class supporting a windows form. Inside the
class, I have defined a private class to hold data.
Instances of this class get placed into a collection
which is public.

Normal procedure is to iterate (for each) through the
collection and pass the returned object to a method in
the class supporting the windows form which breaks out
the fields of the private class. This makes the private
interior class opaque to the outside.

While setting up some new code with copy and paste, I
discovered visual basic .net allowed me to access the
member variables of the private class from outside the
containing class.
1. Is this normal behavior?
2. If so, what is the point of having the interior class
be private?

Public Class EventData
Public AllEvents As New Microsoft.VisualBasic.Collection
....

Private Class EventEnt
Public EventCode As String
Public EventTitle As String
...
New() ...
New(with parameters)...
ToString() ...
End Class
End Class

In a different file:

Public Class Form1
' not using default instance
Public GblEventData As New EventData
...
Private Sub LoadGrid()
Dim M As Object
Dim Row As Integer = 0

' EventList is a DataGridView
Me.EventList.RowCount = Me.GblEventData.AllEvents.Count + 1
For Each M In Me.GblEventData.AllEvents
' Access to private class member?
' No error flagged and it appears to work fine!
Me.EventList.Item(0, Row).Value = M.EventCode
Me.EventList.Item(1, Row).Value = M.EventTitle
Row = Row + 1
Next M

Me.EventList.Sort(Me.EventList.Columns(0),_
System.ComponentModel.ListSortDirection.Ascending)

End Sub ' LoadGrid

PS: The more I learn about datagridview, the more I like it.
Thank you for a great control.
 
C

Cor Ligthert[MVP]

Robert,

Simple as an advice, most things in the Microsoft.VisualBasic namespace are
quiet well.
With one exception. The VisualBasic Collection. Try to avoid that, there are
at the moment countless better ones.

Cor
 
C

Colbert Zhou [MSFT]

Hello Robert ,

The private means that the type is not visible in your IDE editor. If we
write the following codes,

Dim M As EventEnt

We are prompted that "'WindowsApplication.EventData.EventEnt' is not
accessible in this context because it is 'Private'."

But actually, we just define the M as Object and we can surely use late
binding to call the object instance's property or method. That is very
normal in VB.NET. And it can runs OK because our EventCode and EventTitle
are defined as public property.

To disable VB.NET's auto late binding, we can set the Option strict to On
in the Application property page's Compile tab. And then the above codes
will fail with "Option Strict On disallows late binding".

Please let us to know if you have any future questions on this. I will be
happy to be of future help.

Have a nice day!

Best regards,
Colbert Zhou (colbertz @online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Robert Styma

--
Robert Styma
Principal Engineer (DMTS)
Alcatel-Lucent


:

Hi Cor,
Can you give me a suggestion of a better choice than VisualBasicCollection?
I need a container to dump an unknown number of objects and include a key.
I need to be able to retrieve by key or iterate through the objects in the
container.
Thank you for the suggestion.

Robert Styma
 
R

Robert Styma

It looks like System.Collections.Hashtable
will do the job. It contains DictionaryEntry
which has a string Key and a value of type object.
That fits the bill.
Too bad key and value are reversed in the "add"
method comared to Microsoft.VisualBasic.Collection
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top