From the course: Excel VBA: Process Modeling
Create an instance of a class - Microsoft Excel Tutorial
From the course: Excel VBA: Process Modeling
Create an instance of a class
- [Instructor] After you define a class and its properties, you can create an object that is a member of that class. In object-oriented programming. An example of a class is called an instance. For example, if you're simulating movement of customers through a sandwich shop, you would create instances of the shop stations as well as instances of each customer. And once you create an instance, you can assign values to its properties. To demonstrate, I'll work in Chapter01_03 and that is a macro-enabled Excel workbook you can find in the chapter one folder of the exercise files collection. And I don't have any data in my worksheet, but I do have some code already written. So, I'll press ALT F11 to move to the Visual Basic editor. And here I have my definition in the CCustomer class module. And if I resize the code window, you can see that I have defined the property is required for each of the variables up top. Now, I can create a regular code module and refer to an instance of the customer class within it. So, go to the Insert menu and click Module. And again, this is just a standard code module, and I will resize it to cover up the class module in the background so it's not distracting (inhales). The subroutine, I'll call SimTest, so, it's just testing the simulation open and close parentheses, (keyboard clicking) 'cause we're not expecting any data from the outside. Now, I need to define a customer object. So, to declare that I'll do Dim for dimension O-B-J-C, and as the O-B-J indicates, this will be an object variable, so, as CCustomer. And note that because I have CCustomer as a defined class, it appears in the auto-complete when I'm entering in this command. So, I have CCustomer highlighted, so, I'll press tab, (keyboard clicking) a couple of enters, and then I need to actually create an instance of that object variable. And for that we use Set O-B-J C equals New CCustomer. Now, I can assign values to a couple of its properties to make sure it works and then use the Debug.Print statement to send the output to the immediate window to make sure the assignment worked. So, I'll do O-B-J-C.Customer ID, so that property appears again in the auto complete list, equals one. This is just a test. Then O-B-J-C.StartTime, I will also set to one and below that I will do Debug.Print statements for both of those properties, so, Debug.Print, O-B-J-C.Customer ID, and then the same Debug.Print O-B-J-C.StartTime. All right, that looks good. If I run this code, then the output will appear in the immediate window. If the immediate window isn't open in your version of the Visual Basic editor, then you can go to the view menu and click Immediate Window, which is about a third of the way down, or you can press Control G to open it. For me, it's already displayed at the bottom. So, I will click inside of the subroutine and press F5 to run it. And at the bottom I have one and one. So, I have the customer ID and start time. And if I change the start time to two and press F5 to run again, then I get the values of one and two. So, it appears that my code is working correctly.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.