Results 1 to 2 of 2

Thread: This Q is for VBA: Having a sub in one class read information from another class

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Answers
    1

    This Q is for VBA: Having a sub in one class read information from another class

    hi
    I am having problems accessing the attributes/functions of a class inside other classes.
    I will try to explain my structure as easy as possible and I have simplified it to focus on what is gving me all the problems:

    Class A contains a collection (ACol) which consists of objects of the B class
    Class B has a public attribute of type string called Name
    Class C contains a sub

    I would like the sub inside class c to do something with all the different names in ACol
    I tried:
    Dim c As B
    For each c in A.Acol
    c.name…… something something
    It seems the sub in class C cannot access the information in the other classes. If I put the same code in my “main” file and not another class it works.

    In short my question is how you can access information stored in a one class from another class.

    Hope somebody can help me!


  2. #2
    Moderator
    Join Date
    Dec 2010
    Answers
    40

    Re: This Q is for VBA: Having a sub in one class read information from another class

    hello erikwws, It depends on what access specifiers you have defined on the classes and where your class C resides.

    I created a sample console app with 3 classes - A, B, C and the Main Module
    ---------------------------------------------------------------------
    Friend Class B
    Private _name As String
    Public Property Name() As String
    Get
    Return _name
    End Get
    Set(ByVal value As String)
    _name = value
    End Set
    End Property

    Friend Sub New(ByVal pName As String)
    _name = pName
    End Sub
    End Class
    ---------------------------------------------------------
    Public Class A
    Private _bCol As ArrayList = New ArrayList
    Public ReadOnly Property BCol() As ArrayList
    Get
    Return _bCol
    End Get
    End Property

    Public Sub AddB(ByVal pName As String)
    BCol.Add(New B(pName))
    End Sub
    End Class
    ----------------------------------------------
    Public Class C

    Public Function GetAllNames(ByVal aClass As A) As String
    Dim bNames As String = String.Empty
    Dim bClass As B
    For Each bClass In aClass.BCol
    bNames = bNames & "," & bClass.Name
    Next
    Return bNames
    End Function
    End Class
    --------------------------------------------------
    Module Module1

    Sub Main()
    Dim aClass As A = New A
    aClass.AddB("Erik")
    aClass.AddB("Douglas")
    aClass.AddB("Michael")

    Dim cClass As C = New C
    Console.WriteLine(cClass.GetAllNames(aClass))
    Console.ReadLine()
    End Sub

    End Module
    ------------------------------------------------------
    This works fine without any problems. Please check if this suffices your need. Or else, paste in some snippet and structure of your code and project to be able to help you out. As a workaround, you could modify the access specifier to Public, though I would only suggest it as a workaround without breaking the rules of OOP concepts.

    Cheers.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact