Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to pass a variable (string array) to nested classes?

Posted on 2013-06-06
9
345 Views
Last Modified: 2013-06-13
I have an inner class that needs a variable from an upper class.  The variable is actually a string array.  I tried using Friend believing that it would make the variable available to the nested classes below the class that I used it in.  This did not work.  So I created a property to pass the variable but I am convinced that there must be a way to do this without writing a property to pass this variable for every nested class its needed.
0
Comment
Question by:NevSoFly
9 Comments
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 39228250
Can you provide the structure of your parent and child classes?
0
 

Author Comment

by:NevSoFly
ID: 39229125
Thanks for your time.  I should have included this in my first post.  

Public Class OneClass
    Private _Equation As String
    Private equ() As String

    Public Function Start(ByVal Equation As String) As ArrayList
        _Equation = Equation
        equ = _Equation.Split("=") 'splits the equation into right and left expressions.
    End Function
End Class

Class AnotherClass
    Friend Property A As Array
        Get
            Return equ
        End Get
        Set(value As Array)
            equ = value
        End Set
    End Property

    Private ReadOnly Property B As String
        Get
            Return equ(0)
        End Get
    End Property

    Private ReadOnly Property C As String
        Get
            Return equ(1)
        End Get
    End Property

    Class Simplify
        Public ReadOnly Property D() As ArrayList
            Get
                Return equ(0)
            End Get
        End Property

        Public ReadOnly Property E() As ArrayList
            Get
                Return equ(1)
            End Get
        End Property
    End Class
End Class

Open in new window

0
 
LVL 10

Expert Comment

by:adriankohws
ID: 39229240
Will arraylist work for you?
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:NevSoFly
ID: 39229273
It would work.  I just thought it was easier to do a simple array as it was all I needed.
0
 
LVL 10

Expert Comment

by:adriankohws
ID: 39229531
Actually, Arraylist are so simple and ease of use.

Dim ar as New ArrayList
ar.Add("1")
ar.Add("2")

Using a loop, you can load a list inside, and you can identify them by index.
0
 

Author Comment

by:NevSoFly
ID: 39229563
Exactly how would this help my original problem?
0
 

Author Comment

by:NevSoFly
ID: 39233432
Maybe it would help if I restated my desire.

I have an outer class with a variable (Private equ() as string) that I want to make available to inner classes (somestring = equ(0)).  I have tried private, public, shared and friend, all give me the error "'equ' is not declared. It may be inaccessible due to its protection level.".  

I have also included the  outer class when calling the variable (Outerclass.equ(0)).  This gives me the error "'equ' is not a member of 'WindowsApplication1.Expression'.".

Would a Structure work better?

I'm obviously missing something very basic.  Non of my searches has given me a glue as to how to fix it.

Please help.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39239010
You can pass it into inner class in the constructor.
0
 

Author Closing Comment

by:NevSoFly
ID: 39245272
I would prefer not to do it that way but it could work for some instances.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 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