Visual Basic (VB.NET) – Full Course for Beginners

freeCodeCamp.org · Beginner ·📰 AI News & Updates ·4y ago

Key Takeaways

This video course covers the fundamentals of programming with Visual Basic (VB.NET), including sequence, selection, iteration, and event-driven programming. It provides a comprehensive introduction to the language, including variables, data types, input/output, debugging, and control structures.

Full Transcript

this beginner's course will help you learn the fundamentals of programming with visualbasic.net visual basic is an object-oriented programming language developed by microsoft it's often used to make desktop apps for windows but you can use the programming language for a wide variety of applications and not just on windows kevin drum teaches this course kevin is the head of computer science at a school in the uk kevin will teach you everything from getting visual studio set up for programming to understanding the basic constructs of high-level programming languages this is an excellent course for anyone who wants to learn visual basic leave a comment with the most interesting thing you learned in this course in this series of videos i'm going to introduce you to programming with visual basic.net inside visual studio so let's begin by launching visual studio and taking a look at some of the features exactly what you see here will depend on which version of visual studio you're using you can see that i'm using visual studio 2019 but essentially the features will be the same you can see i have a list of my most recently used projects on the left hand side on the right i'm going to choose the option to create a new project and again what you see next will depend on which version of visual studio you're using it will also depend on which programming languages you installed you can see that i've installed a number of different programming languages which tells you visual studio itself is not a programming language it's an environment in which you can use a programming language i'm going to use visual basic on the right hand side i can see a number of different project templates which i can choose from and i'm going to create a windows forms app so with the correct option chosen click next you're now invited to give the project a name it's offering me the name windows app2 this is because it's the second application which i've created it's not a very meaningful name so i strongly recommend that you change that perhaps perhaps not i think that's been done before that'll do for now you also need to be mindful of where the project will be saved the location of your project you can see the location of mine will be d visual studio 2019 projects but you can change this in fact i'll show you later that once you're in visual studio you can change the default location this is particularly important if you're working in a school or a college or an organization it may be that you only have permission to save visual studio projects in particular locations notice there's a browse button here if you do want to change the location my solution has the same name as the project i'll say a little bit about the difference between a solution and a project in a moment and i'm going to say that i want to place the solution and the project in the same directory by ticking this box since this is a brand new project i'm not going to worry too much about which version of the net framework i'm using more about that later as well so let's hit create and here is visual studio i can see a little message on the bottom right there telling me that there's a new version of visual studio which can be downloaded i'll maybe do that a little bit later i'll just ignore it for now now there are lots of options here which i'll be talking about as we go along but suffice to say for now we have a menu of options across the top and each of these has a number of sub options which in turn might have a number of sub options there's a toolbar i can add extra toolbars which you'll see later but the most useful one is here to start with on the right hand side i can see my solution explorer a solution is a collection of files that make up my application and we'll take a closer look at these files later i have a properties window which will become important when i start building my user interface and on the left hand side there's a toolbox if i just click on this it will move into view if i click away from it it disappears i actually like the toolbox in place all the time so i'm going to click on this little drawing pin to keep it there and i can resize it if i need to in the middle i've got my form this is where i'm going to start building my user interface so let's write some code i'm going to start by dropping a button onto the form so from my toolbox i click on button draw the button onto the form because the button is selected i can see properties of the button in the properties window on the right hand side i can see for example we have the text property of the button which is currently button one i'm going to change that to press here and notice how it's changed the appearance of the button on the form the other thing i want to change is the name of the button every object that you place on a form will have a name property now it's currently button one because it's the first button which i placed on the form and i could leave it as button one but i want to start using a naming convention btn in lowercase because it's a button and then something meaningful which tells me what the button does for example start you could call the button pretty much anything you like as long as the name doesn't start with a number and as long as there are no spaces or special characters like question marks or exclamation marks in there this naming convention makes it clear what the button does and it also makes it clear that it's a button and that will become important later on when i start writing code there are other properties which i can change as well for example the background color and i have a color palette here which i can use we'll stick with this for now but you'll learn about other properties as and when you need them what i want to do now is write some code that will run when the user of my application clicks on the button to write some code for the button i can double click it the way to think of this is that the code is behind the form my form is still there you can see there's a tab here that's the design view of my form and this is the visual basic view of my form notice that the toolbox no longer has anything on it because it's inappropriate to use it while i'm coding i have line numbers down the left hand side to be honest i don't like these and i'll turn them off in a moment and i can see some code already there and i have to be very careful not to break it for example i can see public class form 1 at the top and end class at the bottom i'm not going to change this these need to be in place in order for the form to work later when you find out more about object oriented programming these two lines will make more sense i can also see the stub of a procedure which will run when the button is clicked private sub button start click notice that's the name that i gave to my button and notice that it's going to handle the click of the button to be more precise it will handle the click event of the button object there's also some stuff going on in brackets here these are called parameters and you'll find out more about parameters later suffice to say leave this exactly as it is for now we're going to write code between sub and end sub so i'm just going to press the enter key a few times and give myself some more room i can also give myself some more room above the procedure and below the procedure sub by the way stands for sub procedure so let's write our first program i'm going to use the message box command notice i'm typing in lowercase and as i type a list of options has appeared visual studio is looking at the letters i'm typing and offering me a command to choose from so i can see msg box here i can ignore it and carry on typing or i can actually select it from the list i'll show you how we can do this more quickly in a moment i'll just press the space bar and i'm going to open a bracket and again there's some more information appearing on the screen it's quite daunting the first time you see this but as you get used to it you'll find it's incredibly useful again i'm just going to ignore it notice how visual studios automatically put the closing bracket on there for me i'm going to open a double quote and again visual studio has automatically put the closing double quote on there and finally i'll type the text of my message hello world all kinds of things have happened here for example notice that msg box has automatically recapitalized that tells me that i typed it correctly i'm getting visual feedback notice also the color coding the literal string which i'm using in my text message is colored differently from the command itself again i'm getting visual feedback on how to use this this is why it's called visual studio let's put another message in now i've just typed the first few letters this time and i'm going to press the tab key on my keyboard to select the rest of it i've saved myself some typing when you get used to it you can write code very very quickly hello world how are you and one more spelling mistake there i'm just going to hold down my control key and tap the left arrow key which will jump me one word at a time through that text and then i can correct my spelling to be honest it doesn't matter what i put inside these double quotes it won't be a problem if on the other hand i mistyped the command i see a red wiggly line underneath it telling me that i've made an error this is what we call a syntax error i'm breaking the rules of the programming language if i move my mouse over it i can see some kind of error message there msg boxes not declared visual studio thinks i'm trying to use something called a variable and i haven't announced that i want to use it more about variables later on let's just get rid of this line altogether okay so there's my program i'll just close up a little bit of the white space i don't need it and i'm going to run the code there's a start button at the top and my form is now on the screen and to test it i'll simply click on the button there's my first message there's my second message and there's my third message and the program has stopped running my procedure may have stopped running but the application itself is still running the form is still on the screen and i can stop this running by pressing the red square up here or i can simply close the form with the red cross here and everything is stopped so there we have it our first program which is just a sequence of messages but it's a program there's one more thing i want to do now and that is to save my work remember a visual studio application can consist of several files so rather than just clicking this little button here which will save the form i'm going to click this button which will save all of the files in my application i can now close down visual studio and return to my application tomorrow in this video i just want to show you how we can customize the integrated development environment the ide that is visual studio so let's start it up again and i'm going to open up the project that i was working on last time notice that it's in the recent files list so i could just click on it here alternatively i can click on open a project or solution let's do it this way for now so we can see which file we need to open up here's my d drive this is where i'm storing my projects and here's the one i was working on last time notice it's a folder inside that folder there's a solution file sln that's the file which ties together all of the other files that make up my application that's the file which i need to open and here's the application i was working on last time here's my form and here's the code behind it a little word of warning first of all sometimes you might accidentally double click on the background of the form itself watch what happens when i do this i've got another procedure stub here called form1 load any code which i write in here will automatically run when i run up the application which will cause the form to load but i don't need this so i'm going to get rid of it i'm simply going to delete this procedure stub another word of warning you might accidentally or even deliberately click on this where it says one reference let's take a look i've clicked on that something has appeared and i'm going to click on this as well i have another tab across the top and what i can see here looks very complicated indeed this is code which runs when you run the form but you don't really need to see it if you're a beginner in fact it's quite daunting when you look at it suffice to say for now it's actually initializing the form it's setting up the form and it's placing the button on there and it's changing various properties of the button i strongly recommend you leave this alone until you have a better understanding of what it does i'm just going to close down this tab and i'm back to something more familiar anyway what i want to talk about now is how we can customize this programming environment you were given some options when you ran it up for the first time but you can change those options inside tools options notice i'm using a color theme blue but you can change it to dark if you wish some people much prefer this to be honest i don't i prefer black on white not white on black it really is a matter of preference i'm going to switch it back to the way it was i can also have finer control over the keyword colors and over the text colors and any other colours that you see on here for that matter we can do this by selecting fonts and colours you can see there's a lot of options here keyword the default color is blue and i can switch that to magenta you do need to be a little bit careful though because some colors have special meanings and it can start looking very messy as well if needs be you can change things back notice there's an option here to use defaults some people like to increase the font size as well it's entirely up to you something else i want to change to give myself some more room on the screen is to switch off these line numbers i can do that by going to text editor all languages and then here line numbers notice i can have different configurations depending on the programming language i'm using and there's one more thing i want to change and that's some of the default file locations i can do that underneath projects and solutions locations you can see i've already set my project's location to my d drive needless to say there are lots more options which you can set in here and i'll be honest i don't know what half of them are but when i need them i'll find them click ok for your changes to take effect in this video about visual basic.net i want to say more about output and i'm going to introduce you to variables i'll start by creating a new project as before it'll be a windows forms application in visual basic give the project a meaningful name and think about where it's going to be saved you can change this if necessary let's start with a button on the form and i'm going to rename the button get in the habit of renaming things that you place on the form as you go along btn that's my convention and then go this is called camel notation because of the capital g it looks like a camel with a hump on its back i'm also going to change the text property of the button and let's write some code that will run when the user clicks on the button to get to the code behind the form i double-click the button let me just remind you you mustn't change or delete this if you do you'll start seeing errors on the screen watch straight away i've got a syntax error up here class statement must end with matching end class i'll just use the undo button to put it back again and i'm going to give myself some white space just give myself a bit more room on the screen here we've already seen that we can display a message on the screen using the message box command like this to test it i simply press the start button that will run the form up and then i can run my code by clicking on the button itself now there are three fundamental constructs when it comes to programming that i'd like to mention now the first of these is sequence the second is selection which you'll see in a later video and the third is iteration which you'll also see in a later video for now let's just say a little bit about sequence sequence simply means that each statement in a program or a block of code will run one after another in sequence so for example i could display each of these words separately run the program press the button and one message after another and i'll stop my application with this red square if i want to display these words in reverse order i can simply change the order of the commands i'm just dragging and dropping and tidy up some of the white space you can have as much or as little white space as you like whatever makes it easy for you to read the program let's give this a try before i continue and talk about variables i just want to show you another command i can use to display a message on the screen messagebox dot show let's take a look this is another message it does exactly the same thing to be honest this is a bit of an old style command which has existed from very early versions of visual basic this is a more modern way of doing things this is an object-oriented approach and it'll make more sense why you might do it this way later when you find out more about object oriented programming for now you can either use msgbox or messagebox.show i'm going to continue with msg box so now let's talk about variables i'll start by placing another button on the form because i want to write a separate procedure switch to the form and drop another button on there notice i'm getting some guidelines as i drag the button around and resize it this helps me to create a nice layout for my form okay let's get in the good habits of naming objects as we go along so i'm going to call this btn variables and i'll change the text property as well to write the code as before double click and you can see i now have a new procedure stub i can start writing my code here let's give myself a little bit more white space below and a little bit less at the top here so what is a variable well a variable is actually a location in the computer's memory where a program can temporarily store data while it's running and while that sounds a little bit complicated they're actually very easy to set up and use let's say for example i want to create a variable to store somebody's first name the first thing i need to do is declare it i need to announce that i want to use a new variable and i do this using the dim statement like this now there are a number of things going on in this command first of all the word dim is short for dimension because i'm actually setting aside a certain amount of memory i'm specifying the size or the dimension of the variable the amount of memory being set aside depends on this which is the data type of the variable when it comes to string variables one byte of memory will be needed for each character in the string but to be honest i don't need to worry too much about that visual basic will look after the memory for me or to be more precise the runtime engine that visual basic depends on will take care of the memory for me the other thing i'm doing is i'm giving my variable a name and again notice that i'm using camel notation and i've prefixed it with st because it's a string variable that's just a convention which i'm going to encourage you to use you might want to know what data type a variable is just by glancing at its name so that's my variable declaration i've got a green wavy line underneath it to tell me that i haven't done anything with it yet unused local variable so let's now assign a value to it notice i'm typing in lowercase i'm being offered the name of the variable in a list so i'll just press the tab key to select it and i'll say st first name is equal to my first name notice that the green wavy line has disappeared now because i'm using the variable in earlier versions of visual basic you would have to use the let command in front of this you'd say let st first name equal kevin let it become kevin in this version of visual basic we don't use let at all but what you need to appreciate here is i'm putting the string kevin into that piece of memory which i set aside because the string is one two three four five characters long it's going to take up five bytes of memory as i said before though you don't really need to worry too much about that okay now i'm going to output the contents of that variable and i'll do that using messagebox notice that i haven't put double quotes around the name of the variable because i want to output its contents i don't literally want to output the string st first name let's see what happens i'm outputting the contents of the variable i want to make the message a little bit more friendly so i'm going to join the contents of the variable with some literal text like this you can see that this is a literal string because it's inside double quotes and this is the contents of my variable and i'm using the concatenation operator which is an ampersand to join the two together we call this string concatenation i can concatenate something onto the end of it as well again another concatenation operator let's see the effect hello and welcome kevin i hope you are well i want you to notice that i put spaces inside these text strings notice there's a space after the letter e let's just remove it for a second and see what happens i've also got a space there inside the string immediately before the eye i'll just take that out as well look what happens you can see the message is a little bit messy let's tidy it up okay i'm going to declare another variable to hold my last name so another dim statement and i'll assign a value to this variable as well and i'll include the last name as part of the message a common mistake is to not use enough ampersands watch what happens if i get rid of this one you can see i have a syntax error the message isn't particularly helpful but i know that there's something wrong with the way i've concatenated this string together so let's put the concatenation operator back in place another common mistake is not to use enough double quotes so again i'll take out one of those double quotes and you can see that the whole thing seems to be a problem one little error and i've got red wavy lines all over the place this is not particularly helpful just remember double quotes and brackets come in pairs if you have an opening double quote there has to be a matching closing double quote if you have an opening bracket there has to be a matching closing bracket let's try this now you can see kevin drum has come out as one word there's no space in between the two i can fix this easily i need to concatenate a space in between those two variables a space is just another character let's take a look much better notice that i've put my dim statements my variable declarations at the top of the procedure i didn't have to do this as long as i declare a variable before i try to assign a value to it i can put my dim statement wherever i like so for example i could declare the last name here and then assign a value to it that's absolutely fine what i mustn't do is try to assign a value to it before i declare it you can see i have a syntax error local variable st last name cannot be used before it has been declared the sequence of operations is important although what you can see here is perfectly okay i like to put all of my declarations at the top of the procedure keep them all together it actually makes the code easier to manage particularly when you have a lot of code i told you that a variable was a temporary storage location which your program can use while it's running when this program has done its job and it comes to an end the memory being used by the variables will be released it'll be freed up for something else to use but i can change the contents of a variable while the program is running that's why it's called a variable its contents can vary so for example i can do this i'm just going to copy these two lines of code paste them underneath they're still part of the procedure and i'm assigning new values to those variables which i'll output in the same way as i did before i can just borrow this line of code and copy it so watch what happens when i run the program hello and welcome kevin drumm i hope you are well hello and welcome mervyn drake i hope you are well this makes the point that the contents of a variable can change while the program is running i can assign different values to those variables at run time in the next video i'll say more about the different data types that you can use when you declare a variable and what implications this has for your code in this video i want to say more about different variable data types i'm going to store some details about a car let's start with a new button on the form give it a name and change the text property double-clicking will allow me to write the code okay so i'm going to start with a couple of string variables to store the make and the model we've come across string variables already let's assign some values a ford escort a classic car i also want to store the number of doors that this car has this time i'm using a data type of integer an integer is just another name for a whole number notice the naming convention i'm using i doors i for integer i want to store the color of the car this is going to be another string variable i want to store some information about whether or not this car has been taxed this time i'm using a data type of boolean named after the computer scientist george boole a boolean variable can have one of two values true or false this car has been taxed boolean variables are good when you have yes no type data for example whether somebody is a vegetarian or not whether something is switched on or something is switched off i also want to store the engine size another integer 1200cc i'd like to store the price of this car i'm using a data type of decimal decimal is the most suitable data type when you want to store money values i could have used double and i could have used single because all three data types decimal double single allow me to have a decimal point with decimal places after it but double is twice as accurate as single i can have more decimal places and the data type decimal is the most accurate of them all which makes sense when you're doing calculations with money the final thing i want to store is the date that the car was registered so i'm going to use a data type of date there's a special way that i have to assign a value to a data variable notice i'm using the hash symbols to enclose the date i should also point out that this is not the 11th of february but the 2nd of november when you type a date you need to type it in american format which is the month followed by the day followed by the year when i display this on the screen for example in a message box it'll be displayed according to the settings in my computer's control panel you'll see in a moment that my computer is set up to display dates in british format now let's build a message box command to display all of this data notice the concatenation operators between the variable names and notice also that i'm concatenating spaces between these data items now you can see i have a bit of a problem here with reading the code i've written on the screen nevertheless let's test it and see if it runs there's my message and it looks okay notice that the day and the month of the date are the other way around from the way i coded it i've written the date in american format but it's being displayed in british format what i'd really like to be able to do is display each data item on a separate line within the same message box let's see how to do that instead of concatenating spaces in between each data item i'm going to type a special constant i'll say more about constants later suffice to say i'm just going to type vbnewline and i'll replace each space with this let's just copy and paste it ctrl c to copy ctrl v to paste i still need the same number of concatenation operators to join all these different bits and pieces together let's see how it looks now well that is certainly a nicer message but now i want to do something about the problem that i can't actually read the whole line of code i could give myself some more space on the screen by just dragging this bar across a little bit but even then i can't read the whole line of code i essentially want to wrap this text on the screen well i can do this after the ampersand here in fact i can put this pretty much anywhere i'm just going to leave a space and then type an underscore character when i press enter i've moved the rest of the line onto the next line down i can do that again here notice the underscore characters have actually disappeared but this is still just one line of code it won't change the appearance of the message box but it just allows me to read the code more easily on the screen let's make sure it still works yeah that's absolutely fine in the next video i'll show you how we can capture input from the user of an application so we can assign the contents of these variables at runtime rather than hard coding the data like we've done here i want to show you how to capture input from the user of your application and i'll be doing it by means of this form and you'll also see how to use some of these extra controls that we can place onto a form before i do let me just show you a quick and easy way we can capture input from the user by means of the input box function i'll drop a button on the form first give it a name and a text property double-click to write the code the input box function will capture one piece of data specifically it'll capture a string i'll need a variable to put that piece of data into so let's declare a string variable first of all and then i can assign a value to it by means of the input box function just like this and to prove it works i'll output the contents of that variable i'm concatenating the word hello in front of it let's give it a try press the button to launch the procedure and there's the input box prompt please enter your first name that's working fine so it's a quick and easy way to prompt the user for some input but what i really want to talk about in this video is how we can use controls on the form to capture input so i'm going to delete the code you can see here and do something else instead let's go back to the form i'm going to place a text box on the form here it is in the control toolbox i'll give the text box a name notice at the moment it's called text box one i've prefixed the new name with txt to indicate that it's a text box and then i'm calling it first name because that's the data i want to capture i'm trying to keep the name meaningful notice the camel notation as well you'll see the benefit of doing it this way in a few moments i'm also going to put a label on the form so that the user of the form knows what to type into the text box here's the label control and again i'm going to change its name to keep it meaningful lbl for a label and then something to indicate its purpose a label also has a text property which i'm going to change and you can see that is what appears on the form while i'm at it i'm going to add another text box to capture the user's last name and a matching label and one more text box to capture the user's gender notice that a text box also has a text property but anything i type in here will actually appear in the text box it will become the default data if a user doesn't type something into the text box i'm going to leave it blank and we'll have another matching label to go with this text box there are a couple of things you need to be aware of when you're placing controls on the form each control must have a unique name so this one you can see i called it txt first name there's the name property and this one i called txt last name let me just make the mistake of giving this text box the same name as the other one and see what happens property value is not valid take a look at the details and it's telling me that the name is already in use by another component something else on the form has already been called txt first name if this happens just cancel and find out which one is the offending control something else i should mention is you can't have spaces in a control name so let's put a space in this text box name again an error message and maybe i'll even try putting a question mark on the end of the name another error message so there are some limitations as to what you can call a text box or indeed any other control on the form the text property on the other hand of a label or a button can be pretty much anything you like okay let's just tidy up the layout a little bit i'm watching the guidelines for assistance and you can also use the format menu to help you control the layout of a form see i have options for alignment horizontal and vertical spacing this one is particularly handy if i select all of these text boxes i can say i want to make them all the same width for example and i can make them all the same height if i want to move things around as a group i just draw a box around them to highlight them and i can drag them to a new position you can play around with the various tools available to improve the cosmetic appearance of your form when you have time but for now i just want to show you how we can capture some data so let's write the code for the button i'm going to start by declaring three variables into which i'm going to load the data that i capture i'm using a standard naming convention again st because these are string variables and then of course something meaningful in camel notation i've just spotted a spelling mistake as well let's fix this now i'm going to take the information that the user types onto the form and load it into these variables starting with the first name st first name equals a single equal sign means i'm about to assign something to this variable and the information i want is on the form in a text box called txt first name i've just typed the first three characters of the text box name txt and i'm being presented with a list of the text boxes on the form you can see why i use this naming convention as long as i make sure that all of my text boxes begin with txt i'll get a nice little list of them so i'll select txt first name and i want to take the text property in other words the text that the user typed into that text box and i'm going to load that into my variable so this reads as take the text from the text box called txt first name and assign it to that variable let's do the same with the other variables and finally i'm going to output the data in a message box just to make sure it worked i've included a space between the first name and the last name and i've concatenated some extra text in my message as well let's see what happens when we run it then type in some information and there's the output from my procedure if i dismiss the message box the application is still running so i can change the data here the program is assigning new values to those variables overwriting the existing ones one more time so there you have it capturing data from a user by means of text boxes on a form let's quickly take a look at another control which i can use on the form i want to show you a list box and i'm going to use this to capture the user's occupation so let's give it a name lst occupation lst because it's a list box i'll put a label on the form to go with it so that the user knows what the list box is for and i'm going to include some occupations in the list box that the user can choose from first of all make sure that the list box is actually selected and then use the items property there's a little button here which will allow me to add a collection of items click ok and i now have a populated list box let's capture the user's selection i need a variable to store it in and i can capture the user's selection in a slightly different way i'm using the selected item property let's try it out hello kevin drumm you are a male teacher i can also add items to the list box programmatically in other words in my code i'll quickly show you this and then you can experiment with it yourself you might remember in an earlier video i showed you how you could write code when the form loads i'm going into the form design environment and i'll double click the form itself i now have a procedure called form1 load any code i write here will run immediately after i start the application but before the form appears on the screen and i'm going to add some extra code to put a few more items into my list box i am adding something to the collection of items which are inside that list box i'm just going to make sure that the list box is big enough to display everything and let's give it a try hello beatrix potter you are a female writer before we look at some more code i want to introduce you to some of the facilities that come with visual studio for debugging your code in other words tracking down errors in the code the first and probably most useful one that i want to talk about is being able to set a breakpoint and then step through the code i can set a breakpoint by clicking on the grey bar here that's a break point now i'm going to run the program watch what happens i'm now in what's called break mode or debugging mode my application ran at full speed until it came to the break point and now it's suspended and i can step through the code one line at a time there are a number of buttons i can use to do this up here step into step over step out i'll say more about these two later the most useful one is step into notice i can also use the function key f8 to do the same thing so when i click the button i'm getting a message about how the stepping facility will behave i'm not too worried about this for now i'm just going to say i don't want to see these messages again it's highlighting the line of code that it's about to execute next and now it's about to execute this line of code and again it's about to execute this line of code when i'm in debug mode i can hover over the name of a variable to see its contents so i can see that the string male has been assigned to this variable drum has gone into this one and kevin has gone into that one if i take a look at this variable it hasn't been assigned a value yet because this line hasn't executed yet let's continue stepping and i can see there's something inside that variable now final line of code in this procedure will do some output so i've been switched back to the user interface and finally the procedure will come to an end my application is still running the form is still on the screen but the procedure which i launched when i clicked on the button has now finished i can use as few or as many break points as i like i'm going to remove this break point just by clicking on it again and i'll place a break point here instead i'm also going to place a break point on the forms load event handler let's run it again now as i told you in the previous video when you launch a windows forms application the form is loaded into memory before it hits the screen so the forms load event handler is running first i'll step through that and it's populating the list box when that's complete the form can hit the screen and clicking the button causes the button's click event handler to start running and we've seen this before when we're in debug mode there's some extra windows that you might want to switch on a particularly useful one is called the locals window you can find it here on the debug menu debug windows locals what it's showing me is the contents of any local variables in other words variables which have been declared within this procedure i'll say more about the difference between local and global variables later on suffice to say for now this is an alternative way of finding out what's inside a variable i can see a nice summary down here there's additional information in this window as well which pertains to these parameters which i mentioned in another video we'll say more about these later with the locals window i can actually change the contents of a variable on the fly as it were i just double click it here and retype it i've overwritten the existing value of that variable if you've finished stepping through your program you can stop the execution just by clicking on this red square or you can press continue and it will jump you straight to the next breakpoint if there is one at this point i just want to return to a type of error that you've seen already and that's the so-called syntax error for example if i misspell the word text i type text on the end i've got a red wavy line if i open a bracket but forget to close it again that's a syntax error i can see a red wavy line on the end of this keep an eye out for these because your program won't run properly if you don't fix them i like to fix them as i go along but let's see what happens when i try to run a program which has got syntax errors like this in it there were build errors would you like to continue and run the last successful build well that begs the question what does it mean by build so let's say no and i'm going to switch on something called the output window watch what happens when i try to run the program again build xero succeeded one failed this tells us something very important about vb.net programs when you write a program you're writing source code code that a human being can understand but before you can run a program it has to be compiled what that means is it has to be turned into machine code binary ones and zeros that the computer can understand when you press the start button visual studio will automatically try to compile the program first another name for compiling a program or compilation is to build the program so you can see here the program has failed to compile the build failed and i'm very kindly being asked would i like to use the last successful build would i like to run the version of the program that compiled successfully previously well in this case the answer is no i want to fix this version of the program so i'm going to click on no and now i have a list of errors this is really just a summary of the same information that i saw with the red wavy lines i'm being told that it's expecting a closing bracket a double click on there and it jumps me to where i need to do the fix and i'm being told that text is not a member of text box in other words text is not a property of a text box a double click and it will jump me straight to that line i can fix it here now as i said before though i like to watch out for the red wavy lines while i'm coding and fix them as i go along i'll show you some more debugging facilities as and when i need them in later videos in this video i'm going to talk about how we can work with numeric input from the user in other words how we can do calculations with number data before i do i'm going to build myself a simple little interface with a couple of text boxes and a button notice how i've been naming the controls as i go along this will make the code easier to understand the first thing i'd like to do is add together two numbers that the user types onto the form so i'll start by declaring a couple of variables remember the data type integer means a whole number i'm going to declare another variable to store the result of the calculation and now i'm going to capture the data that the user types into those text boxes adding these together couldn't be simpler i result equals i number one plus i number two let's output the result and see if it works 12 plus 5 is 17. let's try something else let's subtract one number from the other i result equals i number one minus i number two i'll just borrow that message box statement and we'll run the program again so that's the two numbers added together and that's what we get when we subtract 5 from 12. what about multiplication to multiply two numbers together we use an asterisk that's the multiplication symbol when you're programming twelve plus five is seventeen twelve minus five is seven and twelve times five is sixty bit of copying and pasting to speed things up to divide one number by another use a forward slash so this reads as i number one divided by i number two twelve plus five is seventeen twelve minus five is seven twelve times five is sixty and twelve divided by five is two well not quite but remember we're working with integers here we're working with whole numbers when i divide one integer by another and store the result in an integer variable vb is automatically going to round to the result either up or down let's improve on this instead of using integer variables i'm going to use a real number type let's use a double i'm also going to change the naming i've used here dbl for a double strictly speaking it doesn't matter what i call these variables but i want the names to be meaningful and i want the names to reflect the data type let's see what we get this time same as before 12 plus 5 is 17 12 minus 5 is 7. 12 times 5 is 60 but 12 divided by 5 is 2.4 that makes more sense just to be clear about the way a variable works i'm multiplying two numbers together here and i'm storing the result in this variable called dbl result when i perform a second calculation i'm storing the result in the same variable what's happening is i'm overwriting the existing value in dbl result i lose the value which was generated by the first calculation when i do the third calculation i overwrite the value inside dbl result again so i lose the result of the second calculation by the time my program comes to an end this contains the result of the first number divided by the second number if i wanted to keep the result of each of these calculations then i should have declared four separate variables these are called arithmetic operators these are the basic operations that we might want to perform with numbers there are a couple more let me show you them that's the carry symbol which is normally above the number six on your keyboard and what it means is raised to the power of so it's going to raise number one to the power of number two twelve plus five twelve minus five twelve times five twelve divided by five and twelve to the power of five that's twelve times twelve times twelve times twelve times twelve another useful operator is integer division i'm going to use a backslash instead of a forward slash in some programming languages you would use the word div in fact the pseudo code that you see on most exam papers will use the word div in visual basic we use a backslash let's take a look that's the effect of using a backslash instead of a forward slash 12 is being divided by five but only the whole number part of the result is being kept twelve divided by five is two with two remainder we're ignoring the remainder let's try fifteen and four fifteen plus four is nineteen fifteen minus four is eleven fifteen times four is sixty fifteen divided by four is three point seven five that's fifteen raised to the power of four and that's 15 divided by 4 ignoring the remainder 4 goes into 15 three times the last arithmetic operator that i'd like to show you now is mod number one mod number two mod is the remainder after whole number division let's see what this does let's go with 24 and 9 this time i'll just move the form across so you can see the code as well 24 plus 9 is three twenty four minus nine is fifteen twenty four multiplied by nine is two hundred and sixteen twenty four divided by 9 is 2.6 recurring the number of decimal places you can see there is governed by the data type 24 raised to the power of 9 is this rather large number 24 divided by 9 using integer division is 2 9 goes into 24 twice and 9 goes into 24 twice with 6 remaining that's what mod is giving me it's the remainder after whole number division let me finish with a word of warning i'm going to run this program again but i'm not going to enter any data i'm going to leave these two text boxes empty straight away i have an error message conversion from string to type double is not valid this tells me something about what's going on behind the scenes let's stop the program and put a break point on this line when you type something into a text box it's actually a string notice here text equals 5 7 in double quotes when this line of code executes vb will automatically convert the string five seven into a number you can see dbl number one has got a zero in it at the moment that's the initial value of any numeric variable until you assign something else to it step through the code the text box contained five seven but the variable contains fifty seven behind the scenes visual basic is automatically converting the string into a double because it can but notice that my second text box contains a zero length string a pair of double quotes with nothing in between them it's a string but it just doesn't have any length and when this line of code executes vb will attempt to convert that zero length string into a number but of course it can't and my program has crashed this is what we call a run time error the program crashed while it was running it crashed at run time another name for a runtime error is an exception so you can see here i have an unhandled exception i'll talk more about runtime error handling or exception handling in a later video but for now with this program we just have to expect the user to do the right thing not to leave any of the text boxes blank in this video i'll say something about complex arithmetic that is arithmetic expressions that contai

