Introduction To Classes in Visual Basic Tutorial

Hello YouTube sam here from youtube.com slash on live gamer for the new Boston and in this tutorial we’re going to be looking at classes now classes are a way to take subs and functions and to encapsulate them inside of what’s called an object so that you can better organize your code and it promotes reuse of code so that it’s easier and it doesn’t take you as long to write the same thing over and over again so let’s go ahead and create a new project we’ll just go ahead and select windows forms application and we will call it person now go ahead and click OK and wait for that to load so before we before we add any controls to our form 1 let’s come over here to our solution.

Explorer will click on person and then we’ll right-click it click Add and then we’re going to add a new item now I’ll shrink this down some now go ahead and select a class right here and we’re going to name our class person okay so now that person has loaded let’s go ahead and add some stuff to this so we will add a couple variables so let’s use the keyword public so that we can see those in our form 1vv class so do public wait as double and we’ll just go ahead and set that equal to let’s say 8 pounds so if the person was just born so let’s do public money as double and we’ll just let them start off with $100 and then we’ll go ahead and let’s leave it at that okay.

So now let’s create some functions and methods that we can use so we’ll type public sub and then we will just type eat and let’s say we will have them pass how many pounds of food they wanted to eat so we’ll type 5 al pounds as integer or let’s just do a double and we will type weight plus equals and then we’ll do pounds and now we will do public sub exercise and we will bivalve the time so by eval time as double and so whatever the exercise will do wait – equals we’ll just do like time so let’s say they workout one hour we’ll just do divided by 10 now I didn’t do much thinking on the calculations for this I’m just doing it for the sake of the tutorial so they will do public sub work and then we will buy Val hours as double and now we’re going to do is let’s come up here and give them an income.

So public income as double let’s just pay the minimum wage to start off with so now we’re going to do is we’re going to type money plus equals hours times income so however many hours they work it’s going to multiply that by their income and add it to money now when we create our person class we have these public variables up here but we don’t really want the other classes to be able to directly access those variables so let’s just go ahead and make those private so we’ll do private money and private wait so now no other class can see these variables and now let’s make some functions that will let other classes get these variables but it won’t let them change it.

So we’ll just we’ll just type public sub get wait and since this is going to return a value we need to make it a function so a public function get weight as double and we’re just going to return the weight so return weight and we will do this for each one of our variables so we’ll do money and income so public function get money as double and we will just return money and we’ll do this one more time for the income so public function get income as double and we will just return the income okay so now let’s make a sub that will allow you to increase their income.

So we’ll go ahead and type public sub increase income and we’ll have them by Val amount as double and so whatever amount they pass this sub we’re going to go ahead and type income plus equals amount okay so now that we’ve created our class if you don’t have this code already typed in go ahead and do that I will scroll through this and if you have any questions leave them in the comments I’ll let me zoom in real quick okay so if you need to look at this pause any time in here so we just made a couple simple subs and functions and we have some private variables that we’re going to be using so now that we’ve created our class we need to be able to use our class so I’m stay tuned for the next tutorial and we’re going to be talking about that during that one so thanks for watching

Leave a Reply

Your email address will not be published. Required fields are marked *