newbie

  • Thread starter Thread starter Gigs_
  • Start date Start date
G

Gigs_

why is this working

namespace ConsoleApplication2
{
class Database
{
public Database()
{
CommonField = 42;
}

public int CommonField; // doesn't this have to be declare before
constructor

public static void Main()
{
Database d = new Database();
Console.WriteLine(d.CommonField);
}
}
}

thanks!
 
why is this working

Because the textual order of declaration is almost entirely irrelevant
in C#.

You can access methods "earlier" than they're declared etc. This isn't
C :)

The only impact of the order is when it comes to initialization -
variables are initialized in the order in which they're declared. It's
a bad idea to use that though - it's too easy to reorder variables and
break things.

Jon
 
Jon said:
Because the textual order of declaration is almost entirely irrelevant
in C#.

You can access methods "earlier" than they're declared etc. This isn't
C :)

The only impact of the order is when it comes to initialization -
variables are initialized in the order in which they're declared. It's
a bad idea to use that though - it's too easy to reorder variables and
break things.

Actually, I did notice the following behaviour in the VS2005 editor just
yesterday when trying to show something unrelated to a co-worker.

- Create empty class.
- Add private variables to the class definition, but at the bottom, like:

class Test
{


private int one;
private bool two;
}

- When adding properties *above* the private variables you will be be
exposing, you will not see them in Intellisense when writing get/set. i.e.:

class Test
{
public int One
{
get { return this. // << Right here, it shows me the "One"
// Property, but not the private "one"
}
private int one;
private bool two;
}

It threw me off a bit, because I'm lazy and love intellisense. Anyway, I
generally out of habit write my private members first, then
constructors, then properties and so on -- it just makes sense to my
brain -- but it does appear to a degree that it affects at least the
IDE, if not the compiler.

Also, just as a final note, I only see this behaviour in these specific
circumstances. It does not generate a compile error if you manually
finish the line with the lower case local variable "one".


Chris.
 
- When adding properties *above* the private variables you will be be
exposing, you will not see them in Intellisense when writing get/set. i.e.:

<snip>

I've just tried that, and it showed me both variables, just as I'd
expect...

Jon
 
Jon said:
I've just tried that, and it showed me both variables, just as I'd
expect...

Maybe I'm missing some updates or something then? I'm using VS2K5
V8.0.50727.42.



Chris.
 
Jon said:
I've just tried that, and it showed me both variables, just as I'd
expect...

Wait, the problem is probably just with me. If you end your get block
properly instead of the way I did in my example, it works and detects
the other private members.

i.e.:

class Test
{
public int One
{
get { return this. } // << Works
// get { return this. // << Doesn't
}

private int one;
private bool two;
}



Chris.
 
Wait, the problem is probably just with me. If you end your get block
properly instead of the way I did in my example, it works and detects
the other private members.

Right - that would make sense, because if you've only got the opening
brace it doesn't work out that there *are* any members defined after
the cursor. It's just broken code as far as it's concerned.

Nice to see a solution :)

Jon
 
Jon said:
Because the textual order of declaration is almost entirely irrelevant
in C#.

You can access methods "earlier" than they're declared etc. This isn't
C :)

The only impact of the order is when it comes to initialization -
variables are initialized in the order in which they're declared. It's
a bad idea to use that though - it's too easy to reorder variables and
break things.

Jon
thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top