By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,338 Members | 1,344 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,338 IT Pros & Developers. It's quick & easy.

Initialising variables inside or outside contructor

P: n/a
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?

--
Daisy The Cow
Now playing: Daniel Bedingfield - Gotta Get Through This
Nov 13 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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 classdefinition, rather than it all be split into the two chunks. Are therethings 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"
Nov 13 '05 #2

P: n/a
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.
"Daisy" <da***@nospam.oops> wrote in message
news:be**********@linux01.dannytuppeny.com...
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?

--
Daisy The Cow
Now playing: Daniel Bedingfield - Gotta Get Through This

Nov 13 '05 #3

P: n/a
"Hector Martinez" <hm********@uci.cu> wrote in message
news:0c****************************@phx.gbl...
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 :)
--
Daisy The Cow
Now playing: Flip & Fill - I Wanna Dance With Somebody [Flip & Fill Mix]
Nov 13 '05 #4

P: n/a
Hector Martinez <hm********@uci.cu> wrote:
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;
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #5

P: n/a
-----Original Message-----
Hector Martinez <hm********@uci.cu> wrote:
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;
}
}

--
Jon Skeet - <sk***@pobox.com>
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
Nov 13 '05 #6

P: n/a
"Jeff Louie" <an*******@devdex.com> wrote in message
news:O$**************@TK2MSFTNGP11.phx.gbl...
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.
--
Daisy The Cow
Now playing: R Kelly - Ignition [Remix]
Nov 13 '05 #7

P: n/a
Hector Martinez <hm********@uci.cu> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #8

P: n/a

"...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 - itworks.


"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.
Nov 13 '05 #9

P: n/a


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.
Nov 13 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.