-1

i've got

namespace Example
{
    class TechCollection : List<Tech>
    {
    }
}

and then i've got this

TechCollection Tee = new TechCollection();

in one form but if I insert data I can't access it in other form Im not sure what's the problem, anyone can help?

otherOne
  • 52
  • 7

4 Answers4

5

By default

class TechCollection : List<Tech>
{
}

is internal and not visible in other assemblies (Has been edited after the comment)

internal class TechCollection : List<Tech>
{
}

so as mentioned before in comments - use public.

Vladimir
  • 1,380
  • 2
  • 19
  • 23
  • Nonsense. Default class visibility is internal. You can easily verify it with the help of https://msdn.microsoft.com/en-gb/library/mt632254.aspx Default method visibility is private https://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx – Alex G. May 28 '16 at 21:23
5

if you are on the same assembly you can use internal:

namespace Example
{
    internal class TechCollection : List<Tech>
    {
    }
}

or if you want to access from anywhere use public:

  namespace Example
    {
        public class TechCollection : List<Tech>
        {
        }
    }
apomene
  • 14,282
  • 9
  • 46
  • 72
1

Not to be that person, but global anything in C# is a bad design idea. What are you trying to achieve? Do you merely want a List for specific objects that can be reached throughout your application at runtime? If so, maybe think about creating a static singleton class that has a list inside to handle it. Makes it easier to manage in my opinion.

https://msdn.microsoft.com/en-us/library/ff650316.aspx

public class Singleton
{
   private static Singleton instance;

   private Singleton() {}

   public IList<object> PseudoGlobalObjects {get;set;}

   public static Singleton Instance
   {
      get 
      {
         if (instance == null)
         {
            instance = new Singleton();
         }
         return instance;
      }
   }
}

Then you can use something like this:

 Singleton.Instance.PseudoGlobalObjects.Add(obj);
 Singleton.Instance.PseudoGlobalObjects.Remove(obj);
Kevin B Burns
  • 1,032
  • 9
  • 24
1

You need to make your class and method public.

Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified.

Access Modifiers (C# Programming Guide)

Accessibility Levels (C# Reference)

Default visibility for C# classes and members (fields, methods, etc)?

Community
  • 1
  • 1
Alex G.
  • 909
  • 1
  • 9
  • 16