Original Description

Lean the fundamentals of programming with Visual Basic (sometimes called Visual Basic .NET or VB.NET). In this tutorial, you will learn about the basic constructs of high level programming languages, including sequence, selection and iteration. You will learn how to build an event-driven, form-based, user interface to capture input, and you will learn how to write code to validate and process the data collected. 🔗 Get Visual Studio for free: https://visualstudio.microsoft.com/ ✏️ Kevin Drumm developed this course. Check out his YouTube channel: https://www.youtube.com/c/ComputerScienceLessons ⭐️ Course Contents ⭐️ ⌨️ (0:00:00) Introduction ⌨️ (0:00:46) Hello Visual Studio ⌨️ (0:13:44) Customise The Visual Studio IDE ⌨️ (0:19:13) Output and Variables ⌨️ (0:34:26) Variable Data Types ⌨️ (0:41:56) Input with Windows Forms ⌨️ (0:54:18) Debugging Code ⌨️ (1:02:47) Arithmetic Operators ⌨️ (1:14:24) Complex Arithmetic Expressions ⌨️ (1:22:50) Selection with If Statements ⌨️ (1:32:58) Logical and Relational Operators 1 ⌨️ (1:47:32) Logical and Relational Operators 2 ⌨️ (1:56:37) Select Case ⌨️ (2:02:57) For Next ⌨️ (2:08:17) Practice For Next Loops & If Blocks ⌨️ (2:12:39) Do While ⌨️ (2:21:24) Condition Controlled Loops ⌨️ (2:29:39) Array Variables ⌨️ (2:39:48) Practice Arrays & Loops ⌨️ (2:49:46) Linear Search ⌨️ (2:56:24) Two Dimensional Arrays ⌨️ (3:07:00) 2D Arrays & Nested Loops 🎉 Thanks to our Champion and Sponsor supporters: 👾 Raymond Odero 👾 Agustín Kussrow 👾 aldo ferretti 👾 Otis Morgan 👾 DeezMaster -- Learn to code for free and get a developer job: https://www.freecodecamp.org Read hundreds of articles on programming: https://freecodecamp.org/news ❤️ Support for this channel comes from our friends at Scrimba – the coding platform that's reinvented interactive learning: https://scrimba.com/freecodecamp
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from freeCodeCamp.org · freeCodeCamp.org · 0 of 60

