Click here to Skip to main content
15,920,633 members
Home / Discussions / C#
   

C#

 
GeneralRe: Command Promt window Pin
Ark26-Oct-03 4:08
Ark26-Oct-03 4:08 
GeneralDirectX and System.Math Pin
Ark26-Oct-03 2:56
Ark26-Oct-03 2:56 
Generalstatic constructors Pin
R. Thomas26-Oct-03 0:55
R. Thomas26-Oct-03 0:55 
GeneralRe: static constructors Pin
Blake Coverett26-Oct-03 2:10
Blake Coverett26-Oct-03 2:10 
GeneralRe: static constructors Pin
R. Thomas26-Oct-03 2:24
R. Thomas26-Oct-03 2:24 
GeneralRe: static constructors Pin
Blake Coverett26-Oct-03 2:39
Blake Coverett26-Oct-03 2:39 
GeneralRe: static constructors Pin
R. Thomas26-Oct-03 3:46
R. Thomas26-Oct-03 3:46 
GeneralRe: static constructors Pin
Blake Coverett26-Oct-03 4:09
Blake Coverett26-Oct-03 4:09 
You can't do it with one constructor, but beyond that there's no one right answer, it depends how you are calculating the value you want to initialize those fields with. If the values are simple expressions, then don't write explicit constructors at all, just do:
class Foo {
    static int foo = Date.Now.Year;
    int bar = (int)(Random.Sample() * 100);
}
Only write a static constructor if you need to execute logic that doesn't fit in an expression like that, say loading a static lookup table out of your config file or database.

Write instance constructors either, if you need more complicated code like above, or if you want to pass parameters that control the initialization.

On an unrelated note, don't use hungarian notation with C#. Have a look at Design Guidelines for Class Library Developers[^] on MSDN for that and other important style tips for C# and .NET.

-Blake
GeneralRe: static constructors Pin
Heath Stewart26-Oct-03 5:58
protectorHeath Stewart26-Oct-03 5:58 
Generalstack and heap Pin
R. Thomas26-Oct-03 0:49
R. Thomas26-Oct-03 0:49 
GeneralRe: stack and heap Pin
Blake Coverett26-Oct-03 2:27
Blake Coverett26-Oct-03 2:27 
GeneralRe: stack and heap Pin
R. Thomas26-Oct-03 2:38
R. Thomas26-Oct-03 2:38 
GeneralRe: stack and heap Pin
Blake Coverett26-Oct-03 3:31
Blake Coverett26-Oct-03 3:31 
GeneralRe: stack and heap Pin
R. Thomas26-Oct-03 3:44
R. Thomas26-Oct-03 3:44 
GeneralRe: stack and heap Pin
Blake Coverett26-Oct-03 3:55
Blake Coverett26-Oct-03 3:55 
Generalremainder Pin
R. Thomas26-Oct-03 0:40
R. Thomas26-Oct-03 0:40 
GeneralRe: remainder Pin
leppie26-Oct-03 0:51
leppie26-Oct-03 0:51 
GeneralRe: remainder Pin
R. Thomas26-Oct-03 0:53
R. Thomas26-Oct-03 0:53 
GeneralRe: remainder Pin
Rakesh Rajan26-Oct-03 1:08
Rakesh Rajan26-Oct-03 1:08 
GeneralRe: remainder Pin
Daniel M. Edwards26-Oct-03 1:11
Daniel M. Edwards26-Oct-03 1:11 
GeneralRe: remainder Pin
R. Thomas26-Oct-03 1:16
R. Thomas26-Oct-03 1:16 
GeneralRe: remainder Pin
Daniel M. Edwards26-Oct-03 1:17
Daniel M. Edwards26-Oct-03 1:17 
GeneralType.GetMembers() Pin
Arun Bhalla25-Oct-03 15:02
Arun Bhalla25-Oct-03 15:02 
GeneralRe: Type.GetMembers() Pin
Heath Stewart26-Oct-03 6:06
protectorHeath Stewart26-Oct-03 6:06 
GeneralRe: Type.GetMembers() Pin
Arun Bhalla26-Oct-03 8:40
Arun Bhalla26-Oct-03 8:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.