none
Why can't the constructor for a Class find something that is Public in another Class? RRS feed

  • Question

  • In an Form class I have this declaraction:

    Public SyncScrollPanels As SortedList(Of Integer, PanelInfo)

    Then in the PanelInfo constructor I need to look at the other instances of PanelInfo in SyncScrollPanels so of course I have this statement:

    For Each pi As PanelInfo In syncscrollpanels

    But is Intellisense is not happy with the reference to SyncScrollPanels, saying "syncscrollpanels is not declared.  It may be inaccessible due to its protection level."  But since SyncScrollPanels is declared PUBLIC I don't understand how it can be inaccessible.

    I see that what I am doing is not a good programming practice and I am thinking about another approach.  But I'd like to make sense of this error message and I cannot. 

    Thanks,  Bob

    Thursday, March 7, 2013 11:47 PM

Answers

  • Thank you Reed.  I've discovered that I can make Intellisense happy if I just make SyncScrollPanels Public Shared.  But that doesn't strike me as the right solution to this problem.  I've decided to pass SyncScrollPanels as an argument to the constructor. 

    Thanks again for your help.

    Bob

    Friday, March 8, 2013 2:48 AM

All replies

  • The problem is that SyncScrollPanels is defined in your Form, but you're trying to use it from PanelInfo.  This isn't going to work since it would need to have a reference to the form instance in order to use it.

    The error message you're getting is used for multiple things - you're focusing on teh wrong portion.  The relevant info is the other half: "syncscrollpanels is not declared.  It may be inaccessible due to its protection level."  In this case, the problem is that syncscrollpanels isn't declared in a place you can see it from PanelInfo - it's part of the form, not PanelInfo, so you don't "see" it.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Blackwood Friday, March 8, 2013 12:14 AM
    Friday, March 8, 2013 12:03 AM
    Moderator
  • I believe Reed's answer is correct.  If you need access to SyncScrollPanels in the constructor for the PanelInfo class, one way to do it would be to pass SyncScrollPanels as an argument to the constructor. 

    Public Class PanelInfo
        Sub New(panels As SortedList(Of Integer, PanelInfo))
            For Each panel As PanelInfo in panels)
                'Do something with panel
            Next
        End Sub
    End Class

    You can create a new instance of PanelInfo from within your form's class like this

    Dim newPanel as New PanelInfo(SyncScrollPanels)
    SyncScrollPanels.Add(newPanel)

    If you need to create a new instance of PanelInfo from some other class, you will need to pass SyncScollPanels to that class as well (in its constructor, or perhaps as a property of the class).


    • Edited by Blackwood Friday, March 8, 2013 12:25 AM
    Friday, March 8, 2013 12:24 AM
  • Thank you Reed.  I've discovered that I can make Intellisense happy if I just make SyncScrollPanels Public Shared.  But that doesn't strike me as the right solution to this problem.  I've decided to pass SyncScrollPanels as an argument to the constructor. 

    Thanks again for your help.

    Bob

    Friday, March 8, 2013 2:48 AM