← Previous Next →
1 React: Production Server Setup Part 2 - Live Coding with Jesse
React: Production Server Setup Part 2 - Live Coding with Jesse
freeCodeCamp.org
2 cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
freeCodeCamp.org
3 Browser history tutorial - Beau teaches JavaScript
Browser history tutorial - Beau teaches JavaScript
freeCodeCamp.org
4 Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
freeCodeCamp.org
5 React: Parameterized Routing with Next.js - Live Coding with Jesse
React: Parameterized Routing with Next.js - Live Coding with Jesse
freeCodeCamp.org
6 React: Dealing with jQuery Issues - Live Coding with Jesse
React: Dealing with jQuery Issues - Live Coding with Jesse
freeCodeCamp.org
7 setInterval and setTimeout: timing events - Beau teaches JavaScript
setInterval and setTimeout: timing events - Beau teaches JavaScript
freeCodeCamp.org
8 Browser and Device Testing - Live Coding with Jesse
Browser and Device Testing - Live Coding with Jesse
freeCodeCamp.org
9 Last Minute Updates - Live Coding with Jesse
Last Minute Updates - Live Coding with Jesse
freeCodeCamp.org
10 Post Launch Updates - Live Coding with Jesse
Post Launch Updates - Live Coding with Jesse
freeCodeCamp.org
11 React: Setting Up Google Analytics - Live Coding with Jesse
React: Setting Up Google Analytics - Live Coding with Jesse
freeCodeCamp.org
12 React: Masonry Layout - Live Coding with Jesse
React: Masonry Layout - Live Coding with Jesse
freeCodeCamp.org
13 Load Balancing Digital Ocean Droplets - Live Coding with Jesse
Load Balancing Digital Ocean Droplets - Live Coding with Jesse
freeCodeCamp.org
14 try, catch, finally, throw - error handling in JavaScript
try, catch, finally, throw - error handling in JavaScript
freeCodeCamp.org
15 Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
freeCodeCamp.org
16 Graphs: breadth-first search - Beau teaches JavaScript
Graphs: breadth-first search - Beau teaches JavaScript
freeCodeCamp.org
17 React: Masonry Layout Part 2 - Live Coding with Jesse
React: Masonry Layout Part 2 - Live Coding with Jesse
freeCodeCamp.org
18 React: WordPress API Live Search - Live Coding with Jesse
React: WordPress API Live Search - Live Coding with Jesse
freeCodeCamp.org
19 Creating WordPress Custom Post Types - Live Coding With Jesse
Creating WordPress Custom Post Types - Live Coding With Jesse
freeCodeCamp.org
20 Dates - Beau teaches JavaScript
Dates - Beau teaches JavaScript
freeCodeCamp.org
21 Miscellaneous Front End Updates - Live Coding with Jesse
Miscellaneous Front End Updates - Live Coding with Jesse
freeCodeCamp.org
22 Merging a Pull Request from GitHub - Live Coding with Jesse
Merging a Pull Request from GitHub - Live Coding with Jesse
freeCodeCamp.org
23 React + Prettier + Standard JS - Live Coding with Jesse
React + Prettier + Standard JS - Live Coding with Jesse
freeCodeCamp.org
24 React: Sortable Responsive Table - Live Coding with Jesse
React: Sortable Responsive Table - Live Coding with Jesse
freeCodeCamp.org
25 Geolocation Sorting by Distance - Live Coding with Jesse
Geolocation Sorting by Distance - Live Coding with Jesse
freeCodeCamp.org
26 Tradeoff Matrix - Agile Software Development
Tradeoff Matrix - Agile Software Development
freeCodeCamp.org
27 The Definition of Ready - Agile Software Development
The Definition of Ready - Agile Software Development
freeCodeCamp.org
28 Getting first React job without experience - Ask Preethi
Getting first React job without experience - Ask Preethi
freeCodeCamp.org
29 React: Google Analytics Click Tracking - Live Coding with Jesse
React: Google Analytics Click Tracking - Live Coding with Jesse
freeCodeCamp.org
30 Submitting a PR to an Open Source Project - Live Coding with Jesse
Submitting a PR to an Open Source Project - Live Coding with Jesse
freeCodeCamp.org
31 Should I go back to school to get CS degree? - Ask Preethi
Should I go back to school to get CS degree? - Ask Preethi
freeCodeCamp.org
32 Hero Section CSS Changes - Live Coding with Jesse
Hero Section CSS Changes - Live Coding with Jesse
freeCodeCamp.org
33 Working Agreement - Agile Software Development
Working Agreement - Agile Software Development
freeCodeCamp.org
34 A day at Pennybox with Co-Founder Reji Eapen
A day at Pennybox with Co-Founder Reji Eapen
freeCodeCamp.org
35 React: Sorting and Filtering Data - Live Coding with Jesse
React: Sorting and Filtering Data - Live Coding with Jesse
freeCodeCamp.org
36 React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
freeCodeCamp.org
37 React: Building a New UI - Live Coding with Jesse
React: Building a New UI - Live Coding with Jesse
freeCodeCamp.org
38 Definition of Done - Agile Software Development
Definition of Done - Agile Software Development
freeCodeCamp.org
39 Getting started with jQuery (tutorial) - Beau teaches JavaScript
Getting started with jQuery (tutorial) - Beau teaches JavaScript
freeCodeCamp.org
40 Making a React Blog with WordPress Content - Live Coding with Jesse
Making a React Blog with WordPress Content - Live Coding with Jesse
freeCodeCamp.org
41 React, NextJS, CSS - Live Coding with Jesse
React, NextJS, CSS - Live Coding with Jesse
freeCodeCamp.org
42 jQuery events - Beau teaches JavaScript
jQuery events - Beau teaches JavaScript
freeCodeCamp.org
43 React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
freeCodeCamp.org
44 React: Working with API Data - Live Coding with Jesse
React: Working with API Data - Live Coding with Jesse
freeCodeCamp.org
45 React: Refactoring Components - Live Streaming with Jesse
React: Refactoring Components - Live Streaming with Jesse
freeCodeCamp.org
46 jQuery effects - Beau teaches JavaScript
jQuery effects - Beau teaches JavaScript
freeCodeCamp.org
47 More React Refactoring - Live Coding with Jesse
More React Refactoring - Live Coding with Jesse
freeCodeCamp.org
48 animate in jQuery - Beau teaches JavaScript
animate in jQuery - Beau teaches JavaScript
freeCodeCamp.org
49 "Finishing" My React Site - Live Coding with Jesse
"Finishing" My React Site - Live Coding with Jesse
freeCodeCamp.org
50 Starting a New React Project (P2D1) - Live Coding with Jesse
Starting a New React Project (P2D1) - Live Coding with Jesse
freeCodeCamp.org
51 React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
freeCodeCamp.org
52 The Agile Manifesto - Agile Software Development
The Agile Manifesto - Agile Software Development
freeCodeCamp.org
53 jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
freeCodeCamp.org
54 React Project 2 Day 3 - Live Coding with Jesse
React Project 2 Day 3 - Live Coding with Jesse
freeCodeCamp.org
55 The INVEST approach to product backlog items
The INVEST approach to product backlog items
freeCodeCamp.org
56 React Project 2 Day 4 - Live Coding with Jesse
React Project 2 Day 4 - Live Coding with Jesse
freeCodeCamp.org
57 Chickens and Pigs - Agile Software Development
Chickens and Pigs - Agile Software Development
freeCodeCamp.org
58 React Project 2 Day 5 - Live Coding with Jesse
React Project 2 Day 5 - Live Coding with Jesse
freeCodeCamp.org
59 jQuery: add and remove DOM elements - Beau teaches JavaScript
jQuery: add and remove DOM elements - Beau teaches JavaScript
freeCodeCamp.org
60 React Project 2 Day 6 - Live Coding with Jesse
React Project 2 Day 6 - Live Coding with Jesse
freeCodeCamp.org

