Interface(Showing Error)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manjitsarma
    New Member
    • Jul 2007
    • 34

    Interface(Showing Error)

    In the following code I am getting the error-' the modifier public is not valid for this item'.Error is showing in 'getAvr'-

    Code:
    public interface student
           {
               public double getAvr(params int[] marks);
               void display(double avr);
    
           }
    I have defined 'getAvr' in a class 'results' which inherits 'student' interface.Code is given below.

    Code:
     
    
    public class results:student 
           {
               public double getAvr(params int[] marks)
               {
                   double avr = 0;
                   double tot = 0;
                   int n;
    
                   for (int n = 0; n <= marks.length - 1; n++)
                       tot += marks(n);
                   avr = tot / marks.Length;
                   return avr;
               } 
           
           }

    Please tell me how to debug the error .
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    There is a keyword that you use there, something like "shallow" or something that tells the compiler that this is a function that has to be implemented in an inheriting class.
    Unfortunatly I don't remember the exact word.
    You might be able to find it with the intellisense though.
    Or help on interface in msdn.

    Either that or interfaces just don't get to decide what's public and what isn't?

    Comment

    • nateraaaa
      Recognized Expert Contributor
      • May 2007
      • 664

      #3
      Originally posted by manjitsarma
      In the following code I am getting the error-' the modifier public is not valid for this item'.Error is showing in 'getAvr'-

      Code:
      public interface student
             {
                 public double getAvr(params int[] marks);
                 void display(double avr);
      
             }
      I have defined 'getAvr' in a class 'results' which inherits 'student' interface.Code is given below.

      Code:
       
      
      public class results:student 
             {
                 public double getAvr(params int[] marks)
                 {
                     double avr = 0;
                     double tot = 0;
                     int n;
      
                     for (int n = 0; n <= marks.length - 1; n++)
                         tot += marks(n);
                     avr = tot / marks.Length;
                     return avr;
                 } 
             
             }

      Please tell me how to debug the error .
      I don't believe that an interface can accept accessibility types such as (private, public, static, etc.). Only data types such as (void, double, int, etc.). Remove the public keyword from your interface and your error should go away.

      Nathan

      Comment

      • manjitsarma
        New Member
        • Jul 2007
        • 34

        #4
        Thank you very much.I will check it out.

        Regards...

        Manjit

        Comment

        • manjitsarma
          New Member
          • Jul 2007
          • 34

          #5
          Ya after removing the public keyword,error is not showing.But I am getting another error in 'marks'.The error is-'marks' is a 'variable,but used like a 'method'.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            marks is an array, not a function
            you will want to address is like an array.
            Code:
            tot += marks[n];

            Comment

            • manjitsarma
              New Member
              • Jul 2007
              • 34

              #7
              It is working now.Thank you very much.

              Manjit Sarma

              Comment

              Working...