Counting array probelm

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    Counting array probelm

    I have this code:
    Code:
    int totalNumberOfGames = games.totalGames;
    games is a class and its has this function in it:
    Code:
    public int totalGames()
    { return processNames.Count(); }
    It gives me this error when i try to build it:
    Error 1 Cannot convert method group 'totalGames' to non-delegate type 'int'. Did you intend to invoke the method? C:\Documents and Settings\admin\ Desktop\Project \Test\Test\frmM ain.cs 98 42 Test
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    I assume that this is because totalgames is being returned as a string. Trying converting to an integer.

    Comment

    • mldisibio
      Recognized Expert New Member
      • Sep 2008
      • 191

      #3
      Try:
      Code:
      int totalNumberOfGames = games.totalGames();
      Eliminating the parens confuses the compiler into interpreting the method call as a delegate, which it is not.

      Comment

      • ziycon
        Contributor
        • Sep 2008
        • 384

        #4
        Try:
        Expand|Select|W rap|Line Numbers

        1. int totalNumberOfGa mes = games.totalGame s();


        Eliminating the parens confuses the compiler into interpreting the method call as a delegate, which it is not.
        Tired that but getting this error now:
        Error 1 An object reference is required for the non-static field, method, or property 'Test.games.tot alGames()' C:\Documents and Settings\admin\ Desktop\Project \Test\Test\frmM ain.cs 98 42 Test

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Yup that is correct.
          Either instanciate your class and use the object to make your totalGames() or declare it as a static function (assuming it does not rely on any member varriables)

          While you're at it, I also recomend picking up a tutorial on object oriented programming (OOP), because this is all pretty basic level in there.

          Comment

          • mldisibio
            Recognized Expert New Member
            • Sep 2008
            • 191

            #6
            If games is a class you need either:
            A. to have an instance of it
            Code:
            games MyGames =  new games();
            int totalNumberOfGames = MyGames.totalGames();
            or make totalGames a static method:
            Code:
            class games {
              public static int totalGames()  { return processNames.Count(); }
            }
            
            int totalNumberOfGames = games.totalGames();

            Comment

            • ziycon
              Contributor
              • Sep 2008
              • 384

              #7
              Originally posted by Plater
              Yup that is correct.
              Either instanciate your class and use the object to make your totalGames() or declare it as a static function (assuming it does not rely on any member varriables)

              While you're at it, I also recomend picking up a tutorial on object oriented programming (OOP), because this is all pretty basic level in there.
              Thanks for that, its been a year since i've done proper coding and over looked the creating an instance. It'll come back to me(Hopefully). Thanks again.

              Comment

              Working...