This video course provides a comprehensive introduction to Visual Basic (VB.NET) programming, covering the basics of programming, including variables, data types, input/output, and control structures. It also introduces event-driven programming and provides hands-on experience with Visual Studio. By the end of this course, learners will be able to write and debug their own VB.NET programs.

Key Takeaways
  1. Install Visual Studio
  2. Create a new VB.NET project
  3. Write and debug VB.NET code
  4. Use variables and data types
  5. Implement control structures
  6. Create event-driven programs
💡 Understanding the basics of programming, including sequence, selection, and iteration, is crucial for building a strong foundation in software development.

Related AI Lessons

You Are Not Behind. The World Is.
You're not behind, the world is still adapting to AI, and it's okay to take your time to learn and grow
Medium · AI
Career choice with the advent of AI - pure Computer Science or learn software with a background of core engineering area
Learn how to choose between a Computer Science and Engineering career path or combining programming with a core engineering background in the age of AI
Dev.to AI
The AI Hype Cycle: Calm Before the Next Breakthrough?
Understand the AI hype cycle to anticipate the next breakthrough and make informed decisions
Medium · Programming
AI won’t replace scientists. It will make the current model of science obsolete
AI is not replacing scientists, but rather making the current model of science obsolete, enabling new forms of discovery and collaboration
Medium · Data Science
Up next
Motorist saved by human chain | 9 News Australia
9 News Australia
Watch →