views:

100

answers:

6

hi, well... I'm confused about what can I do and what I can't do in CLASS SCOPE.

For example

=========================

class myclass
{


  int myint = 0;

  myint = 5; *// this doesnt work. Intellisense doesn't letme work with myint... why?*

  void method()
  {
    myint = 5; *//this works. but why inside a method?*
  } 
}

==================================

class class1
{ 
 public int myint;
}

class class2
{
  class1 Z = new class1();

  Z.myint = 5; *//this doesnt work. like Z doesnt exists for intellisense*

 void method()
  {
    Z.myint = 5; *//this Works, but why inside a method?*
  }
}

Thats I make so many mistakes, I dont understand what works on class scope and what doesnt work.

I know that there are local varaibles and its life cycle. But I dont understand the so well the idea.

+1  A: 

You can initialize a field, but you can't just have assignment statements.

Steven Sudit
This answer appears to be correct, so why was it downvoted?
Steven Sudit
A: 

Basically, when you create a class, you are not allowed to have code & assignments inside that class directly.

but as c# wants to help you, it allows you to define initial states for fields. and they are basically executed directly bevore your constructor code.

when you now want to define code inside the class. somewhere... the program would not know, when to execute. so they are forbidden.

cRichter
+1  A: 

You can't do the below:

class myclass
{
  int myint = 0;
  myint = 5;

Because there is nothing to do, you can only declare members and possibly set an initial value. It is nothing to do with scope. One of the reeasons you can't do it is that there is no guarantee of order, the compiler just gurantees all values will be initialised by the time the class is instantiated. That is why you also cannot do the following:

class myclass
{
  int myint = 0;
  int MyOtherInt = myint;

If you want to set the value when the class is instantiated, put it in the contructor.

Ben Robinson
But if the class has this order ?class1{ int a; void method(); int b;}Will be that order? a, method, b . ??? "b" can not be initialized before the "method" executes, right?
in a class definition, the methods do not execute you are just declaring them so in your example method will not execute at all.
Ben Robinson
But what do you mean about "there is no guarantee of order" ?
When you instantiate a class, there is no guarantee of the order that members are initialized in. The compiler simply guarantees that they will be initialised by the time the class is instantiate. From your posts and comments I suspect you don't really understand what a class is, I would recommend further reading.
Ben Robinson
I'm reading the most I can. I'm eating it step by step. But sometimes this questions rise up, and I need to ask them.Thank you. :)
A: 

A class definition defines its makeup, that is, the fields, properties, methods, events, etc. that it contains. The "work" is actually done inside methods.

What you seem to be confusing is the initialization of the fields of a class with the usage of those fields. You can initialize a field outside a method definition, but to use it, you need to be inside a method.

Matt Davis
+8  A: 

A class can only contain declarations, initializations, and methods. It cannot contain statements of its own.

class MyClass
{
    int x; // a declaration: okay
    int y = 5; // a declaration with an initialization: okay
    int GetZ() { return x + y; } // a method: okay

    x = y; // a statement: not okay
}
JSBangs
+1: Any "class scoped statements" required need to be placed within a constructor.
Reed Copsey
Binary Worrier
We should probably note that properties count as methods here.
Steven Sudit
A: 

To clearly understand it and get answers on your questions you should read Jeffrey's Richter "CLR via C#, Third Edition" and C# 4.0 Language Spectification

Eugene Cheverda