PC Review


Reply
Thread Tools Rate Thread

Class declarations

 
 
web1110
Guest
Posts: n/a
 
      18th Mar 2005
Is this legit? I thought that class members could not be allocated in the
class declaration. I thought this had to go into the constructor. If this
is OK, what are the associated rules?

class WhatEver
{
private object[] thePair=new object[2];

[STAThread]
static void Main(string[] args)
{



 
Reply With Quote
 
 
 
 
Bruce Wood
Guest
Posts: n/a
 
      18th Mar 2005
Yes, it's legitimate. It's equivalent to putting the initialization in
the constructor. I believe that each initialization expression must be
stand-alone... that is, it can't use "this" to access other,
already-initialized members because the initialization order is not
guaranteed.

That said, I prefer to put everything in the constructor so that it's
all in one place. I find even class declarations like

int sales = 0;

to be annoying, because then I have to look two places (the constructor
and the member declaration) to see what the initial values are.

I know other people who avoid putting initialization code in
constructors and prefer member initializations. It's a style thing.

 
Reply With Quote
 
web1110
Guest
Posts: n/a
 
      18th Mar 2005
I concur. I always use the constructor. I was suprised to see this and it
made me wonder. Thanx

"Bruce Wood" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Yes, it's legitimate. It's equivalent to putting the initialization in
> the constructor. I believe that each initialization expression must be
> stand-alone... that is, it can't use "this" to access other,
> already-initialized members because the initialization order is not
> guaranteed.
>
> That said, I prefer to put everything in the constructor so that it's
> all in one place. I find even class declarations like
>
> int sales = 0;
>
> to be annoying, because then I have to look two places (the constructor
> and the member declaration) to see what the initial values are.
>
> I know other people who avoid putting initialization code in
> constructors and prefer member initializations. It's a style thing.
>



 
Reply With Quote
 
Michael S
Guest
Posts: n/a
 
      21st Mar 2005
"web1110" <(E-Mail Removed)> wrote in message
news:HdidnXxq3vXSrKbfRVn-(E-Mail Removed)...
>I concur. I always use the constructor. I was suprised to see this and it
> made me wonder. Thanx


I don't concur.

I use both direct assignment and constructors.

The great thing with direct assignment is that you tell a future maintainer
that
- No, this member is not null.

Regards
- Michael S


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Web controls declarations in base class Chris Microsoft ASP .NET 0 15th Dec 2004 04:46 PM
Web controls declarations in base class Chris Microsoft ASP .NET 1 14th Dec 2004 02:26 PM
Error Trapping in class declarations J. A. Bailo Microsoft C# .NET 1 20th Jun 2004 05:11 PM
Error Trapping in class declarations J. A. Bailo Microsoft Dot NET 1 20th Jun 2004 05:11 PM
class declarations - basic question Jason Shohet Microsoft C# .NET 4 19th Aug 2003 05:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:16 AM.