C# Classes / Class Indexers
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
One of the new features in C# is something called a Class Indexer and I'm going to show you a real simple example of that in this video because it can be kind of funky the first time you see it. Now a Class Indexer is kind of similar to a constructor in that we can access a value or set a value pretty quickly and easily except this is kind of like a property on steroids. It's basically in a property that's acting like an Array and you can have one of these in your class. Now the only way to really explain this is to show it to you. So without confusing any further let's take a look at it. What I've done here is I've created a Class called Mark and first thing I've done in this class is create a private Array called X and this is going to hold Strings and then of course I bring it into existence and it's a String Array with 5 places, 0 through 4. Then notice I treat this, let me on here so it's a little easier to read. I'm going to treat this like a Array now as a property and notice I'm going to say Public String and I'll use the This keyword because it, the property basically is going to point to this class and then I'm going to create a variable called Index that this property is going to use. Now, when someone tries to read this property they are going to just basically just be returned whatever the value of X is at whatever position they ask for in the index on X and when they try to set it the same thing is going to happen. This part is just like a property. Then also in this class I have put just a simple method that we can test. Now here's where the magic of this happens. Notice the code that I have put in OK? Notice I'm saying Mark A, I'm creating a variable A as a type of my class Mark and then I'm just instantiating a new instance making it alive in memory. Then notice I can set A, 0 and notice I'm not calling a variable, I'm not pointing to a method, I'm just simply saying A, 0 and I'm setting Jennifer in that, Bob in one, James in Two, Jack in Three and Angels in Four. Then I'm just going to do a message box and show you what's in the fourth index. Then I'll also show you, actually I can just show you right here, that if I hit A, I don't see any reference to this indexed value or this indexer but I do see my method here which is Test this. So this Indexer just kinds of sits in the background and I can load up it up with whatever I want and then access it. So let's run this and when I click on the Button 1 on my Form it's going to of course fire off the Button Click Event. I will create my class and then I'll be able to access the Indexer on my class and so I click this and when I hit OK there's Angela so it has read Angela as the fourth member of that Indexer and then it called the Method, Test this. Now what you need to do is go back through this, build your own and watch how you can basically just kind of store data in an Array Status inside a class and then access it as you need to from that class. It's a really cool functionality, it's new in C#, play with this and it should make some light start to flash and some bells and whistles go off about some cool things you can do with these with your classes in C Sharp, OK.
Tutorial Information
| Course: | Introduction to Microsoft C# 2008 |
| Author: | Mark Long |
| SKU: | 34046 |
| ISBN: | 1-935320-78-5 |
| Release Date: | 2009-10-09 |
| Duration: | 7 hrs / 76 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 