Solved

Define a private Structure within a class

Posted on 2004-08-24
4
288 Views
Last Modified: 2008-03-06
Folks:

My goal is to be able to create a private structure within a class.  What I have done so far is:

    Private Structure SESSION_STATUS
        Private chShortSessionName As Char
        Private strLongSessionName As String
        Private chSessionType As Char
        Private bSessionCharacteristics As Byte
        Private iNumRows As Int16
        Private iNumCols As Int16
        Private iHostCodePageNum As Int16
    End Structure

    Private MySession as SESSION_STATUS

    MySession.chSessionType = "X"
C:\john\VBScreenScrape1\HostSession.vb(396): 'VBScreenScrape1.MySession.SESSION_STATUS.chSessionType' is not accessible in this context because it is 'Private'.


my problem is - when I attempt to access the data items (example MySession.chSessionType) from within the class, I get a compile error telling me the data item is private.  I've attempted to change the protection of the structure members to protected - but that is not allowed.  My bet is if I change to public it will work - but I don't want any of that detail to be visible outside the class.

If my structure is private but data members public - will the data members be visible outside the class?

Is this the best way to achieve the behaviour I am after - ability to manipulate within the class - but invisible outside the class?  If not, what is?

0
Comment
Question by:jflanner
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
I think that to provide the best answer here, I would need to know what you are trying to accomplish by using a structure.  You can have a private structure that stores values for public properties, but you don't need them.  You could just define private fields:

Private _chShortSessionName As Char

Public Property ShortSessionName() As Char
  Get
     Return _chShortSessionName
  End Get
  Set(Value As Char)
     _chShortSessionName = Value
  End Set
End Property

Bob
0
 
LVL 3

Accepted Solution

by:
DJ_Back-Q earned 250 total points
Comment Utility
   Private Structure SESSION_STATUS
        Private chShortSessionName As Char
        Private strLongSessionName As String
        Private chSessionType As Char
        Private bSessionCharacteristics As Byte
        Private iNumRows As Int16
        Private iNumCols As Int16
        Private iHostCodePageNum As Int16
    End Structure

To fix your problem change all the private of your property to public.

        Public chShortSessionName As Char
        Public strLongSessionName As String
        Public chSessionType As Char
        Public bSessionCharacteristics As Byte
        Public iNumRows As Int16
        Public iNumCols As Int16
        Public iHostCodePageNum As Int16

The problem occurs because in the dotnet environment, the structure is almost the same as a class. So by having the properties private, they are only accessible within the structure.

Andre, MCSD
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
>> If my structure is private but data members public - will the data members be visible outside the class?

No they won't.

Idle_Mind
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
If the structure is private, then any variables declared of that type in your class must also be private and therefore not accessible outside the class.

Futhermore, anything outside the class will have no knowledge of the private structure inside the class.

Idle_Mind
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now