Problem with static methods

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Clement

    #1

    Problem with static methods

    I didn't write this code so I'm not sure what is going on with this error.
    checkedListBox1 is a public object. Why does the error say that it is
    expecting a 'class' in a for loop? Is it because the method is static?
    Here is the error and the code:

    C:\...\Form1.cs (1130): 'APP.mainForm.c heckedListBox1' denotes a 'field'
    where a 'class' was expected


    public static void MenuItem_Click( object sender, System.EventArg s e)
    {
    for(int i = 0; i<checkedListBo x1.Items.Count; i++)//<<<<line 1130
    {
    checkedListBox1 .SetItemChecked (i,false);
    }
    }

    Also getting an error saying: C:\...\newPromp t.cs(172): An object reference
    is required for the nonstatic field, method, or property
    'APP.mainForm.M enuItem'


    private void button1_Click(o bject sender, System.EventArg s e)
    {
    for(int o = mainForm.histor yMenuItem.MenuI tems.Count-1; o>=0;o--)
    mainForm.histor yMenuItem.MenuI tems.RemoveAt(o );

    }

    Help is appreciated.



  • Chris Clement

    #2
    Re: Problem with static methods

    Ok. I made the checkListBox1 static and the error went away, but now it
    disappears from the form in design mode. Any ideas?


    Comment

    • Gary van der Merwe

      #3
      Re: Problem with static methods

      Hi

      Make both the method and the field non-static?

      Gary


      "Chris Clement" <chrisclement@o nlyonnewsgroup. com> wrote in message
      news:#N#XgHHqDH A.2536@tk2msftn gp13.phx.gbl...[color=blue]
      > Ok. I made the checkListBox1 static and the error went away, but now it
      > disappears from the form in design mode. Any ideas?
      >
      >[/color]


      Comment

      • 100

        #4
        Re: Problem with static methods

        Hi Chris,
        See comments inline.
        [color=blue]
        > C:\...\Form1.cs (1130): 'APP.mainForm.c heckedListBox1' denotes a 'field'
        > where a 'class' was expected
        >
        >
        > public static void MenuItem_Click( object sender, System.EventArg s e)
        > {
        > for(int i = 0; i<checkedListBo x1.Items.Count; i++)//<<<<line 1130
        > {
        > checkedListBox1 .SetItemChecked (i,false);
        > }
        > }[/color]

        this method is static so you have to have reference to the form which has
        this public field *checkedListBox 1*.
        The error message is somehow confusing, but since it is static method you
        can use (beside method arguments and local variables) only static members.
        Since *checkedListBox 1* is not method argument nor local variable it has to
        be a static member of some type ant it has to be preceded by a type name.
        [color=blue]
        >
        > Also getting an error saying: C:\...\newPromp t.cs(172): An object[/color]
        reference[color=blue]
        > is required for the nonstatic field, method, or property
        > 'APP.mainForm.M enuItem'
        >
        >
        > private void button1_Click(o bject sender, System.EventArg s e)
        > {
        > for(int o = mainForm.histor yMenuItem.MenuI tems.Count-1; o>=0;o--)
        > mainForm.histor yMenuItem.MenuI tems.RemoveAt(o );
        >
        > }
        >[/color]

        For this error I can only guess. Is it mainForm the name of the class?

        HTH
        B\rgds
        100


        Comment

        • Chris Clement

          #5
          Re: Problem with static methods

          The method is being called by another class so it needs to be static for
          that class to access it with instantiation. Yes, what you are saying makes
          sense and I actually fixed the error by making the checkListBox1 static, but
          now the checkListBox no longer appears in design mode.


          "Nelson" <nelson470@cox. net> wrote in message
          news:00c501c3a8 72$2605ac70$a30 1280a@phx.gbl.. .[color=blue]
          >
          > Since your function is declared static, it can be called
          > without instantiation of your form class wich contains
          > the checkbox. Therefore your static function has know
          > way of knowing a checkbox exists. Why are you using a
          > static function here anyway?
          >
          > I hope that makes sense.
          >[color=green]
          > >-----Original Message-----
          > >I didn't write this code so I'm not sure what is going[/color]
          > on with this error.[color=green]
          > >checkedListBox 1 is a public object. Why does the error[/color]
          > say that it is[color=green]
          > >expecting a 'class' in a for loop? Is it because the[/color]
          > method is static?[color=green]
          > >Here is the error and the code:
          > >
          > >C:\...\Form1.c s(1130): 'APP.mainForm.c heckedListBox1'[/color]
          > denotes a 'field'[color=green]
          > >where a 'class' was expected
          > >
          > >
          > >public static void MenuItem_Click( object sender,[/color]
          > System.EventArg s e)[color=green]
          > >{
          > >for(int i = 0;[/color]
          > i<checkedListBo x1.Items.Count; i++)//<<<<line 1130[color=green]
          > >{
          > >checkedListBox 1.SetItemChecke d(i,false);
          > >}
          > >}
          > >
          > >Also getting an error saying: C:\...\newPromp t.cs(172):[/color]
          > An object reference[color=green]
          > >is required for the nonstatic field, method, or property
          > >'APP.mainForm. MenuItem'
          > >
          > >
          > >private void button1_Click(o bject sender,[/color]
          > System.EventArg s e)[color=green]
          > >{
          > >for(int o = mainForm.histor yMenuItem.MenuI tems.Count-1;[/color]
          > o>=0;o--)[color=green]
          > > mainForm.histor yMenuItem.MenuI tems.RemoveAt(o );
          > >
          > >}
          > >
          > >Help is appreciated.
          > >
          > >
          > >
          > >.
          > >[/color][/color]


          Comment

          • Chris Clement

            #6
            Re: Problem with static methods


            "100" <100@100.com> wrote in message
            news:%23WXX$KHq DHA.3612@TK2MSF TNGP11.phx.gbl. ..[color=blue]
            > Hi Chris,
            > See comments inline.
            >[color=green]
            > > C:\...\Form1.cs (1130): 'APP.mainForm.c heckedListBox1' denotes a 'field'
            > > where a 'class' was expected
            > >
            > >
            > > public static void MenuItem_Click( object sender, System.EventArg s e)
            > > {
            > > for(int i = 0; i<checkedListBo x1.Items.Count; i++)//<<<<line 1130
            > > {
            > > checkedListBox1 .SetItemChecked (i,false);
            > > }
            > > }[/color]
            >
            > this method is static so you have to have reference to the form which has
            > this public field *checkedListBox 1*.
            > The error message is somehow confusing, but since it is static method you
            > can use (beside method arguments and local variables) only static members.
            > Since *checkedListBox 1* is not method argument nor local variable it has[/color]
            to[color=blue]
            > be a static member of some type ant it has to be preceded by a type name.
            >[color=green]
            > >
            > > Also getting an error saying: C:\...\newPromp t.cs(172): An object[/color]
            > reference[color=green]
            > > is required for the nonstatic field, method, or property
            > > 'APP.mainForm.M enuItem'
            > >
            > >
            > > private void button1_Click(o bject sender, System.EventArg s e)
            > > {
            > > for(int o = mainForm.histor yMenuItem.MenuI tems.Count-1; o>=0;o--)
            > > mainForm.histor yMenuItem.MenuI tems.RemoveAt(o );
            > >
            > > }
            > >[/color]
            >
            > For this error I can only guess. Is it mainForm the name of the class?
            >[/color]
            Thanks for the help, 100. Yes, mainForm is the name of the class where the
            static method is. Fixed the problem by making checkListBox1 static but now
            it does not appear in the design mode.


            Comment

            • Chris Clement

              #7
              Re: Problem with static methods

              Gary,

              Thanks for the response. The newPrompt class needs to be able to access the
              method so it must be static.

              Chris


              "Gary van der Merwe" <garyvdm@hotmai l.com> wrote in message
              news:%239NeSJHq DHA.648@TK2MSFT NGP11.phx.gbl.. .[color=blue]
              > Hi
              >
              > Make both the method and the field non-static?
              >
              > Gary
              >
              >
              > "Chris Clement" <chrisclement@o nlyonnewsgroup. com> wrote in message
              > news:#N#XgHHqDH A.2536@tk2msftn gp13.phx.gbl...[color=green]
              > > Ok. I made the checkListBox1 static and the error went away, but now it
              > > disappears from the form in design mode. Any ideas?
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • nelson

                #8
                Re: Problem with static methods

                Chris,

                Why don't you just pass the checklistbox to the function.

                Example:
                public static void mystaticfunctio n(CheckedListBo x clb)
                {
                for(int i = 0; i<clb.Items.Cou nt;i++)
                {
                clb.SetItemChec ked(i,false);
                }
                }

                This should solve your problem.
                [color=blue]
                >-----Original Message-----
                >The method is being called by another class so it needs[/color]
                to be static for[color=blue]
                >that class to access it with instantiation. Yes, what[/color]
                you are saying makes[color=blue]
                >sense and I actually fixed the error by making the[/color]
                checkListBox1 static, but[color=blue]
                >now the checkListBox no longer appears in design mode.
                >
                >
                >"Nelson" <nelson470@cox. net> wrote in message
                >news:00c501c3a 872$2605ac70$a3 01280a@phx.gbl. ..[color=green]
                >>
                >> Since your function is declared static, it can be[/color][/color]
                called[color=blue][color=green]
                >> without instantiation of your form class wich contains
                >> the checkbox. Therefore your static function has know
                >> way of knowing a checkbox exists. Why are you using a
                >> static function here anyway?
                >>
                >> I hope that makes sense.
                >>[color=darkred]
                >> >-----Original Message-----
                >> >I didn't write this code so I'm not sure what is going[/color]
                >> on with this error.[color=darkred]
                >> >checkedListBox 1 is a public object. Why does the[/color][/color][/color]
                error[color=blue][color=green]
                >> say that it is[color=darkred]
                >> >expecting a 'class' in a for loop? Is it because the[/color]
                >> method is static?[color=darkred]
                >> >Here is the error and the code:
                >> >
                >> >C:\...\Form1.c s(1130): 'APP.mainForm.c heckedListBox1'[/color]
                >> denotes a 'field'[color=darkred]
                >> >where a 'class' was expected
                >> >
                >> >
                >> >public static void MenuItem_Click( object sender,[/color]
                >> System.EventArg s e)[color=darkred]
                >> >{
                >> >for(int i = 0;[/color]
                >> i<checkedListBo x1.Items.Count; i++)//<<<<line 1130[color=darkred]
                >> >{
                >> >checkedListBox 1.SetItemChecke d(i,false);
                >> >}
                >> >}
                >> >
                >> >Also getting an error saying: C:\...\newPromp t.cs[/color][/color][/color]
                (172):[color=blue][color=green]
                >> An object reference[color=darkred]
                >> >is required for the nonstatic field, method, or[/color][/color][/color]
                property[color=blue][color=green][color=darkred]
                >> >'APP.mainForm. MenuItem'
                >> >
                >> >
                >> >private void button1_Click(o bject sender,[/color]
                >> System.EventArg s e)[color=darkred]
                >> >{
                >> >for(int o = mainForm.histor yMenuItem.MenuI tems.Count-[/color][/color][/color]
                1;[color=blue][color=green]
                >> o>=0;o--)[color=darkred]
                >> > mainForm.histor yMenuItem.MenuI tems.RemoveAt(o );
                >> >
                >> >}
                >> >
                >> >Help is appreciated.
                >> >
                >> >
                >> >
                >> >.
                >> >[/color][/color]
                >
                >
                >.
                >[/color]

                Comment

                • Chris Clement

                  #9
                  Re: Problem with static methods

                  Thanks, Nelson. I'll give it a try.

                  "nelson" <nelson470@cox. net> wrote in message
                  news:006401c3a8 78$69c6bd40$a40 1280a@phx.gbl.. .[color=blue]
                  > Chris,
                  >
                  > Why don't you just pass the checklistbox to the function.
                  >
                  > Example:
                  > public static void mystaticfunctio n(CheckedListBo x clb)
                  > {
                  > for(int i = 0; i<clb.Items.Cou nt;i++)
                  > {
                  > clb.SetItemChec ked(i,false);
                  > }
                  > }
                  >
                  > This should solve your problem.
                  >[color=green]
                  > >-----Original Message-----
                  > >The method is being called by another class so it needs[/color]
                  > to be static for[color=green]
                  > >that class to access it with instantiation. Yes, what[/color]
                  > you are saying makes[color=green]
                  > >sense and I actually fixed the error by making the[/color]
                  > checkListBox1 static, but[color=green]
                  > >now the checkListBox no longer appears in design mode.
                  > >
                  > >
                  > >"Nelson" <nelson470@cox. net> wrote in message
                  > >news:00c501c3a 872$2605ac70$a3 01280a@phx.gbl. ..[color=darkred]
                  > >>
                  > >> Since your function is declared static, it can be[/color][/color]
                  > called[color=green][color=darkred]
                  > >> without instantiation of your form class wich contains
                  > >> the checkbox. Therefore your static function has know
                  > >> way of knowing a checkbox exists. Why are you using a
                  > >> static function here anyway?
                  > >>
                  > >> I hope that makes sense.
                  > >>
                  > >> >-----Original Message-----
                  > >> >I didn't write this code so I'm not sure what is going
                  > >> on with this error.
                  > >> >checkedListBox 1 is a public object. Why does the[/color][/color]
                  > error[color=green][color=darkred]
                  > >> say that it is
                  > >> >expecting a 'class' in a for loop? Is it because the
                  > >> method is static?
                  > >> >Here is the error and the code:
                  > >> >
                  > >> >C:\...\Form1.c s(1130): 'APP.mainForm.c heckedListBox1'
                  > >> denotes a 'field'
                  > >> >where a 'class' was expected
                  > >> >
                  > >> >
                  > >> >public static void MenuItem_Click( object sender,
                  > >> System.EventArg s e)
                  > >> >{
                  > >> >for(int i = 0;
                  > >> i<checkedListBo x1.Items.Count; i++)//<<<<line 1130
                  > >> >{
                  > >> >checkedListBox 1.SetItemChecke d(i,false);
                  > >> >}
                  > >> >}
                  > >> >
                  > >> >Also getting an error saying: C:\...\newPromp t.cs[/color][/color]
                  > (172):[color=green][color=darkred]
                  > >> An object reference
                  > >> >is required for the nonstatic field, method, or[/color][/color]
                  > property[color=green][color=darkred]
                  > >> >'APP.mainForm. MenuItem'
                  > >> >
                  > >> >
                  > >> >private void button1_Click(o bject sender,
                  > >> System.EventArg s e)
                  > >> >{
                  > >> >for(int o = mainForm.histor yMenuItem.MenuI tems.Count-[/color][/color]
                  > 1;[color=green][color=darkred]
                  > >> o>=0;o--)
                  > >> > mainForm.histor yMenuItem.MenuI tems.RemoveAt(o );
                  > >> >
                  > >> >}
                  > >> >
                  > >> >Help is appreciated.
                  > >> >
                  > >> >
                  > >> >
                  > >> >.
                  > >> >[/color]
                  > >
                  > >
                  > >.
                  > >[/color][/color]


                  Comment

                  • Frank Oquendo

                    #10
                    Re: Problem with static methods

                    Chris,

                    I've read all the responses in this thread and remain unclear on the
                    need for a static method. Yes, you need to access this method from
                    another class but marking it public takes care of that problem.

                    It seems you're misundertanding the purpose of a static function. The
                    compiler expects to find the CheckedListBox within a containing type,
                    i.e. your form. However, since the method is static, there is no
                    instance of the form for it to access so it cannot locate the
                    CheckedListBox.

                    You either have to pass the object into the static method or make the
                    object a static member of your form. The second option makes little
                    sense since the only time you need something like a ListBox is as part
                    of a UI so that leaves you with the first option.

                    Personally, I'd recommend making the method non-static. Perhaps if you
                    gave us some more context, we could provide you with a better answer.

                    --
                    There are 10 kinds of people. Those who understand binary and those who
                    don't.


                    (Pull the pin to reply)


                    Comment

                    • Nelson

                      #11
                      Problem with static methods


                      Since your function is declared static, it can be called
                      without instantiation of your form class wich contains
                      the checkbox. Therefore your static function has know
                      way of knowing a checkbox exists. Why are you using a
                      static function here anyway?

                      I hope that makes sense.
                      [color=blue]
                      >-----Original Message-----
                      >I didn't write this code so I'm not sure what is going[/color]
                      on with this error.[color=blue]
                      >checkedListBox 1 is a public object. Why does the error[/color]
                      say that it is[color=blue]
                      >expecting a 'class' in a for loop? Is it because the[/color]
                      method is static?[color=blue]
                      >Here is the error and the code:
                      >
                      >C:\...\Form1.c s(1130): 'APP.mainForm.c heckedListBox1'[/color]
                      denotes a 'field'[color=blue]
                      >where a 'class' was expected
                      >
                      >
                      >public static void MenuItem_Click( object sender,[/color]
                      System.EventArg s e)[color=blue]
                      >{
                      >for(int i = 0;[/color]
                      i<checkedListBo x1.Items.Count; i++)//<<<<line 1130[color=blue]
                      >{
                      >checkedListBox 1.SetItemChecke d(i,false);
                      >}
                      >}
                      >
                      >Also getting an error saying: C:\...\newPromp t.cs(172):[/color]
                      An object reference[color=blue]
                      >is required for the nonstatic field, method, or property
                      >'APP.mainForm. MenuItem'
                      >
                      >
                      >private void button1_Click(o bject sender,[/color]
                      System.EventArg s e)[color=blue]
                      >{
                      >for(int o = mainForm.histor yMenuItem.MenuI tems.Count-1;[/color]
                      o>=0;o--)[color=blue]
                      > mainForm.histor yMenuItem.MenuI tems.RemoveAt(o );
                      >
                      >}
                      >
                      >Help is appreciated.
                      >
                      >
                      >
                      >.
                      >[/color]

                      Comment

                      Working...