Tips & Tricks #0005
In my consulting practice, I frequently see examples of Visual Basic code that could be improved upon, either from a readability point of view or from an efficiency point of view. Today I'd like to discuss a relatively new Visual Basic construct that can improve both the readability and efficiency of your code---and save you quite a few keystrokes in the process. That construct is the With…End With statement.
The With…End With statement allows you to 'bundle' Property and Method calls to a single object within the With…End With block. For instance, here's some code that sets the properties of a Command Button, and then executes its Move Method.
Private Sub Form_Load()
Command1.Caption = "OK"
Command1.Visible = True
Command1.Top = 200
Command1.Left = 5000
Command1.Enabled = True
Command1.Move 1000,1000
End Sub
By using the With…End With statement, we can eliminate quite a few programmer keystrokes, and make the code a bit more readable in the process---like this…
Private Sub Form_Load()
With Command1
.Caption = "OK"
.Visible = True
.Top = 200
.Left = 5000
.Enabled = True
.Move 1000,1000
End With
End Sub
Not only have we reduced the number of keystrokes in the procedure, but using the With…End With construct produces more efficient code.
Each reference to an object's property or method requires a Registry 'look up' to execute the code statement---by using the With…End With statement, VB is able to 'pool' this lookup into a single Input Output operation---thereby making your program run faster.
The bottom line---whenever you find yourself coding multiple operations against an Object, use the With…End With statement.
Monday, June 26, 2000
Monday, June 19, 2000
Developer Career Tip #0005---Learning Visual Basic---Part 3
Developer Career Tip #0005
Learning Visual Basic---Part 3
In my last two articles, I dealt with ways to learn a new programming language, including taking official Microsoft Curriculum courses at a Microsoft Certified Technical Education Center (CTEC), long term training at a computer programming school or institute, or enrolling in a course at a local colleges or university. All of these options have one thing in common: you need to be present at a predetermined time and place for a specific duration in a classroom led by an instructor. In today's article, I want to discuss an interesting alternative to classroom based training---web based learning.
I've taught programming for 20 years, and up until three years ago, all of it had been via the traditional route of instructor led classroom training. When I was initially approached to teach a web based programming course in Visual Basic (I teach for SmartPlanet and ElementK), I must confess I had my doubts, but within two weeks of beginning the course, I was amazed that not only could web based learning be just as good as traditional classroom based training, but in many ways it was far superior.
From a student's perspective, web based training can solve a myriad of logistical problems. Most web based training is message board based---which means that there's no formal meeting time. Students log in, obtain the week's assignment (usually a reading assignment), and then use the message board to ask questions, and check back later for answers.
For students whose work or personal commitments prevent them from traveling to formal classroom training, web based training permits them to sign into the 'classroom' whenever their schedules permit. Also, many web based programs are self paced, which means you can speed up or slow down the pace of your learning as your schedule demands. And for some students, perhaps the greatest benefit of web based training is that it's typically a fraction of the cost of formal classroom training.
From an instructor's point of view, I was amazed how web based training solved some of the problems I see in my traditional classes. For instance, since there is no formal lecture mode via the Internet, a higher percentage of students actually read the assigned material, since they can't rely on picking up the material in a lecture period. Secondly, since questions and answers are posted publicly on the message board, just about everybody in the class obtains the benefit of the questions that are posed and the answers that I give. Unfortunately, in a traditional classroom setting, students do not always pay attention to the questions others pose, nor listen to the answers.
Learning Visual Basic---Part 3
In my last two articles, I dealt with ways to learn a new programming language, including taking official Microsoft Curriculum courses at a Microsoft Certified Technical Education Center (CTEC), long term training at a computer programming school or institute, or enrolling in a course at a local colleges or university. All of these options have one thing in common: you need to be present at a predetermined time and place for a specific duration in a classroom led by an instructor. In today's article, I want to discuss an interesting alternative to classroom based training---web based learning.
I've taught programming for 20 years, and up until three years ago, all of it had been via the traditional route of instructor led classroom training. When I was initially approached to teach a web based programming course in Visual Basic (I teach for SmartPlanet and ElementK), I must confess I had my doubts, but within two weeks of beginning the course, I was amazed that not only could web based learning be just as good as traditional classroom based training, but in many ways it was far superior.
From a student's perspective, web based training can solve a myriad of logistical problems. Most web based training is message board based---which means that there's no formal meeting time. Students log in, obtain the week's assignment (usually a reading assignment), and then use the message board to ask questions, and check back later for answers.
For students whose work or personal commitments prevent them from traveling to formal classroom training, web based training permits them to sign into the 'classroom' whenever their schedules permit. Also, many web based programs are self paced, which means you can speed up or slow down the pace of your learning as your schedule demands. And for some students, perhaps the greatest benefit of web based training is that it's typically a fraction of the cost of formal classroom training.
From an instructor's point of view, I was amazed how web based training solved some of the problems I see in my traditional classes. For instance, since there is no formal lecture mode via the Internet, a higher percentage of students actually read the assigned material, since they can't rely on picking up the material in a lecture period. Secondly, since questions and answers are posted publicly on the message board, just about everybody in the class obtains the benefit of the questions that are posed and the answers that I give. Unfortunately, in a traditional classroom setting, students do not always pay attention to the questions others pose, nor listen to the answers.
Monday, June 12, 2000
Programming Tips & Tricks #0004---Printing the contents of your VB project
Tips & Tricks #0004
I recently received an email from a reader who wanted to print the contents of his Visual Basic project to a file.
For those of you not familiar with this Print feature of Visual Basic, you can print an image of any or all of the forms in your Visual Basic project, plus the code in those forms, to a printer for documentation purposes.
In addition to printing directly to a printer, you can also route the print to a text file---either for archive purposes or for subsequent printing.
The problem is that when my reader tried to view the output file using his Notepad Editor, he reported seeing strange characters at the top of the document, and when he printed the file from within Notepad, the results were far from satisfactory.
What causes this is that when he directed the print output to a file, his Windows default printer was set to a high tech model (either a laser or an ink jet printer). The strange characters included in the document were printer control characters that Windows inserted into the file---the same characters that Windows would have sent to the default printer to ensure that the document printed properly..
Fortunately, the fix is easy.
To produce a file that can then be opened cleanly in an editor such as Notepad, you must first install a Generic-Text Only Printer driver on your system (You can do this by Adding a Generic-Text Only Printer via the Windows Print Manager), and then selecting this Printer as the target printer in the VB Printer Dialog Box.
The result is that your output file will contain only text---no special Printer Control Characters.
I recently received an email from a reader who wanted to print the contents of his Visual Basic project to a file.
For those of you not familiar with this Print feature of Visual Basic, you can print an image of any or all of the forms in your Visual Basic project, plus the code in those forms, to a printer for documentation purposes.
In addition to printing directly to a printer, you can also route the print to a text file---either for archive purposes or for subsequent printing.
The problem is that when my reader tried to view the output file using his Notepad Editor, he reported seeing strange characters at the top of the document, and when he printed the file from within Notepad, the results were far from satisfactory.
What causes this is that when he directed the print output to a file, his Windows default printer was set to a high tech model (either a laser or an ink jet printer). The strange characters included in the document were printer control characters that Windows inserted into the file---the same characters that Windows would have sent to the default printer to ensure that the document printed properly..
Fortunately, the fix is easy.
To produce a file that can then be opened cleanly in an editor such as Notepad, you must first install a Generic-Text Only Printer driver on your system (You can do this by Adding a Generic-Text Only Printer via the Windows Print Manager), and then selecting this Printer as the target printer in the VB Printer Dialog Box.
The result is that your output file will contain only text---no special Printer Control Characters.
Monday, June 5, 2000
Developer Career Tip #0004---Learning Visual Basic---Part 2
Developer Career Tips #0004
Learning Visual Basic---Part 2
This is a continuation of my previous article, in which I answered the question, how to learn a new programming language. In that article, I stated that the answer to that question depends on many factors: Do you already know a programming language? How much time do you have to learn one? How do you learn best?
Last time, I described CTEC's (Certified Technical Education Centers), and pointed out these may not be everyone--in fact, they may be a good choice if you already know the programming language you are there to learn!
Are there other alternatives?
Yes, there are plenty.
For instance, there are computer schools that, with programs of varying lengths, turn out graduates that are employable to some degree or other. In my area, schools such as the Computer Learning Center and the Chubb Institute have programs that range from 3 months to a year and teach a variety of programming languages and PC skills. The advantage of a program like this is that you are spending a lot of quality time learning the language. When you are finished, you know it well. The disadvantages are time and cost. You might spend 3 full months, 8 hours or so per day, 5 days per week, learning the language. If you are already employed, this is not a viable alternative, although most of these schools do offer evening hours. Another disadvantage is cost---these schools are not cheap.
A quicker, cheaper alternative are the Professional Development or Certificate programs that many College and Universities are now offering. I teach in such a program myself, and I think they're ideal. For instance, my Visual Basic course meets one time per week, 3 hours per session, for 10 weeks. The amount of classroom time is just about the same as a CTEC, but spaced out more. This permits the students to learn more on their own, spending time in between meetings reading the textbook (and it's an excellent one), doing exercises, and working on a class project.
This one time per week session is less encompassing than a Computer School--but it's also cheaper, and is a good choice for those already employed.
Next time---Online learning alternatives
Learning Visual Basic---Part 2
This is a continuation of my previous article, in which I answered the question, how to learn a new programming language. In that article, I stated that the answer to that question depends on many factors: Do you already know a programming language? How much time do you have to learn one? How do you learn best?
Last time, I described CTEC's (Certified Technical Education Centers), and pointed out these may not be everyone--in fact, they may be a good choice if you already know the programming language you are there to learn!
Are there other alternatives?
Yes, there are plenty.
For instance, there are computer schools that, with programs of varying lengths, turn out graduates that are employable to some degree or other. In my area, schools such as the Computer Learning Center and the Chubb Institute have programs that range from 3 months to a year and teach a variety of programming languages and PC skills. The advantage of a program like this is that you are spending a lot of quality time learning the language. When you are finished, you know it well. The disadvantages are time and cost. You might spend 3 full months, 8 hours or so per day, 5 days per week, learning the language. If you are already employed, this is not a viable alternative, although most of these schools do offer evening hours. Another disadvantage is cost---these schools are not cheap.
A quicker, cheaper alternative are the Professional Development or Certificate programs that many College and Universities are now offering. I teach in such a program myself, and I think they're ideal. For instance, my Visual Basic course meets one time per week, 3 hours per session, for 10 weeks. The amount of classroom time is just about the same as a CTEC, but spaced out more. This permits the students to learn more on their own, spending time in between meetings reading the textbook (and it's an excellent one), doing exercises, and working on a class project.
This one time per week session is less encompassing than a Computer School--but it's also cheaper, and is a good choice for those already employed.
Next time---Online learning alternatives
Subscribe to:
Posts (Atom)