开发者

What is the best practise of declaring the Variables in Asp.Net Programming?

开发者 https://www.devze.com 2023-01-19 04:51 出处:网络
I just want to know the declaration of variables in a separate class file or declaring in the same aspx.cs file.Please any tell 开发者_开发技巧me the best practise of declaring the variables.

I just want to know the declaration of variables in a separate class file or declaring in the same aspx.cs file.Please any tell 开发者_开发技巧me the best practise of declaring the variables.

Thanks in advance


Its a good practice to separate business logic from presentation. If the variables you are talking about are related to Presentation only, then you can:

  • Declare page level variables at the beginning of the class and surround them with a region tag(as rightly mentioned by slugster)
  • Declare local variables as close to the place where they will be first used as possible(this will cover ChaosPandion's point.

All other variables should fall into the category of business logic and should be declared in the class file containing business logic.


Keep the scope of the variables as small as possible. If you follow this practice the actual code file they are in becomes irrelevant.


There is no practical reason for declaring them in a separate file - if you have so many class level variables that this seems to be an option then you have too many class level variables and you need to refactor.

One thing you can do if they are taking a lot of space is to put them at the bottom of the class definition and/or surround them with a collapsible #region tag.

0

精彩评论

暂无评论...
验证码 换一张
取 消