I remember, when I was still in college, coding is not that hard. That was when I still haven’t met Array. Arrays really cause “aray” for most. And you knew that too. Isn’t it?
Now, I would like to share some tips on how to make array manipulation easy for you. Believe it or not, I just realized it when I started working as a programmer.
What Is An Array?
From www.cplusplus.com
An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.
As I am creating programs, I seldom use arrays. Or let us say, I am not using it most of the times. I’d rather declare more variables instead of memorizing array indexes. And there’s a thing I realized. As long as you move away from using array, the more you will know the importance and ease of using it.
Tip #1: Use arrays often.
Instead of declaring the usual variable, declare them as arrays. You might call me crazy or over acting for it. But I promise you, it will work. The more you became familiar of using it, the more you can understand the use of it. I am not that good in using array, I admit that. But I improved already using it.
Let me cite an example. Have you ever used command buttons in VB? Yeah, I know. That was a fool question because I know you love command buttons in calling an event. Okay, my example is a simple (SIS) Student Information System. That was my last VB project in the Computer Programming 2 course.
In that simple system, you are going to use few buttons – ADD, EDIT, DELETE, SAVE/UPDATE, and EXIT. So, that would be 5 buttons. This means, you have 5 objects. Isn’t it? Yeah! You are good in counting huh!
Now instead of having 5 objects, why don’t just having only one? I mean five objects in one module.
Step 1
Create your modules for each event, one module for “ADD”, another for “EDIT”, and so on. So you will have a total of five modules.
Step 2
On your form, put a command button. Name it as CommandButton. Then, copy it and paste it in the same form. When you do that, VB will ask you if you want to make it a control array or not. Choose “YES” so you have a CommandButton array. You will now notice that the name of the original CommandButton is now CommandButton(0) and the new one is CommandButton(1). The numbers are what we call the index. Now, do this step until you have 5 buttons (up to index 4).
I am a little distracted when it comes to index because it started with zero (0). So, if you feel the same, paste again another button – CommandButton(5) – and then, delete the one with index zero – CommandButton(0).
Step 3
You now have five buttons – from CommandButton(1) up to CommandButton(5). Actually, with that step, you only have one CommandButton with five indexes which is good in saving space in computer’s memory.
Change the caption of the buttons with “ADD”, “EDIT”, “DELETE”, and so on. Then double-click any button. That will prove that the five buttons are actually just one button.
Step 4
Use SELECT Case statement like this.
Select Case Index
Case 1
Call add
Case 2
Call edit
Case 3
Call save
Case 4
Call delete
Case 5
Call exit
End Select
And that’s it! See how it works.
Now, let’s go to next tip.
Tip #2
Practice Tip #1 often. And you will just realize that you’re slowly getting rid of Ouch brought by array.
That’s all. I hope you learned.
Class Dismiss!
Array makes your code more clean rather than using many variables. Also, even array is hard we must think that it also makes programming much easier if we already understand it’s concept. Nice post!
That’s a late realization happened to me. I’m glad that I am getting rid of it now.
I too hate arrays when I was in college.. but now, I love arrays more than anything, not unless I’m being confused of using objects and/or hash… put them all together and you are creating a one of a kind program! 🙂
nice post kris!
Seldom we can see students to love array. And explaining the use of it is not enough to convince them that array is very useful. I want them to experience it in their way. 🙂
I’d love to use ARRAY!!! But I don’t understand them much yet. >_< Haha hopefully I will.
You’ll get rid of them soon. By software, they make your code prettier. By hardware, you save memory. Thanks for dropping by Nadine! 🙂