[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

create static method that holds toolstripmenuitems c#

Posted on 2010-03-29
14
Medium Priority
?
339 Views
Last Modified: 2012-05-09
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
0
Comment
Question by:elmbrook
  • 4
  • 4
  • 3
  • +1
14 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 29039828
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
 
LVL 12

Expert Comment

by:lazyberezovsky
ID: 29072664
What is the purpose of FormCollection() method? You can access your collection like this:
YourClass.MyItems.
0
 
LVL 5

Expert Comment

by:mikebirt
ID: 29100980
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 29101280
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
 
LVL 12

Expert Comment

by:lazyberezovsky
ID: 29102679
kaufmed, I wrote that few post above.
0
 
LVL 5

Expert Comment

by:mikebirt
ID: 29102730
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 29102785
?   Not sure I follow you.
0
 
LVL 12

Expert Comment

by:lazyberezovsky
ID: 29103046
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 29103073
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
 
LVL 5

Accepted Solution

by:
mikebirt earned 2000 total points
ID: 29104676
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
 

Author Comment

by:elmbrook
ID: 29128282
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
 

Author Comment

by:elmbrook
ID: 29130022
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
 

Author Comment

by:elmbrook
ID: 29152137
It works fine with the solution from mikebirt
and how i suppose to remove a certain value from the list?

thank you
0
 
LVL 5

Expert Comment

by:mikebirt
ID: 29181154
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…

611 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