Click here to Skip to main content
15,884,743 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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
Updated 14-Apr-11 19:37pm
v2
Comments
Pong D. Panda 15-Apr-11 1:44am    
With this kind of questions, I'm really worried what will happen in the near future.
Sergey Alexandrovich Kryukov 15-Apr-11 2:42am    
:-) LOL
--SA
MarkRiffey 18-Oct-12 14:42pm    
I guess you were never new at something. How fortunate.
Toli Cuturicu 15-Apr-11 15:21pm    
Get a book or attend your classes.

You are missing a <big>;</big> there

a=10


Moreover, you cannot use statements in class.
 
Share this answer
 
v3
Comments
M.CHAITHANYA 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 15-Apr-11 1:47am    
In first case you are missing semicolon, not a.
M.CHAITHANYA 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 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.
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
 
Share this answer
 
v2
Comments
Ankit Rajput 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 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 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.
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...
C#
<code>
static int a;

        static void Main(string[] args)
        {
            a = 10;
            Console.WriteLine(a);
            Console.ReadKey();
        }
</code>
 
Share this answer
 
v2
Comments
Ankit Rajput 15-Apr-11 2:44am    
Hi,

What is the use of object of class Program in your code?

Regards
Ankit
M.CHAITHANYA 15-Apr-11 2:54am    
HI,

To use the field a,i created object.
Regards
Chaithu
Ankit Rajput 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900