create static method that holds toolstripmenuitems c#

Hi,

I have a question about how do you create a constructor method
a static one, that holds the ToolStripMenuItems as the value

public static ToolStripMenuItem MyItems = new ToolStripMenuItem ();

        public ToolStripMenuItem ListItems
        {
            get { return MyItems; }
            set { MyItems = value; }
        }

        public static List FormCollection()
        {
            ArrayList mylist = new ArrayList();
            mylist.Add(MyItems );

            return mylist;
        }

i do it like this but it does not work

and i dont know how to call it from forms

this is a windows app

thank you
elmbrookAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
What do you mean by "i do it like this but it does not work?" Do you receive an error message? Or are you getting a null returned to you from the property's "getter?" Are the properties above coded within the form's code, or are they in an external class? If they are external, then because they are marked "static", you have to access them like

    ClassName.ListITems
--and--
    ClassName.FormCollection

where "ClassName" is the name of the class where the properties' code resides. If they are in the same file as the form's code, then you should just be able to call them using the property name, but it doesn't hurt anything to use the method mentioned above.
0
lazyberezovskyCommented:
What is the purpose of FormCollection() method? You can access your collection like this:
YourClass.MyItems.
0
mikebirtCommented:
Hi,

You can define a constructor in C# as static, there you can setup any static fields/members.
In the code example you can see i have two constructors, a public and a static. The static constructor will always fire first, intime for the static collection to be created and used by the public constructor.

HTH

Mike
    public class MyClass
    {
        private static List<Guid> instanceList;
        private Guid instanceId;

        public MyClass()
        {
            instanceId = Guid.NewGuid();
            instanceList.Add(instanceId);
        }

        static MyClass()
        {
            myStaticIntList = new List<int>();
        }
    }

Open in new window

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

käµfm³d 👽Commented:
A line such as:

    public static ToolStripMenuItem MyItems = new ToolStripMenuItem ();

has the same effect as

    public static ToolStripMenuItem MyItems;

    public static MyClass()
    {
        MyItems = new ToolStripMenuItem();
    }
0
lazyberezovskyCommented:
kaufmed, I wrote that few post above.
0
mikebirtCommented:
actually, if we're being picky, neither of our code samples would build. in my example i provided the wrong construction code in my static constructor (should have been instanceList = new List<Guid>();). in your case, there's no such thing as a public static constructor. one or the other, public or static.

other than that, yes i would agree that the code amounts to the same thing. however, i was attempting to help elmbrook by showing him static constructors. That is what his question starts off asking.
0
käµfm³d 👽Commented:
?   Not sure I follow you.
0
lazyberezovskyCommented:
Sorry, thought that your are telling about accessing go MyItems
  public static ToolStripMenuItem MyItems = new ToolStripMenuItem ();

is equal calling method
  public static List FormCollection()
        {
            ArrayList mylist = new ArrayList();
            mylist.Add(MyItems );
            return mylist;
        }
0
käµfm³d 👽Commented:
There's not really a need for a static constructor if you initialize the variable on the same line  :)

Since OP already had the line

    public static ToolStripMenuItem MyItems = new ToolStripMenuItem ();

offering a static constructor is calling "6 of one": "half a dozen of the other". I agree with the public modifier on a static constructor, though. I should really slow down when I type ;)
0
mikebirtCommented:
Hi,

The other contribution i would offer as a way to help elmbrook would be the implementation of FormCollection. It attempts to return an ArrayList but the method declaration specifies a List, which causes a compilation error.

I've suggested an alternative implementation to that.

HTH

Mike
        public static List<ToolStripMenuItem> FormCollection()
        {
            List<ToolStripMenuItem> mylist = new List<ToolStripMenuItem>();
            mylist.Add(MyItems);

            return mylist;
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
elmbrookAuthor Commented:
Hi,

I tried mikebirt last post and there is an error in the return value.
It seems that the it cannot returned arraylist.

which is what i want is to returned arraylist collection every times i add one to the constructor.

thank you
0
elmbrookAuthor Commented:
Oh i just tried again and it worked,
the code that mikebirt gave me

again i havent fully tested the code,
i'll let you guys know the results soon

thank you
0
elmbrookAuthor Commented:
It works fine with the solution from mikebirt
and how i suppose to remove a certain value from the list?

thank you
0
mikebirtCommented:
Hi,

MSDN documentation is pretty good for this area. List<T> provides some very useful methods. Here's a link to the MSDN page giving you all the members available:

http://msdn.microsoft.com/en-us/library/d9hw1as6.aspx

HTH

Mike
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.