11. Object naming

When you place all your objects onto your form, VB will automatically name them. This however gives the IDE far too much control over your program. With this in mind, we need to come up with a naming scheme you can use when renaming objects on your form.

Rather than dictate exactly how you should do this I’ll point out a few options and let you decide which one appeals to you.

Object Reference
The first thing you should do is make sure the name of your object allows you to understand what type of object it is. I would suggest using a three letter prefix that tells you what the object is, i.e:

  • Form = frm
  • Button = btn
  • Label = lbl
  • Textbox = txt

By using three digits as a prefix instead of a suffix, you can have all your items sorted by type when you select the drop-down box from your properties window. Three characters also means the names of your objects won’t become too large.

Once you have a prefix you then need to decide how to name that particular object. Once again I suggest being brief, abbreviating where possible and utilising either camelCase or underscores.

This simply refers to having separate words in your object name differentiated by their first character being capitalised, i.e:

  • frmTicTacToe

You can probably see why it’s referred to as camelCase since the capitals look like humps on a camel.

The other method of object naming is to separate your words with underscores, i.e:

  • frm_tic_tac_toe

This method can often make it clearer that each word is meant to be separate.

Both of these methods are perfectly acceptable and it is up to you which you use. There are, however, a few important guidelines you should adhere to:

  1. Be consistent: Whichever method you choose, stick with it always!
  2. Do not start an object with a capital letter: This is simply a coding convention that will put you in good stead later in your programming journey.
  3. Rename EVERY object: Even if it seems silly, just get into the habit of naming all your objects.
  4. Use the same prefix always: Don’t switch or change the prefix you use for any particular object type.
  5. Don’t use spaces! VB won’t let you anyway but just so you know, it is almost never okay to name something when programming that includes spaces!

Okay so with that in mind, go ahead and rename all the objects on your form. Pretty boring I know but pedantry and meticulousness are key programming skills so it’s worth getting used to them early!

Leave a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>