Results 1 to 13 of 13

Thread: [RESOLVED] Misc Variable Questions

  1. #1

    Thread Starter
    Fanatic Member steve65's Avatar
    Join Date
    Jun 2000
    Posts
    610

    [RESOLVED] Misc Variable Questions

    Hi all,

    A few questions came up while I was dabbling in C#:

    When naming variables (I guess they're called data now) is it still best practice to use: frmMain, txtUserName, strTemp, intMaxUsers, etc?

    I declared my VB6 variables at the beginning of my functions/subs, in the C# code I have seen they are declared where ever needed. What is the best practice for this? I liked having the variables at the beginning of the function, it made it easy to tell what variables I had.

    If variable are embedded within a function do you normal try to dispose of them (if you can) as soon as you don't need them anymore or do you just leave them alone until you get out of the function?

    Thanks Steve
    Last edited by steve65; Sep 11th, 2005 at 07:49 PM.
    This space for rent...

  2. #2
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Misc Variable Questions

    There is no rule saying you should use txt and so on in front of your controlls in C#. But I tend to do it no matter what language I use, because I find it easy to read. But when you are working on big projects it is not always you that desides. So if you work on a big project, and people tell you not to do it, then of course you can't do it. But as long as you are just writing code for your self. I can't see any reason not to do it. Better safe then sorry.

    "For every coder, there is a coder that doesn't like his coding style." - Greg Snook


    When you say disposing variables. I guess you mean objects. Yes it is a good idea to call dispose when you are done with it. An other smart thing to do is to use the Using statement when ever it is possible. Then you are sure that it will be disposed when it should. An other thing to notice is that if you make a class, and you have member objects that is derived from idisposable, then you need to dispose these member variables before your object is disposed. If not, then the GC will never dispose your object at all.

    If you are wondering about a few techniques on how to look for memory leaks in C# and Visual Studio. Then I suggest you take a look at this short movie. It should give you some hints.

    http://go.microsoft.com/?linkid=3325538


    Hope that helps a bit. Have a good evening Mr.
    - ØØ -
    Last edited by NoteMe; Sep 9th, 2005 at 01:52 PM.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Misc Variable Questions

    Microsoft recommends not using Hungarian notation (prefixes that indicate type) on variables and also declaring variables when they're needed rather than all at the start of a method. I've adopted these recommendations and I do prefer them, but it's all a matter of personal preference, as NoteMe says. The thing I really hate is when people prefix a class name with "cls". Hungarian notation is for variables, not types. The other thing I hate is the use of the "obj" prefix. It tells you nothing so what's the point? Steer clear of those two and I'll still be your friend.

    With regards to Disposing, the idea is to release resources so it is a good idea to do it as soon as possible. In a short method it's not really a big deal, but if an object might live for hours unused, then you should probably Dispose it as soon as you know it is no longer needed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Misc Variable Questions

    I have to agree with jmcilhinney on the use of hungarian notation on buildt in datatypes and objects. But I don't fully agree on not using the using statement when you have the possibility. Tom Miller have stated secera, times that it is good practice to use it. So I will continue to use it, and continue to suggest that others uses it.


    - ØØ -

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Misc Variable Questions

    Since no-one else answered this bit

    Quote Originally Posted by steve65
    I declared my VB6 variables at the beginning of my functions/subs, in the C# code I have seen they are declared where ever needed. What is the best practice for this? I liked having the variables at the beginning of the function, it made it easy to tell what variables I had.
    If you declare them all at the beginning of the function, then it is a lot easier to find them and see exactly what they are when you come across them in the code. But then, if you declare them inline then it saves a few lines of code, and it looks cool Up to you.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Misc Variable Questions

    Quote Originally Posted by penagate
    Since no-one else answered this bit
    What do you call this then:
    Quote Originally Posted by jmcilhinney
    Microsoft recommends... declaring variables when they're needed rather than all at the start of a method. I've adopted these recommendations and I do prefer them, but it's all a matter of personal preference...
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Misc Variable Questions

    Quote Originally Posted by jmcilhinney
    What do you call this then:
    An oversight

  8. #8
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Misc Variable Questions

    Quote Originally Posted by penagate
    An oversight


  9. #9

    Thread Starter
    Fanatic Member steve65's Avatar
    Join Date
    Jun 2000
    Posts
    610

    Re: [RESOLVED] Misc Variable Questions

    Thanks for all of the info!
    This space for rent...

  10. #10
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: [RESOLVED] Misc Variable Questions

    I stumbeled over this tutorial on coding standards in C# and .NET. Pretty ok. Woudn't say I agree 100%. But no one that has coded for a while would agree with someone anyway. We all have our small charming syntax styles.


    Well for the tutorial:
    http://www.dotnetspider.com/Technolo...Practices.aspx



    Enjoy
    - ØØ -

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] Misc Variable Questions

    Not bad.

    Three points from that one that I strongly agree with:

    - Never ever assume things. That is, your code should be as abstract as possible.

    - Never catch general exceptions, or in fact any exceptions unless absolutely necessary, otherwise you just end up hiding mistakes.

    - Write comments only when necessary to explain the code, and strive to write code that requires no commenting

    I used to comment my code all the time, now I never do. If it's not self-explanatory I re-write it until it is.

  12. #12
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: [RESOLVED] Misc Variable Questions

    Just one more comment about comments.. It is nearly as obvious that you don't think about it, but it is really easy to mess up like this. The rule is:

    "It is better with no comment then a wrong comment"


    Explenation:
    - Choose your words carefully when commenting
    - If you change a function for some reason after a while, it is easy to forget to change the comment. Leading to a comment that will only confuse.
    - If copy pasting a function because it is very similar. Never copy the comment. It will be to easy to forget to change it.




    Just as a reviel by my self. I often comment my code after I have written it... Like for D# now, it is only the classes that is 90% + done that has comments at all...and classes that I have stoped working on that I won't work on again before in a few weeks. Then it is also easy to forget what actualy happens...


    - ØØ -

  13. #13

    Thread Starter
    Fanatic Member steve65's Avatar
    Join Date
    Jun 2000
    Posts
    610

    Re: [RESOLVED] Misc Variable Questions

    Thank you for the info on the web site it helps a lot. Reading through the site though has me questioning if I am doing something right in my code. If you have the time could you take a look at this thread?

    Thanks Steve
    This space for rent...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width