Initialising variables inside or outside contructor

  • Thread starter Thread starter Daisy
  • Start date Start date
D

Daisy

Another this vs that post.

Any real difference in declaring variables with default values, eg.

public class MyControl : UserControl
{
public int RowHeight = 20;
}

Over just declaring the variable, and setting it to 20 in the Constructor?
Currently, everything in my constructor could be just inside the class
definition, rather than it all be split into the two chunks. Are there
things I can only do in the constructor, any advantages to either?
 
Any real difference in declaring variables with default values, eg.

public class MyControl : UserControl
{
public int RowHeight = 20;
}

Over just declaring the variable, and setting it to 20 in the Constructor?
Currently, everything in my constructor could be just inside the class
definition, rather than it all be split into the two chunks. Are there
things I can only do in the constructor, any advantages
to either?


Well the "ReadOnly variable" must be initialized in
the constructor; out of constuctor they are "constants".


Hector...

"The best way to predict the future, is to invent it"
 
Hello Daisy,

Aside from early vs. late binding, not really. Class constructors are more
or less safeguards against uninitialized variables being used by the
consumer of the class. When you call a parameterized constructor and it
initializes it's variables and properties, then you are ensuring that the
variable is populated before first use by the consumer. It still follows the
normal steps in the class lifetime process.


So,

public class MyClass : UserControl
{
public int iRowHeight = 20;
public MyClass()
{

}
}

could be done the same way:
public class MyClass : UserControl
{
public int iRowHeight;
public MyClass(int RowHeight)
{
iRowHeight = RowHeight;
}
}

HTH,

Bill P.
 
Hector Martinez said:
Well the "ReadOnly variable" must be initialized in
the constructor; out of constuctor they are "constants".

I also realised adding controls, setting properties of objects etc., can
only be done in the constructor :)
 
Hector Martinez said:
Well the "ReadOnly variable" must be initialized in
the constructor; out of constuctor they are "constants".

No, readonly variables can be initialised in variable initializers.
There's no difference between:

public class Test
{
readonly int x = 20;
}

and

public class Test
{
readonly int x;

public Test()
{
x=20;
}
}
 
-----Original Message-----
are "constants".

No, readonly variables can be initialised in variable initializers.
There's no difference between:

public class Test
{
readonly int x = 20;
}

and

public class Test
{
readonly int x;

public Test()
{
x=20;
}
}

--
Jon Skeet - <[email protected]>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
.


I have to try but I think you can't do something like
this...because out of constructor the readonly variable
become a constant

"...Direct assignments to readonly fields can only
occur as part of that declaration or in an instance
constructor or static constructor in the same class..."

C# Language Specification by ECMA
 
Jeff Louie said:
Daisy... If you initialize at the time of declaration you will minimize
init errors, especially with multiple constructors. Use the
constructor for dynamic initialization.

That's what I've been doing. My constructor only has things that I didn't
think would work outside it, like Controls.Add(), setting properties on
objects declared outside the constructor, etc.
 
Hector Martinez said:
I have to try but I think you can't do something like
this...because out of constructor the readonly variable
become a constant

"...Direct assignments to readonly fields can only
occur as part of that declaration or in an instance
constructor or static constructor in the same class..."

C# Language Specification by ECMA

Note the "as part of that declaration" bit. Furthermore, try it - it
works.
 
Note the "as part of that declaration" bit. Furthermore, try it - it
works.

"as part of that declaration" means:
readonly int i = 5;


In the other hand take a look to this...

public class GameGrid
{
public bool[,] Grid;

internal readonly int Rigth;
internal readonly int Down;

public GameGrid(int r, int d)
{
Rigth = r; Down = d;
Init();

for (int i= 0; i< Rigth; i++)
for (int j= 0; j< Down; j++)
Grid[i,j] = false;
}

private void Init()
{
Grid = new bool[Rigth,Down]; //Important Line
}
}


I can put the "Important line" inside the constructor,
because i receive a message which tell me that the
expresion need constant (or somethin like that), but in
the way i show up its work. Why?

"Because out of constructor "Rigth" and "Down" readonly
field become in constants.

Don't take in the bad way, ok.
 
I'm sorry, you are rigth, i just missunderstand what
you post...

is exactly like this :

You can't do this:

public class test
{
public readonly int y = 25;
}

or this:

public class test
{
public readonly int y;

public test
{
y = 25;
}
}


What you post is exactly what I want to post, I only
don't read ok what you post. I hope you understand me...

I'm sorry , again.
 
Back
Top