How can I access a list that is in another .cs file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Dransfield
    New Member
    • Jan 2011
    • 46

    How can I access a list that is in another .cs file?

    I have a list, m_deskList, that's created on one form and I need to also use it in another. I've tried using FormName fn = new FormName(0, 0) but this doesn't work either. Is it possible to access the List (Which is declared as public on another form) from within my main form?

    Code:
    m_deskList.Add(new Desk(Convert.ToInt32(xPos), Convert.ToInt32(yPos), Convert.ToInt32(nWidth), Convert.ToInt32(nHeight)));
    My List is declared in the other class, and initialised further down.

    Code:
            private List<Desk> m_deskList = new List<Desk>();
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    You said it was declared as public, but the code you posted showed it as private ;)

    That said, this is a good time for a property.

    Code:
    ...
    private List<Desk> m_deskList = new List<Desk>();
    ...
    public List<Desk> DeskList
    {
      get { return m_deskList; }
      set { m_deskList = value; }
    }
    ...
    You might want to not include a set on the property to make it read only, that way it can't be nulled or changed externally.

    As an alternative, you might want to look at this article that Curtis Rutland wrote. You might find another approach you like there.



    Good luck! :)

    Comment

    • Alex Dransfield
      New Member
      • Jan 2011
      • 46

      #3
      Sorry about it being private, I did play around with it being public and I must have changed it before I posted it here :D

      I've now got

      Code:
      private List<Desk> m_deskList = new List<Desk>();
      
              public List<Desk> DeskList
              {
                  get { return m_deskList; }
                  set { m_deskList = value; }
              }
      and

      Code:
      RoomDesigner rd = new RoomDesigner(0, 0);
      
                          rd.DeskList.Add(new Desk(Convert.ToInt32(xPos), Convert.ToInt32(yPos), Convert.ToInt32(nWidth), Convert.ToInt32(nHeight)));
      but I'm getting the error

      Code:
      Error	1	Inconsistent accessibility: property type 'System.Collections.Generic.List<ExamsHelper.Desk>' is less accessible than property 'ExamsHelper.RoomDesigner.DeskList'	E:\ExamsHelper\ExamsHelper\RoomDesigner.cs	17	27	ExamsHelper
      Any idea what it could be? Thanks for your help by the way!

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Is your desk class declared as public? I notice I get that sometimes when I create a new class and forget to make it public. When you create one through the designer, it doesn't put public or private, so it defaults to private (within the file, I guess... I'm not sure how that works 'cause it's still in the same namespace).

        Try making the Desk class public and see what happens.

        Comment

        • Alex Dransfield
          New Member
          • Jan 2011
          • 46

          #5
          Okay, no more errors but I think that things aren't working properly. When I trace through the program and hit the line

          Code:
          rd.DeskList.Add(new Desk(Convert.ToInt32(xPos), Convert.ToInt32(yPos), Convert.ToInt32(nWidth), Convert.ToInt32(nHeight)));
          my program reaches for the "Get" property of

          Code:
          public List<Desk> DeskList
                  {
                      get { return m_deskList; }
                      set { m_deskList = value; }
                  }
          which I assume it shouldn't be. This means that it's returning the value of the list m_deskList doesn't it? The desks aren't drawing when I read the coords from a text file because there's nothing in the list and I can't figure out why that's the case.

          When I remove the 'get' accessor, I get the error message

          Code:
          Error	1	The property or indexer 'ExamsHelper.RoomDesigner.DeskList' cannot be used in this context because it lacks the get accessor

          Comment

          • GaryTexmo
            Recognized Expert Top Contributor
            • Jul 2009
            • 1501

            #6
            Yes, you are accessing the get for your class...

            Code:
            [B]rd.DeskList[/B].Add(new Desk(Convert.ToInt32(xPos), Convert.ToInt32(yPos), Convert.ToInt32(nWidth), Convert.ToInt32(nHeight)));
            This is returning the reference to the object, m_deskList. This is the list object, which then allows you to add (as you are doing).

            You can verify that something went in after the add by just checking the contents of it. Either use the debugger, check the count, or output the list.

            If it's still not working, you might need to post more code.

            Comment

            • Alex Dransfield
              New Member
              • Jan 2011
              • 46

              #7
              Doh, I was being an idiot and recreating
              Code:
              RoomDesigner rd = new RoomDesigner(0, 0);
              in my while loop. Everything works as intended now :D

              Thanks!

              Comment

              Working...