Results 1 to 8 of 8

Thread: [RESOLVED] [2.0] Error In static Function

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Resolved [RESOLVED] [2.0] Error In static Function

    Hi all
    check out the class I am using static function in it.

    C# Code:
    1. class sample
    2. {
    3.     int i;
    4.     static int count = 0;
    5.     sample()
    6.     {
    7.         i = 0;
    8.         count++;
    9.     }
    10.     static void ShowCount()
    11.     {
    12.         Console.WriteLine(count);
    13.     }
    14. }

    But When I am calling the object of the class I am getting error at the underline part..
    C# Code:
    1. class Program
    2.     {
    3.         static void Main(string[] args)
    4.         {
    5.             sample s1=new sample();//First Error
    6.             sample.ShowCount(); //Second Error
    7.         }
    8.     }

    The error saying that
    1)'sample.sample()' is inaccessible due to its protection level
    2)'sample.ShowCount()' is inaccessible due to its protection level

    So what is the problem , what I am doing wrong here.
    Thanks

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: [2.0] Error In static Function

    In C# the default access modifier is Private. In your case you have not explicitly mentioned the access modifier so it is private by default, which in turn means that the methods cannot be called from outside the class.

    Read about access modifiers here
    http://msdn2.microsoft.com/en-us/library/ms173121.aspx
    Last edited by Shuja Ali; Apr 16th, 2007 at 01:17 AM.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] Error In static Function

    Also, count++ needs to be sample.count++.

    I strongly recommend using uppercase for class names (Sample rather than sample) so that you do not muddle them with variables.
    Last edited by penagate; Apr 16th, 2007 at 01:23 AM.

  4. #4

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2.0] Error In static Function

    Page not opening

    I strongly recommend using uppercase for class names (Sample rather than sample) so that you do not muddle them with variables.
    That I forget
    OK
    Last edited by shakti5385; Apr 16th, 2007 at 01:32 AM. Reason: Reply of the pengate post

  5. #5
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: [2.0] Error In static Function

    There was something wrong with the link. I have edited my post above.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  6. #6

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2.0] Error In static Function

    Not get the solution.

  7. #7
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: [2.0] Error In static Function

    The methods in your class do not have an access modifier and in C# when you do not provide an access modifier, the default modifier (which is private) will be used. So in your case both your constructor and the static method are private and you cannot access anything that is private from outside the class. Here is the modified code
    Code:
          class sample   
          {   
              int i;   
              static int count = 0;   
              public sample()   
              {   
                  i = 0;   
                  count++;   
              }  
              public static void ShowCount()  
              {  
                  Console.WriteLine(count);  
              }  
          }
    Also the link that I posted above will explain you what access modifiers are and how to use them.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  8. #8

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2.0] Error In static Function

    Thanks sir

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width