Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi Everyone,

When the following code is compiled i ended up with a compile time error :a is used like a type.
class Program
    {
 
        int a;
        a=10
      static void Main(string[] args)
        {
 
            Program p = new Program();
            Console.WriteLine(p.a);
            Console.ReadKey();
 

        }
}
But,when change the code to :
 
class Program
    {
 
        int a=10;
              static void Main(string[] args)
        {
 
            Program p = new Program();
                        Console.WriteLine(p.a);
            Console.ReadKey();
 

        }
}
 
Code got successfully complied.What makes the differnce ?what is happening here?
 
Regards
Chaithanya M
Posted 14-Apr-11 19:33pm
Edited 14-Apr-11 19:37pm
Prerak Patel140.5K
v2
Comments
Pong D. Panda at 15-Apr-11 1:44am
   
With this kind of questions, I'm really worried what will happen in the near future.
SAKryukov at 15-Apr-11 2:42am
   
:-) LOL
--SA
MarkRiffey at 18-Oct-12 14:42pm
   
I guess you were never new at something. How fortunate.
Toli Cuturicu at 15-Apr-11 15:21pm
   
Get a book or attend your classes.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You are missing a ; there
 
a=10
 
Moreover, you cannot use statements in class.
  Permalink  
v3
Comments
M.CHAITHANYA at 15-Apr-11 1:44am
   
Hi,
 
where i am missing a ,in the first case i declared an integer a assigned a value to it in the next line,But in the 2nd case ,a value is intialized to a value while declaring itself..where am i missig a?
Prerak Patel at 15-Apr-11 1:47am
   
In first case you are missing semicolon, not a.
M.CHAITHANYA at 15-Apr-11 2:14am
   
Hi ,
 
I am sorry ,i missed ,But even though if i add ";",Complier is raising error:"a is used like a type".
Ankit Rajput at 15-Apr-11 2:23am
   
In this case, your error should be like "Invalid token '=' in class, struct, or interface member declaration".
Reason of this I have already mentioned in the next solution.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

In the first case, you have not terminated the line that's why compiler is treating it like

a=10 static void Main(string[] args)
 
That's why you are getting this error
  Permalink  
v2
Comments
Ankit Rajput at 15-Apr-11 2:21am
   
Hi,
 
You are facing this problem because you are using a=10; and it is not the Initialization of variable. It's a statement and you can not use a statement in class. you have to put it into a function.
 
RegardsAnkit
M.CHAITHANYA at 15-Apr-11 2:36am
   
Hi Ankit,
 
If it is not the intialization,with the second case block of code i am able to prit the value of a.How is this possible?
 
Regards
Chaithanya M
Ankit Rajput at 15-Apr-11 2:43am
   
In second, compiler will treat it as initialization. Becuase you are intializing variable at the time of declaration.
But in first one, you are trying to intialize it in another statement. and statement are allowed only in functions.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You are missing semicolon in the first case.....and the second one is you cannot assign values to the variables without using object of the class...If you declare the variable 'a' as static then you will not have any compilation error and you even don't need object inorder to access the variable...
try this...
static int a;
 
        static void Main(string[] args)
        {
            a = 10;
            Console.WriteLine(a);
            Console.ReadKey();
        }
  Permalink  
v2
Comments
Ankit Rajput at 15-Apr-11 2:44am
   
Hi,
 
What is the use of object of class Program in your code?
 
Regards
Ankit
M.CHAITHANYA at 15-Apr-11 2:54am
   
HI,

To use the field a,i created object.
Regards
Chaithu
Ankit Rajput at 15-Apr-11 3:00am
   
Hi Chaithu,
 
I am talking about code provided in this solution.
In your code, It was already cleared.
Regards
Ankit
sravanirn at 15-Apr-11 3:05am
   
There is no need to create an object and I've modified it...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 15 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100