Shared Members / Using Shared Members
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
Using shared members is a pretty straightforward process, and so what I want to do is go out to visual studio .NET and let's take a look at some, at using some of these shared members. I want to use the same little program that we have been working with, this is a console application. And our entry point for this is sub main. And what do I want to point out to you is: if you'll notice, I'll bring this up some. This is our class that was called Mark, if you'll noticed we've got - Public x As Integer - and - Public Sub SaySomething and DoSomething and then we got a Private Sub YouCannotSeeThis. Now these can be accessed except I first create instance of Mark. And so just for, just to remind you if I -Dim XX As New Mark - then when I hit xx notice I can now access these particular methods and properties. Now if I just try to access them just by going Mark. - notice I don't get anything. And if I type - Mark.SaySomething () - it's going to give me a squiggle, it's going to tell me this is a non-shared member, it requires an object reference and so I can't do that. So let's go create a shared member; first thing I want to do is I want to come all the way to the bottom and make it easy to see these things. I'm going to say - Public Shared Sub - called - DoThis - and normally code would go here. Then I'm going to end my sub, my sub is already ended for me. And so now you'll notice I've got a Public shared sub. Now I'm also, while I'm in here, I'm going to do a Public Shared abc as Integer. And I'll stick with Int16. Now to use these, you'll notice this time when I type my class name Mark and press period and tell us it shows me that my property abc is exposed to me because it is shared and DoThis is exposed because it's shared. And so now I can call DoThis and it will work, it will be just fine, also I can call abc. And I could set that equal to say 76 and so I can set the value of my property. And so that's a shared member. And notice I did both my routines, my subroutines and my properties that way. Now this is great for instances where you need this kind of functionality but keep in mind anywhere in this program that I type Mark.abc = 76, I'm changing the same variable. Whereas if I'd created an instance then each individual object would have it's own abc variable that would have been set. So shared functions, assuming of course, abc wasn't shared. So shared members are just that: they are shared members. But they do give you the ability to get some things without first having to go through object instantiation. And that is basically what a shared instance is. Now in the next set of videos, we are going to start to look at properties, we are going to talk about some global properties and then fields; and fields are some very powerful ways to manipulate some of our variables within our classes. And so we'll take a look at that next.
Tutorial Information
| Course: | Microsoft Visual Basic .NET |
| Author: | Mark Long |
| SKU: | 33433 |
| ISBN: | 1932072349 |
| Release Date: | 2003-05-27 |
| Duration: | 6 hrs / 87 lessons |
| 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 