Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
(Update : I move those two variables from inside namespace block to class block)

I create a window form project named TheProject, the program structure is as below.

I also define two variables, TheVariable_A and TheVariable_B, however, the ways to
assign value to variable are different, TheVariable_A is assigned value immediately
after it is declared, and TheVariable_B is assigned value in MainForm() block,

Could you help to show what's the difference between these two ways ?
Which one is better or a standard one ?

Thanks of your prompt and detailed response.


namespace TheProject
{

   public partial class MainForm : Form
   { 

      int TheVariable_A = 10;
      int TheVariable_B;


      //----------------------------
      // MainForm initialization
      public MainForm()
      {
         InitializeComponent();

         TheVariable_B = 1;

      }

      //-----------------------------
      // TheFunction description
      void TheFunction()
      {
          // function code
      }

   }

}
Posted
Updated 17-Jul-11 19:13pm
v2

How are you, Sports?

As the code will not compile, imagine you fix it and put variables in class. They are becomes instance (non-static) members of the class.

The variant TheVariable_A is much better for class and there is nothing wrong with TheVariable_B. In both cases, assignment can be done to non-constant impression, even the method of the declaring class, but only if this method is static. It will work and, not surprisingly, will happen before class constructor, as for a static function calling constructor is irrelevant.

Now, TheVariable_A won't work for struct, so the variant with TheVariable_B will be needed. It can be a justification of the way of initialization of TheVariable_B, because in this case it's easy to re-think implementation from class to struct and back.

Good luck, don't trust people too much, validate answers.

—SA
 
Share this answer
 
Comments
Sports Kuo 18-Jul-11 1:27am    
Thanks SA with another solution.
I've improved my question, and your comments are surely helpful to me much.
Sergey Alexandrovich Kryukov 18-Jul-11 1:33am    
Thank you, Sports.
Now it's better :-). My answer remains valid after your fix.
Good luck, call again.
--SA
As for me there is almost no difference between them and maybe it is even more about your own (or company) coding standards, however

Variable A will be initialized for every constructor you have, in case of B you will have to assign value in each constructor in some way (actually, it will get default value at the start, which is 0 for int). This doesn't matter in your case since you have just one constructor.

Variable A may be initialized just with some constant or constant expression, and variable B may use all kind of functions, other variables, etc.

When using step-by-step debugging, when entering constructor of such class like your MainForm you will first have to go through all initialization lines, and if you have lots of them it may be annoying. This can be solved by placing breakpoint at some position and using run.
 
Share this answer
 
Comments
Sports Kuo 15-Jul-11 1:10am    
Thanks of your kind answer, it should be helpful for me to understand.
Sergey Alexandrovich Kryukov 15-Jul-11 4:12am    
You failed to see: the program will simply not compile as name space cannot contain variables. All variables must be in classes. All the speculations make no sense. The statement about constant is not true (if the variables are properly declared).
I voted 1. It's not good to tell something which is not true and not checking up the "solution" using a compiler.

Sports, I advice you check up solutions before you accept them. Fortunately, you can undo (reject) it.
--SA
skv_lviv 15-Jul-11 5:24am    
Well, it is your right to put any mark you wish (even if I don't agree with it :) )
However, your assumption that I failed to see compilation problems, isn't correct. I just didn't think that this is important thing to mention here and just a simple misprint while entering simplified code and may be ignored like some irrelevant to the question.

Yes, was wrong regarding static methods.
Philippe Mori 16-Jul-11 16:42pm    
Without checking if it compiles or not, it is bad code anyway... You should not uses global variables (in language that support them) when it is not necessary.
Sergey Alexandrovich Kryukov 17-Jul-11 13:58pm    
True, even though there is not such concept in .NET.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900