?
Solved

Define a private Structure within a class

Posted on 2004-08-24
4
Medium Priority
?
308 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
ID: 11885721
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 750 total points
ID: 11885737
   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 86

Expert Comment

by:Mike Tomlinson
ID: 11885807
>> 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 86

Expert Comment

by:Mike Tomlinson
ID: 11885881
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
Integration Management Part 2
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

839 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