Transcripts
1. Introduction: Hey, guys, I'm excited to welcome you to my new course entitled C Sharp Consul and Windows Forms Development with Link and A T or dot Net. Now that title is quite a mouthful, but I can assure you that we have quite a bit off content in store for you. This is a beginner to intermediate course, where I will take you through the basics off C sharp programming. You should be able to understand basic C sharp context. The blocks of cord, the different tokens that occur. The breezes holds me commence. Variables, collections, object oriented programming. All of those things will be imparted to in this course. I take a very beginner friendly approach. I assume you have no prior experience. Programming are developing applications and so I bring you step by step through each off the concepts. By the end of this course, you should be able to understand how to create our own Windows applications. Both console Onda graphical user interface using Windows forms. You'll also be learning a bit off database development as we will be building a data driven Windows forms application. Onda. We will learn how to use our tools like a tub on visual studio on. By the end of this course, you should be comfortable in either environment and be able to build a full enter end application. No, I want to bring you through why it's important for you to learn dotnet and see sharp Microsoft. Net is one of the most popular platforms that is used to build enterprise applications all over the world. You can get jobs basically anywhere in the world. If you're a document developer on, people will pay you well. The whole document platform, as well as allowing would see Sharp, which is the most popular language used in the Darknet platform, can be used to develop a wide range of applications for mobile devices for desktop applications. More recently, limits on dMarc also anything on Windows and also Web applications for this course will be using Visual Studio 2019. Community Edition, which is a fully featured I. D. E. I. D. Short for integrated development environment on it is very modern. It's very poor fool. You can use it to build a wide range off applications it's pre integrated with get and get hub on Did. It has some database management capabilities in it also. No, I did mention get up, and I'm just going to give you a brief introduction. Estelle, what that is it is source control that will allow us to track our changes being made. It's also a great way to back appear cold because if your machine crashes and you have a copy off your project up and get up, then it's easy to recover from that point on. Industry loves to know that you know how to use tools like get up. So I will be showing you how to use Get top, Andi. It will off course come off as a great benefit to you. Now, with all that being said, it is good if you have some database development knowledge on golf course you need an email address on Do you need the willingness to learn and grow That being said, However, if you don't really have all of that knowledge, it's fine. I take you step by step, and once again, this is a verb beginner friendly course. And if you're not a beginner but your intermediate, I'm sure you can find some value in this course. So welcome on, I'm glad to have you
2. Install Visual Studio 2019 Community Edition: in this video, you get started setting up our development environment, so we'll be installing visual studio. We can get to their upset by going to visual studio dot Microsoft dot com and then we will proceed to Don't. Lord. The community edition there is also professional Under is also enterprise, both of which they offer trial versions as you need to buy the license. But since we are individuals who are doing this for educational purposes are not for enterprise purposes, then we can proceed with 2019 Community Edition. Selecting that option triggers are don't Lord, which, when completed, we can execute the program. This launch is on installer, and it's a pretty straightforward process. It justly continua lowered toe, do some more, don't lose and checks. At the end of those checks, they will launch a screen, asks you to select the models that you wish to install, since we're doing C sharp programming for dotnet and for next top, then we need to make sure that we select dot net for desktop development So we just take that box. If nothing else gets ticked, that one needs to be taken and to the redial kind off list or what you'll be getting as part of this bucket. So I'm going to take some other things because I would definitely want to include some other DOTNET framework versions. No, the reality is that each version comes with its own compatibility, You know, support for certain things and also reverse compatibility for older systems. So I am just taking all of them to make sure that I have them as options when needs be. No, you look to the bottom right, and then they see the total space required is five points here for gigabytes, and that's just for one model. I could have ticked other models once again. I don't need them for this, but I'm just saying that visual studio is very powerful, and it allows you to select the kind of development libraries that you will need for your context. After selecting dot Net, they're stopping development that package. Then we can just go ahead and click install so this installer will proceed to Don't load all of the packages from the Internet, and this may take a while, so I will reconvene when this is done. Once that installation process is finished, then we can go ahead and launch is your studio. If it doesn't launch automatically along the way, you may be prompted to identify yourself using a live accounts. No, my live account is already kind off registered on my machine, so it automatically detected me on signed me in. But if you do not receive that automatic signing, then I encourage you just create a level content by live account. I mean, you can simply go to Microsoft dot com, proceed to click sign in. You don't have an account, so you just create one on then The cool thing is that you don't have tohave on act some micro saucers that kamajii me works at Yahoo Works. Whichever email address you use primarily. You can use that as your live account. And then once you've completed that registration with that live account, then for visual studio you can proceed to use that live account to sign in notes that the community addition is very free. But then they will tell you that you have a 30 day trial on that is really pending your creation off our live account and using it to authenticate in visual studio. Once you get to this screen. Then you know that your installation has been completed and as we go along with, see how we create a project and understand our development environments.
3. Install .Net Core SDK: to complete our development environment configuration, we need to install dot net core. This may not have come out of the box with visual studio or the version that was in visual studio, maybe a bit dated. So the latest version or the version at the time off this recording is dot net core 3.1 Soto, Get that one. You just goto dot net dot Microsoft dot com slash download Already could just google dot net core, and it will bring you to this site on. Then you can proceed to download the sdk. So you know, it says I have done that core 3.1 on the latest dotnet framework, which also may not have been included in that visual studio installation. So you can don't know both what I'm really good. I went to focus on the core because that's when we really want Andi. Just click. Don't know sdk. It will launch a don't load on. Then when that is finished, then we look at the installation process. When you launch the installer, it's pretty much straightforward. You just need to click install on, let it go through any security checks on, then it will just install dotnet core STK for 3.1, which at the time after this video was the latest version. So once that is completed and we reconvene at the end of the installation, you will see a confirmation page on the most important line here is the installation was successful. Once you see that you can click close Onda that's it for setting up your development environments.
4. UPDATE: Visual Studio 2022 Community Edition Tour: Hey guys. In this lesson we'll be installing Visual Studio 2022, which ships with dotnet six, which is the fastest dotnet yet. So to get started, we just need to go over to Visual Studio dot Microsoft.com slash downloads. And then we will go ahead and hit the free download on the community edition, which will then give us an installer. So once you run that installer, we will be able to select the workloads that we want. Workloads really refer to the libraries or the supporting libraries for the type of updates you're interested in creating. So in this case, we definitely want to get that web development ASP.net and web development workload. But then if you are interested in older technologies, you can go ahead and select them. So you can see I have several workloads selected with Azure development, NodeJS and desktop development. So you can go ahead and split those also. I think that they will come in handy with other projects that you might end up building. All in all, once you have done that, you can go ahead and hit Install, which would be done by the bottom right-hand corner. Of course, the more workloads you select is the bigger the download size. So once you've completed that, then you can continue. Now when that download is finished, you may get certain options that I am not going to get because I already have it installed. So this will walk you through what to expect. You may get an option where you get to choose your theme. As you can see, I'm using the dark theme, but then you do have the option of a light theme, a blue theme. And the thing is that Visual Studio 22 has many more themes than its predecessors. So even if you don't like the initial themes, you can get other themes if you need them. Also, you may be prompted to sign in. So if you already have 2019 installed on your machine and you are just installed in 2022, you already signed in 2019, then it probably won't prompt you to sign in again. However, if this is your first installation of Visual Studio, you may be prompted to sign in or create a live account, which point you would want to just use your Microsoft issued at live at all. Look at hotmail.com at cones. Or you can just go ahead and create one. And if I if my memory serves me correct, you can use an another non-Microsoft email address to create a lighter color. So if I go ahead and create a new project and do a new console up, C Sharp console up. And then this is just going to be VBS 2020 to demo. All right, head hit Next C. C is the same thing, but here I can choose which framework and I can now choose the latest dotnet six. So dotnet 5 was released last year and is occurring one dotnet three-point one sit as long term support. So it's still quite capable. And each one is actually backwards compatible. So five, most of what you do in three, most, if not all of what you do in 3.1 is still compatible with five. And most, if not all of what you would have done with these two can be forward, forward it to dotnet 6.net, 2.1, a lot of support. So I would suggest if you have any ups still in two-point one, that you start looking to upgrade or update them. Or at least if you're just learning dotnet Core, don't start with 2.1. So when we go ahead and hit Create, another thing I take note off. So I paused the video, but it wasn't really that Quetelet. I paused the video, but one thing I took note of is that it is much fossa. So thats is probably because it is on a 64 bit application, so it's making better use of your 64 bit machines resources than previous versions would have. So no, you're going to see Foster search times, faster load times. They'll solutions with 109 projects, it shouldn't take as long to load them up. So another thing that I want to point out is that this is a console app. And if you have any experience writing a typical C-Sharp application, you will know that there is a standard template where you have a bunch of using statements at the top. And then you have int main Java class, and then you have int main, and then you have the code in there. But here all I'm seeing is the code. So this is done at 66. Actually got rid of that whole template. So let me just add dotnet five console app project quickly. Let me just call this one demo, met five so we know Next, and then this one is done five. So that is what we're probably cost them too when we look at dotnet five projects or done it five console project or code file. So you have your class, you have your static void main, and then you have your code. However, in dotnet seeks, they got rid of the wrappers around all of that. And the just start writing code, right? So this is not, this is not a C-sharp tutorial. I'm just pointing all the little nuances in C-Sharp tutorial. I will definitely go through all of that and the differences between the dotnet, regular dotnet C-sharp and what dotnet 6 brings to the table. But it's really cool As far as I see it. Now another cool feature to me is it's increased intelligence when it comes to code hinting are hinting what you might be getting. That's why they are getting a quote. So I can see this as a huge productivity when the dope, the feature IntelliJ code. And you'll see that little icon down here that's allows you to kind of toggle some of the options. So if I type console, you see that it's kind of auto completing dot write line. It's not waiting for me to go into the list and then select it. I can just press Tab and then tub as long as they're agreed. Characters to the right of the cursor, I can press tab and then it will just fill them in, right? I am here. So let me try something else. If I do something like var, var num one is equal to and then seed suggesting that I probably want to initialize it to 0. Let me see if it's really that intelligent okayed. So just the norm to what if I wanted var name, what would it suggests var name? And then a space and then okay, So I guess I broke it. But you can see what it's getting at maybe. All right. So I guess varName is vague because you know, you need the type 4 to know what exactly what kind of variable It's going to be before you give the value. So if I said string name one, Let's see what it would suggest. String name1, know suggestions. Alright, so that's a feature, I guess that over time it will increase in its offering because this is really based on goal completions, based on other code samples from various repositories all over the place. So I guess random variable names that these are rarely used, which is probably why it's having such a hard time understanding what I am getting it. But then you can see with the num 1, num 2 if you've already told art, so I just press enter. It's suggesting console.log, line tab. And then it's going to suggest another one, tub. So read, they're just printed num one without writing a code line of code, I look at this, I press tab ones and I'm printing num 2. So I think, you know, as you use it, it will seem easier. It will make better suggestions. It's clearly contexts to all because it knows that num1 and num2 are in the document, are in the program. So it knows that he can suggest it later. Don't for my operations.
5. Tour Of Visual Studio: in this video, we're going to be pouring visual studio will be creating a simple console application for C Sharp on. We'll be looking at the interface and just getting familiar with all of the buttons and what all of them do. No, my visual studio is using what we call dark mode, which makes it black, so your own is probably silver or white in the interface, and I will show you how to change that. But for no, this screen launches whenever you click visual studio onto the left, you see a list off all of your recent projects off after a number of projects working on, I kind of collapsed them. So it's not toe. Put too much on the screen for display at this point, but your own will probably be empty, but you will have the option. So the right asking you to clone or check out open a project folder or create a new project so I'm going to click create a new project. This launch is the different categories off projects that are available to you. So based on the package that you would have installed, you may see more or fewer than I have on my screen. I have quite a few packages installed on my visual studio instance, so I'll have more options. But for the purpose off this lesson, we want to create a C sharp consul application so we can actually just search instead of trying to scroll through and look and try and figure out which one it is. We can search, or we can narrow it down by selecting the actual language that we want the environment that we want to develop something for, in this case, windows on the with its desktop or mobile. So be once again based on the packages that you would have installed. Then you can get that filter. So I chose C Sharp Windows console and then I have two options. I have a dot net core console up, and I have a dotnet framework console up. No, no. The difference between the tour dot net core console up creates a project that allows you to create a console application that can run on windows. Looks on dMarc OS over the traditional one. Up until Corky Mobile was just the regular dotnet framework, which created a project that can run on windows. So I'm just going to continue with the traditional dotnet framework application for no on. Then we can create a doctor core one later and compare. So I'm went toe, proceed with the dotnet framework. Click next, and then we give it a name. Someone to call this test nets console. All right, so in the project name, you want to avoid special characters as much as possible. And also, you probably want to camel case to make sure that it's readable. It is very case sensitives. If you camel case or you have a mixed case word, then rest assured that it will play a very big part in referencing anything inside the project going forward. This might be your default location based on your use anymore, your fullest set up on your computer, whoever you can always go to bro's and change where you want your projects to go. Next we have the solution name and I'll show you what the project name different from the solution name. But we can actually choose a different solution name as the project cause inside off a solution, and the solution can have multiple projects, so these two don't necessarily always have the same name. All right, on. Then we can select the version off, Done free, more that we want. So you can see that it has backwards compatibility open till dotnet framework to point. Or like I said, each framework has, you know, the latest one has more recent libraries and support for libraries. But then what works in the latest on me not work on an older computer, And then what was done on the older versions might not be very compatible with, say, Windows seven and Windows 10. So, you know, choose your battles fearful. You know what your environment is. But I have a Windows 10 computer, and I like to be on the cutting edge of things. So I leave mine at dot net framework 4.7 point two, and then I can doesn't take a cursory glance. Make sure everything is good and then click create. Once all of the preparations are done, Visual studio and launch on. Then we'll see a court file followed by you know, this wonderful interface and some other things. So the first thing I'm going to do, however you show you how to change the dark more if you're not already in Dartmoor andare interested, so you can go to tools, then select options on then the very, very first option in this dialog boxes allowing you to choose your college team. So you're probably in light or blue. You have blue contrasts and you have dark so you can to select there. Click OK on. Then you'll get this wonderful looking contrasted interface. No. Another thing you may not is that your solution Explorer is to your right minus to my left . I rearrange it that way because I find it easier to just move to the left to find the file . Sometimes I'm typing court here and they have to move all the way over to the right. I mean, it's it's really a month off layout on flexibility on whole. You prefer it? No visual studio. I lows you to put it the way you prefer it. So you can actually drug any, um, pain. They're called champions. You can drug drug, any pain, and then you can dock it where he wants. You see, these little, um, squares are tangles. If you dragged them into the space that it will kind of put it right in that section that is designated right or I can just leave it right there floating. But it's more for humbug here in the middle of the screen than anything. So I like my into the left so you can do that if you wish. If you want more real estate, sometimes you're right in court and Europeans are to the side, you know, open like this. Under take him too much space you may not have a big screen on. Do you want more space to do certain things You can always just unp in it. So you see, this pin is seeing auto heights. I can make it auto hide when that when I need it, I just click it and it comes out. I do what I'm doing and then it goes back. You'll also see that I have other pins that you know you may not have. So as the need arises and you feel that you need a pain, you can just go toe view and then you find the pain that you want. So, for instance, I have one here that's a sequel server object explorer on. That's like a mini database management system inside visual studio For this tutorial, you won't need that. But And for other projects I work on, I use it. So when I expand it, I see things in there. But then I'm not going to be using them for this for this set off lessons. Now, this court file that I had alluded to before this is our mean court file for our console up . So if I want to increase the fun size, I can just hold on on the control button and screw in and you see, I can screw up, are known to increase or decrease the size off the funds. So I'm just going to increase its a bit so that we can all see exactly what's happening on my screen in the solution Explorer. You see that the file name matches the class name. All right, so I'm not going to teach you chord in this, particulary less time. Just trying to point out some visual accused to make sure that when you see them, you know exactly what they need. Right? So we have the program dot CS and see sharp fathers are stored as dot CS the class files get extension. That's yes. But then you see that you have other files with different extensions, like not config on. As you put more and more in your project, then you're going to see different file extensions for different purposes. On being a bit more attention to the solution. Explorer, you notice that you have and I just expand minor bit. You have the solution and then you have and it does have a names. It's a solution testing console. But then under that, you see that it has another like a child, right? You see that indentation that suggest that this is a child off the solution? So this is the project which then has the project files. So I was saying earlier that the solution doesn't necessarily have to have the same name as a project. As a matter of a fact within the solution, I can actually click and remove a project if I want, or I can add other projects. So if I wanted to console ups, but I want to them under the same solution. So when I open the solution, which is my overall collection off projects that I would see every single projects. I could have one to say hello, world One to do my one to do other operations. And that's exactly how we're going to be building a boat over applications on which abusing one solution on. Then we're just going to add a project per court example that we will be doing no at the top. And I'm talking about this section that is underneath our toolbar and you see the little icons you know, you have that safe icon you have saved all that'll save all the files. So if you have multiple fires open and anything, I just want to make one my sissy Vitter's do save all. But then you notice that they give keyboard shark cuts along the way. So you know, it's easy to just do. Control s while you're writing cord than to stop moving on from the keyboard and go all the way up here. Discipline to save or to save all. So along the way, I'm going to be showing you a little keyboard shark. What's so that you can, you know, make your according experience more efficient? No, As you continue from the save button, you see that you have some configuration settings they have Deeble year of release. You have any CPU, so they load it kind off. Determine. Okay, what settings should I simulate when I am about to start my programs? So you see that? But with the green arrow and it says, Start, you can just click that whenever you want. Oh, actually, Rohner application or you can press f five. So I'm just going to start this one. So notice would generate anything, understand to start it so that you can see what visual studio looks like when it's starting a program. So it goes through. What? Because a build where compiles all of your court, off the court fans that you have on the one project on, then it will see. All right, Are there any errors or syntax errors that I need on? Then it launches what you call that console so that consulates, like command, prompt a notice. It was flashed off the screen because, well, it did exactly that. It's launched the console and then looked inside off our main function. The main function didn't tell you to do anything, so it came on the screen. So that's that. Nothing to do so it came off so that so console applications were Iran on. They do a task until they're completing the half. Completed the task on, then they just exit automatically. Now that's a very quick tour off visual studio. We have, ah, lot off work and a lot of catching up to do. So I'm going to end this one here on. Then when we come back, we'll start writing or some C sharp programs.
6. Write a Simple Hello World Program: in this video, we're going to be creating our first console application. So let's get started right away. Bring up visual studio on, then go to create a new project, and then we can narrow it down to say console up. So maybe you did that already. Maybe you didn't. That's fine. But you can always just go to see sharp windows and insolent Consul in this 3rd 1 or you can just search console, and then it will narrate Don't for you. Now we're going to start off with that Dr Core console at this time, so I select console up dot net core fleet next, and then we name it differently this time. So we have a project names. I'm going to call this one Hello world. That's name off the project. But then I'm goingto have multiple projects underneath my solutions. I don't want my solution to be called Hello World on. Then I'm going to have multiple projects with other names, so naming is very important. It helps it identify exactly what you're seeing on. You know, it just makes life easier. Pretty much so. I'm going to call this entire solution console programming because once again. We're going to have this solution with a bunch of projects geared towards US learning consul programs. So the solution name once again is consul programming. But the project that we're creating right now is called Hello World. So I'm just going to click create on a low visual studio to do its thing. No one visual studio launches our project. They're going to see something very, very, very unique. You notice that it already launched our program that we just said We're going to write it Is printing hello world to the console? No, I just wanted Before we look at the court, I just want to point out that the solution is going toe our solution. Explorer is going to have a slightly different look. So if you're used to dot net framework, I know you're using dot net core than the files. The Leo to me look slightly different, but ultimately it's the same set off concepts. You have your program that CS file, and that is the one that comes open. When visual studio launches on. We see that we have a class called Program on Did it has a function called mean on DSO mean gets called once your application launches whatever is inside off army and function is what gets fired. All right, If you expand dependence is then you'll see they have dependencies, frameworks and then basically any libraries that you mean want. So sometimes you want to do a particular thing on Somebody has what they call a library. That donating already. You can actually include them and then use the library. But that's for later on. I'm just giving you an appreciation of what you're looking at, right? No. So no would need to see exactly what happened. So once again or cord is console dot right line on. Then we open the breeze and that we have a quotation marks. That's a hello world close quotation marks, flows. Brace on, then semi colon. So I'm going to explain what each token means, but I just want us to see how this works first. So once again to execute, I'm just going to go and click the start button. Or we can distress F five on our keyboard. We let it build. All right, on. Then we get our consoles are console once again looks something like command prompt. If you ever had to use command problems? You know that little black box where you have if the type and run command. So that's what the console program is. So we're seeing here. Microsoft Studio, Microsoft Visual Studio Sorry, Debo console on. Then we see that it's printing hello world, and then it gives us some data about where it's printing it from on. Did you know it will automatically close when debugging on press any key toe. Close this window so I can express enter on that window with disappear and we're right back where we started. Now let me take the time or to explain exactly what is being written here. Notice one. That visual studio kind of tries to give you color coding and see that it's quite colorfully have blue texture of white takes. You have I don't know what color that is any kind of have yellowish looking text, your code hinting, Czar, your color coding might be different based on the theme that you're using. So if you're in the light team or in the blue theme, then your color quoting may not be the same as I have it here, but just have an appreciation for what gets color coded. So we have name space name space refers to the pretty much the project and then by extension, the folder in the project. So if we had a fold in this project with a class file in there than the name space would be hello world dot that folder name. So program CS is directly inside off our project. So the name space is just Hello world. All right, we have a class. So our classes a collection off what we call properties on functions. But I'm not going to teach classes just yet, but our first and major classes class program, because that's the first thing that gets fired when a console up is going to run and then inside off program, we have static void mean on it takes some arcs, right? So I don't want to get it to watch detectable what the arcs means. But just know that if you're going to modify anything in this file, do not modify the class name. I do not modify the mean function. You can do any bit off changes you want inside off here inside off the main function, and we know where we're inside because you see the open brace on the clothes brace on notice. It's kind of hinting where the brace closes. It's trying to do all of that for you to help you not make certain mistakes or at least find them quickly. So the name space has an open brace, Onda close breeze and inside off those we have the class on that opens and closes on, then inside off the function. So this is a function of we know functions because they have the open breeze and the clothes brace inside off the main function. Well, you have curly braces, they have breeze are parentheses, so sometimes they use them to change that. I just realized that I use them slyly interchangeably. So we have the curly braces and then we have the regular braces are the parent disease. All right, so we know functions because they have parentheses and they may or may not have values inside off the open and close. So in this case, mean has values on. We don't have to trouble that on. Then, after declaring or function, we have open calibrates and close calibrates. Then we define what this function should do inside off those braces. In this case, we said, Do do a print to the console That's his hello world. So notice we have class on we're function Notice. The color coding program is a class on mean is a function. I'm pointing that out because if you look at the color coding, you do realize that console is a class. The world console is a class on the right line is a function, and we know functions because one the call according May look the same. And if it doesn't for you, that's fine. But our way to know a function in any C sharp chord that you're looking at is to see that it has Perrin to seize afterwards whether or not it has values in there. That's a different story, so I could actually say Bright line on then that would just be quiet. Okay, so right land is a function because it has the open and close parentheses on. If I wanted to define the text that I want written to the consoles and literally seeing this is the console, call a console on, right. The line that I'm putting inside off the open and close. Braces are parentheses. So then I'm going to put back hello world on that is whole or program team, All right. No, I want to annotate the scores because, you know, we're all starting c sharp. I want to look at it tomorrow and remember that. Oh, that's what was happening here. So I can just put in what we call a comment by using slash. Slash. All right on then. I can type anything I want someone to see printing toe consul screen. So that's our printed a console screen. You can use the slash slash. And if you wanted to write a paragraph, maybe you wanted toe be a bit more explicit. Hable what everything is. Then you can use the slash than an asterisk on. Then in between those you can have as many lines as you need. All right? Just writing something around other stuff on. Then you just make sure that you close it with the asterisk. Onda slash. All right, Now you notice that when when I'm showing you guys how to do things, I'll always open and close at the same time. And that's avoid making a mistake off forgetting toe, close it. So sometimes we get carried away on we start writing quote and we open the curly brace on right on, Right on. Right. And we don't close the current breeze on. Then, later on, we're getting errors that were wondering, why are we getting errors? Because everything looks OK. So I'm just going to show you what I mean by getting errors. And hope is your studio goes about showing you that you have errors. So if I forget to close a brace on, maybe I forget the part indices unnoticed. Every line ends a semi colon, but when we have multiple lines are pay more attention to that. So one you see that some lines start appearing. So when you have red lines in visual studio, that's never a good thing. They can appear because the syntax is incorrect. So the red line here is saying that Hey, you know something is wrong. I'm not entirely sure what, but something is around somewhere. I can always go to the era list, and then it will kind of tell me that there's an expected talk and somewhere so if I double click that it will kind of bring me toe the line that the arrow is that. But this is not quite where the arrow is. However, I can be bought this and say that. Okay, When I click this close one, it's much ing to disclose one which is inaccurate because I have a curly brace here. I know when I click this one, it's much into this. So that means a credit braces missing somewhere so I can just press enter on. Then I can put about the curly brace and notice. So everything realigned so visual studio will actually realize everything because then it realizes. OK, so you're closing this one? No. So let me just realign on, reinvent everything so you can see exactly where your court starts and stops. Indentation is very important. It makes there could look clean. Andi readable. No. Two more things Notice that we have a little asterisk beside the program name or the court finally and rather on that asterisk is just an indicator that this fire has been modified. So you can always just know which file has not been saved. When you look at the file, name the tub and you see that asters you can always save save all. If you have multiple files or if you've made modifications on you, click run. Then it will automatically save automatically. Re compile on, then allow you to proceed. So if I have some illegal characters here, all right? And we just looked at Eros. I see red lines. Maybe I thought I wrote quote properly, but visual studio sees it as illegal. Then it's going to one. Allow me to hold her over it and give me potential fixes. So I'm going to drop down this little This is the light bulb when it's going to suggest something some even know. The suggestions are usually like grasping at straws because their errors that visual studio knows about, like maybe you declared something improperly. Then he will say, Oh, I see that you declare this, but you didn't do this step. Do you want me to do it for you? Then you say Sure, and it does it for you. However, in this situation, this is random text. Visual studio doesn't know what to do. So no, none of these suggestions were really fix the problem. Another thing to note is that if I try toe execute where there are errors, it will do the build. And then you will tell me that there were errors. Do you want to run the last successful build? I tend to always click. No, because it doesn't make sense. I run the last successful bill. If I'm trying to build my current bit off court, so you click No on, then it will kind of show you with the European, telling you that you have some arrows. There's a cynical and expected all right, so at least it's amid a difficult to write code. Reacted properly. But then the ERA list is still telling me that there are things missing. There's something here that it cannot cope with. So if I remove this or I can just comment it out, make it you know, our let me use the other comment so I just come into throat. So that's one way to comment, and this is another way to comment on. Then Order's restored. And if you look down here, they're little visual cues along the way to tell you how many issues you have there. Tell me no issues phoned. I know that I fixed my earth. I'm just going to run my console application once more and then we see that we have our hello world printing to the screen once again. Second, press enter. Now, if I ever wanted to find this project will find the files. You know, I have the project of the solution of the project, but I don't want to open it in visual studio. Maybe I want to put it on a flash drive. I want to physically move it, auto some storage device so that I can use done another machine. Then we can easily just right click solution on. Then you see open file in file exports. When I do that, it launches my file Explorer showing me this file, and I'm going to see if I can zoom in a bit so you can see what the fast look like. Better. So then, this is the most timeless logo in the darknet community. This is the logo that represents a solution file. Once you double click the solution file, it will launch Reserve studio. Andi basically launched the projects that are inside off that solution. So that's one quick way to get back to your project that you're working on you. Just find the physical location. Wherever you stored it, you double click the solution file, and then it will launch visual studio with all off the cord that you've written notice. Hello. World is in its own folder on That's because it is a project. So each project will be get putting its own folder on all of them will be in one folder together with the name off the solution. So the solution was consul programming, it created a folder. Then he gave me a solution. File noticed. Extension is that s l N on then, for each project, it gives me a folder with that name. So, like I said, the naming is very important. So those names are money fasting outside, off visual studio on. Then if I look inside this folder, I want to see a bunch off Little little fast. But we'll get back to those fast on what they're really about. That's it for building a hello world console application and understanding visual studio bit more. Next we look at how we write a program to receive data from a user and print something back to the screen.
7. Input and Output Programs: in this video, we're going to be creating a project that will receive user input on print. Some output. I'm starting from our folder where our project was created. Because maybe you shut down the machine. You went to have dinner. It's been days since you were last in visual studio on. So I just want to show you how you can relaunch your project from the file system. Andi, you know not to have to go into visual studio and source it so I can just go back to the file system, find the solution and double click that will launch results to you, and that will relaunch our project or solution with our project and everything that we were doing. So every time you go ahead and you correct code and you save and you re launches solution, it may relaunch with all of the tabs EOD open or the court files and all the projects and everything that you have. Now let's go about putting a new project inside the solution so that we can test our right a new program. So this was our hello world program notice at a comment here at some static text are some random takes. I was giving problems that has come into the throat, but that's it for the hello world. So let's move on to a new one. Someone directly the solution. No, it's not the project. This is a project. I'm right. Click in the solution some, or notice that it's really just a conflict fire. So I'm right. Click in the solution on with the click. Add on, then I'm going to say new project. Then it brings about this menu that we should be familiar with this dialog box that we should be familiar with asking. Okay, what kind of project do you want this time? So I'm going to select once again, console up dot net core Click next on. Then This time I want the project name to be input Old puts input outputs. That's a spelling error, so you want to make sure that you're right. It properly right? Naming is important on. Then I click create on. Then I get a brand new court first noticed. It's kind of standard to come with Hello world Hello World is probably the first programming phrase that most people will ever learn when they're learning a new language hold to print Hello World to the screen. But that being said, I just want to city sometime. Moto appreciate what is happening here. So notice I have a new program that CS file. I still have my existing one on then. So it's not to meet the mistake, and it's assumed that they were both identical. They were both very knew Both of them had the same court in its How do I know which one belongs to which project. So each time ever console application are project. You're going to get the program dot CS file Andi, it's going to come with a mean function and let's just say it standard for it to say hello Were that these? We've seen that with the darknet core template, we can use this drop down list Look at which project it is in. So this program that ceasefire belongs to the hello world project. If I go to the next one, you see that it belongs to the input output project. If I close them both onda stores this file that's a project file on. I have no files open then from the solution Explorer, I can specifically go toe which project I want and then select the program that CS file. All right, so that's how you can manage multiple projects. Onda appreciate where what fire is coming from so you don't make mistakes goes well, you don't want to be writing, um, input all put stuff inside off. Hello World program dot CS on. Then when you're running, you're wondering why is there court not working? Or why isn't it detecting the code that you wrote? And that's just because he wrote it in the wrong program, that CS five So you want to be very careful and very attentive? Um, And as you build more and more applications, even more advanced applications, you're going to have multiple projects. They just want to be very careful and sure where each file is. Now, let's get started. I did say that we wanted to write something to get user input on, then print back to the screen. So let me just remove hello world. So we're going to be learning a few things here. We're going to be learning about one variables to hotel prompt for inputs. Receive it from the user on then three. How to print it back to the screen. So let's get started with variables, and I just put comment. Variables. A variable is like a little container are, and so when we want data from the user, we have to create a container on. Then we use that, contrary to store the value that the user gave us. So, for instance, if I wanted a name, then I need a variable on I'm It makes sense to just call it, Name the name off my county and therefore somebody's name is name aren't on then, Further to that, I need to define a type. So what type means the type off data that I'm expecting? So if I want somebody's name, then that is a text, because I will be writing letters, right? So anything that's letters is text type or a string. So in C sharp, we call text types. String when you want takes from somebody you need a pocket are that container, and it needs to be able to store what we call a string or text aren't so. Let's start off with whole we declare a variable. I will first define the data types O C. Sharp as a lot off data types, and it's good to know when to use which one. So I just give us a case study for the data type string. So I say, string on, then I give it the name. So I want some variable that is called Street now is called name Sorry on its is off data type string. You don't have already noticed that you have something called string on our eggs earlier. So that was the data type on our eggs would be your variable names. So in this case, we're making our own verbal telling it that Okay, declare a pocket in memory on call it name on it should be ableto store anything that is a text. So I want name. That's one. So I'm going to see be clear variables and then you can have multiple variables. You can have as many variables as you want, right? But then let's just keep it simple. So we want name on, then I'm going toe. Want to store what the user is entering into the console in my pocket called name. So I need to assign the data being collected from the console screen inside off need, so we never want to store data inside off the Skonto. You know, we say contain a name or variable name. So this is name is equal to on. Then if I'm receiving input, I'm going to call my console. So we get this class console. Remember that when we were printing to screen, we had console as a class name. So Consul, once again on then I said dot And then this is all call intelligence where it's telling me all off the potential things I can do with my consul class. So the thing I want to do this time last time we were writing lines. So if you scroll down, you see right line and you see right. But this time I want toe read line so you'll see that they have an option. Is's read lines. I can double click that, and then the red line is appearing because it's saying Okay, I see read line. But the context the sun talks is not correct, and that's because it's a function, so I need my open and close parentheses. Then I can use my semi colon. So I did say would pay attention to the semicolon. Every line that you write most end with a semi colon. There are exceptions, but for no. Let's just follow that rule. Every line that you write most have a semi colon. So we'll see the exceptions as we go along on relent. Really just a matter of practice to get comfortable with when to use your cynical and when and when not toe no. After we've gotten the name, then you want to print it back to the screen. So then I go to the next line. So this is Let me just put the comments as I go along getting value from input or let me see. Yeah, getting on, storing, getting on, storing value from inputs. All right, so that's what we're doing in this line. Then the next line is printing value, our values toe console screen. So we've gotten the value on we stored it. I know we want a printed box, so the cool thing is that I can store it, I can change the value, and I can always manipulate whatever is in here before I printed Buck. And that's the value off having the variable. So no, I want to print back to screen. And we did. We printed the screen already. We did print hello World, so we can just follow that same pattern on we say console docked. I remember the print was right line and see Intelligence is helping me write line, and then it's a function. So I need my R N theses and then their court hinting me and telling me that. OK, what do you need inside here is a string value. So we did put hello world inside quotation marks and that's what the string is its text. Like I said. So whenever one takes, we need to open and close quotation marks on that we can type anything we want any Alfa numeric sequence and the special characters wants. Once they're inside the quotation marks, C sharp will kind of say, Well, that is static. I'm not supposed to touch that because that is what my master being you wants me toe print . All right, so I'm going to remove this and I'm going to so I can put the sentence, you know, anything I want. We did say hello world last time, But what I want to print is not static I don't want to determine what I'm printing here because I'm receiving something from the user on. I want to print what the user gave me. So instead off using my quotation marks. And if finding you know, this is a low cost static, right? So it's ecstatic. It can't be changed against if I type hello world and hello, ER will be printed. If I type my name manually, then obviously I am bypassing whatever was inserted through the console because anybody can come and use this program. Put in that name on this will always just prints travel or Williams, regardless off what values inside name. So what we want to do is print the value inside off name to the screen on the when that is done is to actually put the variable names, so console that right line on. Then we put in the very bland notice. Once again, we need a string value, but we do have a string variable. So there two ways to get the string. We can use it a static meaning. Whatever happens here will never change until I come back in the court and change it, or I can use a variable off type string. All right, so I just declared a fair above type string. Received some string input from some user on. I'm going to print it back to the screen. So once again, every line interestingly, Colon, a C visual studio semi cynical and is expected. So I put that they're on. Then I just run no notice that the start still has the hello world. So if I run issue just going to print Hello World, it's just going to execute the Hello World Program. We have multiple projects and at this point I want to run input or put on not hello world. So what I need to do is change the start of project from Hello World to input output. So I'm going to right click input output on, then select the up the option that says said, start set as start a project. So I click that and they noticed that this there's no bold so before hell Oarlos board. No, this one is both. So that's a nice, quick way to know which one is the start of project on the notice that contextually the start button text also changed. So I can just click input output art. So my program, it's started and it's just going to sit there with a blank screen, just blinking and waiting. So what it is doing is waiting on the reader line. So it already declared name. You're not going to see a visual cue off the variable being declared You're going toe forever. Read line is just going to wait until they're some input, followed by an enter key. So if I put on let me put ah, fictitious near Archibald Gordon on, then I press enter. Then it executes. I see Archibald Gordon printing. But the screen No, this is not very intuitive because it was just blank. You're just waiting what exactly wasn't waiting for? So it's always good to give your user visual cue to say, Please enter your name or something like that, right? So what I'm going to do is above this line where I am receiving data, I'm going to put a console right land to print to screen the prompt. So we got this a prompt so console dot right line, open braces or open parentheses on. Then I want the static text that sees enter your name. So I'm saying, please, I can say please enter any make and whatever your prompt, it's so obviously, whenever you run a program or you use a program when you're feeling out a farm on the Internet, whatever it is you're doing on a computer, they always give you a visual cue to say, This is the date I'm expecting to you from you, right? No. So I'm seeing enter your name and then this line needs before the best in the colon on. Then I'm going toe wait for you to enter your name on. Then once you have done that, I'm going toe print your name back to the screen. So let's try that again. All right? So no, it says, enter your name because it ran that console right line. Enter your name. I know it's waiting for me to enter some value somatic and store it inside my variable on, then printed back to screen. So this time I'm going to use Kim early feed gun, and then I'm going to press enter on. Then you notice it printed Kimberly figure and back to screen. So no, it's still not very intuitive because I don't know which one is which. Like, Okay, I'm looking on the console as Kimberly Figure and I just see repeated. What? What is the repetition for? It's not very intuitive. They always want to put in visual cues, dear users so that they know exactly what they're looking at at each point. So I'm just going toe close this on sore on. Then what I'm going to do is Putin another console, right land or putting more text in this console right line to make sure that context only you know exactly what you're looking at. So what I'm going to do is add another console right line and then this time actually going to do console, right? So you have console dot I have right line, but you also have right? So I'm going to do our right and show you the difference in the right on the right line. So right is going to see your name is on, then semi colon on. Then we're writing once again to the console, the name that was entered. So let's try that again. All right? So, no, we say enter your name, so Okay. This time it is Paul Official and then we first enter. And then we see your name is Colon Paul Bishop. No notice that when we did console that right line, it automatically went to the next line and waited for text. When we did Hans older, right? It just did it in one line on. Then whatever else we wanted is still in that line. So that's the difference between right and right line. Right line Automatically. It's almost like you type of sentence in Microsoft Word and press. Enter and go to the next line. That's what right line does for you automatically just puts the next block off text in the next line automatically, however, right will not meet that break. It will. Just any other stream that is to be printed will just be printed in the same line onto that line. You know it's the text wraps, you know well, should know what word wrapping is so until the text wraps are on the screen, that's all right. Work. So that's the difference is you and console that right on the console. That right line. So that's it for input output. So let's just do a quick review. One we learnt about declaring variables. Once again, there are many data types that variables can have. We're just keeping it simple. We're just getting started. So I'm just using string where I collect some text and store inside some variable called name. Then I prompt the user for them to enter their name on then once they enter it. So this is how we get input from the console. We automatically assign it to our variable. Having gotten and stored that value from the user, we then proceed to say your name is so we're printing back some static sentence. That's giving them a visual cue as to what the data is that we're printing. So we're seeing your name is Colon. This text will never change on this. I changed in the program, but then we dynamically printed whatever name was entered because we're printing back the value in the variable. In the next lesson, we're going to go through some more data types in C sharp on. Look at the requirements between collecting data from the console for these different data types. Andi printing them back on what conversions need to be done.
8. C# Data Types and Conversion: Hey, guys, welcome back. So this time I'm going to open my project for using visual Studio's interface. So I opened visual studio and then to the left there, showing me all off the recent projects that I have done. So I'm just going to select this my project from the list. I can also pin it because maybe, you know, have a bunch of projects working on on. I want this one to be at the top always, so I can just spin it on. Then I can just go ahead and click it and open it on that visual studio will launch with my projects. So visual studio is putting us right back where we left off, which is when we were doing that input all put program. But this time we're going to be looking at some data types. So I'm going to create a new project so they're strictly the solution. Goto add on, Then select new project. And then when the console comes up or the dialog box comes up, I just select console up dot net core and then click next on. Then this time the project name that I want is variables and data, types of variables Andi data types and then I click create. No, I did say that seizure boasts a wide collection off data types. There are simple data types on there are user defined data types. So the basic ones that you have with all those a simple data types which are like the string and, you know, for numbers. And so now we're going to go through some off them. That being said, there are a lot. So I'm not going to go through every single one on git along the way we may end up using one Are few that we didn't cover in this video. So I'll go through the basic ones that you will definitely need to know. But as we go along, you see others that contextually you may need. I also want to point out that C sharp is what you call a strongly typed language. So that means that if you are going to declare a variable that should hold text, then it's going to treat it like text. So if I put one as in the numeral one inside off a text variable, it won't treat it like a number two do math with it. So you have to choose your data times very carefully based on what operation you want to carry out. So let's start off. So we did see string. So let me say variable declarations on the type someone to use this section Just define some variables. Onda We can look at what they are So one string. So we went through a string already String on. I went to see name then maybe you want to collect the age Also know Are we going to store somebody's age as string? Or are we going to store their age as maybe a number? Because if I tell you, I'm 10 years old and then you want to do some muff to say OK as a 10 year old, this is mathematically hole. You're going to be in X Y Z or something like that. You know, you may want to do a mathematical operation. You cannot use a string variable for months. So you want to use what you call an int so into short for integer on that one on indigenous basically a number that doesn't have any decimal places. So if you put a decimal number inside age like you say there, 22.5 years old because and it is going to ignore the 0.5. You do have other variables that that so I could probably make one. That's a salary. So I could say double celery, so double will take a decimal point. All right. You also have another data type called Float that does this more points. But then, based on the number off decimal places, you know, the that's where they're really different. The number of decimal places on the precision that is a load in simpler terms. You know, you may have like a volley like pie, which we all know as a decimal is 3.1 for something something, something, something something double on float me end up storing different values because one will be more precise than the other. You also have what you call a char. So char is short for character. Onda char bearable is usually used for a single not usually use. It is used sorry for a single character. So a case study Oh, our case for that would be like gender, right? So charge gender. You're either M or F another very popular basic data type. Um, that we would see is bullion. So we have bull, and then I'm going to use this one to say working Sobule means yes or no. It's either true or falls right. You see the one or zero. There is no other value that can go inside a bull. But true are falls, so let's recap real quickly. So we have string name, so string takes text. It takes anything that is, it can take any often numeric on character combination is just going toe. Receive something on, then print it back. You can't do any muff on the numbers you put in there. You have to do conversions on. While it's relatively easy to do conversions, I just recommend that you use the right variable when you need it for a certain thing. So if we have a string, it's clearly not to do any month. If we have an integer, it's because you want a number. Um, then we're not expecting decimal places. If we have a double, that means we can get a number and we might have decimal places. We have char because you're expecting one character, this one character can be off any perspiration. Meaning can be a letter, a number, a special character. But it's only going to be one, so you can put your name inside off a char. All right, So for this example, we're going to accept M or F for gender on. Then we have the bullion right, which can only be true or falls. Now, we just wrapped up a session about prompting users for so what I'm going to do is from them for their name, their age, their salary, their gender, Andi, if they're working or not. So I'm just going to do about two off them. And then I'm going to allow you to try and do the rest. So I'm going to say console not on. Remember, we have toe print to the screen. I Hey, I'm asking you for this data right here. Right? So I'm going to use right on. Then you will see the difference in the problems when I use right instead off right line. But then I'm prompting this time for names. I'm going to say enter your name Colon on. Then after that, I'm going to say name should get get the value from the consoles. The name is no equal to console that read line on open and close burn disease. That's the name prompt, right? No, To expedite this process, I can just copy if I want I can this copy Those two lines go down and pieced on. Then I just changed all the text, so I asked for name. This time I want age and then the pattern is the same. All I have to say is it is equal toe console that read line. No notice. I'm getting an error when I'm asking for the age. All right, and then that's where our conversions come into play. So I did say that this is variable data type and conversions, right? So in this situation, it is seeing that there's an error cannot implicitly convert type string to int. No, this didn't receive that era because console that read line is getting some keyboard input from the console, which it is transporting toe the variable in the form of a string. So in this situation, I did say that it strongly types they can put ah, word inside off are variable. That is expecting a number. So this number variable is expecting a number to be input, but the mechanism were using to get the data is transporting a string. So that's a problem, right? So, no, we have to run what we call a conversion where we accept the string converted to end on. But all off that before it gets into the variable. So what I'm going to do is serious this and I'm going to rewrite this line step by step. So C Sharp gives us tools to convert on literally. They give us a class called convert so we can write the word convert and then say dot on Then we have a number off conversions available to us. We can convert in 32 in the double, to the time, all sorts of things. So I want to end 32 on, Then I open. So this is a function. All right? I know. What do we know Functions? Because they have open and close parent disease. Then inside off this function, cause notice is still as a red line. It saying that it expects some value. All right, so, no, I need to tell it what exactly? I want to convert on What I want to convert is the input coming from the console. So I'm going to call the console that read line when it gets there is going to promise the user is going to pause, get the input when the user presses enter. So remember that this pauses for input until it sees enter. Once the person presses enter, it's then going toe, convert it toe into a 32 bit signed into Joe. All right, so just the stuff. And so they're seeing. Put it in an integer on. You did say it till you did see toe 32 in 32 2 in 16 and you may have seen on the numbers. The fact is, the larger the number is, the more bits required, right, so 32 is pretty much standard. We can leave it with 32 on, then, once it is converted than it is assigned to our age. Verbal, I noticed all is well in the universe. There are no more errors, so I'm going to do the wrist on. I'm not going to do any conversions just yet. I'm going to pause here and I'm with I love you to try and right the problems for each off the other three variables. And then we can look at the potential arrows together. All right, Siro, the rest of my problems and I change that takes accordingly. Interest, celery, enter your gender. And then for the one with working, I kind of changed the Burbage because once again, you want to tell the user what kind of beetle you're expecting from them. So you don't want to leave it up to them toe in for what they should be putting in. So I said, Are you working true or false? NC. I spent through wrongly, so true or false. So we're expecting them to enter the word through or the word falls. But more importantly, notice how many errors we have and the same reason that we had the era for ages. The same reason we're having these errors, because console that read line is turning a string. But the string cannot be put in tow. A double just like that. It can be put into our charges like that and it can be put in tow. Ah, bully. And just like that, and as I said char, I noticed that I did not explicitly tell them that I'm expecting M or F. So if a person enters the word male or the word female, then that will cause a problem. Because this is only expecting one character. One letter, 11 not a word, right? So I'm going to be a bit more explicit with this problem. I'm going to say M or if All right, so with all of that don't know, let's fix these converse on issues. So it's the same thing. We just have to say Convert dot to double on there. We have to double. And then what are we converting? We're converting console that read lines. So I opened the parentheses here. I need to make sure I close it. So this is a common aero Open. It may well get up and go and get coffee on. Come back on. Then we look at its one time, I would say, Oh, no, it's closed because there's a close one, but we're not paying attention, right? So it's always good the open and close in the same sweep, and then you can get distracted afterwards. That would spare a lot of heartache. So there we go. Double is, um sorry. Seller is expecting a double. So we're getting the input from the console, which is coming as text converting it to double. So whether or not it has decimal places, it will treat it like it does on. Then it will put it in our double. For char We have convert, not touch. Are so you have a convert for almost every single data type that is in C sharp. Right? So you have quite a few data types have quite a few ways to convert them. Andi, I hope I know your allies that I did see case matters when we're creating our projects. Well, in the language itself, case those matter. Because if I write convert as ah with a capital C that will never work because it doesn't know what convert the common see? Sorry. If Iraq convert with a common see, it doesn't know what that is, so it will only detect it when I adhere to the strict rules and use my upper case C for convert, not consul convert on. Then I said that and then I have toe bullion as an option and then I open parenthesis and then I make sure to close my parenthesis on. There we have our functions. Are our court our promise? Taking data and storing them into the different variables? No. Once I've done all of that, I want to print this person's profile back to the screen. So I'm going to do a little difference here with my printing to screen. And we're going to call this concatenation. So this is the process off joining strings together so earlier I would have had one console right that printed the static string and then another one to print the variable. This time I just want what, Right one console that right on. I'm printing both the static string. Seeing your name is Colon on. Then I'm putting the variable inside off that it gets printed automatically, right? So there are many ways to do it. I'm going to show you may be the easiest way right now. And as we go along, I'll probably do it the other way so that we can get up and running. Actually, I think we have enough data to prints toe Look at all off the concatenation techniques. I'm just going to go through all off them. So I'm just going toe right The 1st 1 Sorry. Toe print the name someone to see. Console dots, right, open parentheses. And I just my cynical in their same time, everybody codes differently. I just like doing it at the same time. And then I want to say your name is Colon, and then I can use a plus sign so we all know that the plus sign is used in math. Right? Well, when we're talking about strings and printing sentences are printing to the screen, we can use the plus sign to say, I want to add this block off text toe, whatever other block optics, I can have as many blocks off text as I want and close ends in between. All right, I can do that. But in this situation, what I want to add to this static block off text is the value that is inside names. Understand? To say your name is static text. Close name. So this is a concatenation. So this will print one line saying your name is on whatever value was collected from the user at the time off input. All right, so I can just duplicate this or will not let me write another one. So console not right on. Then. This time I want to print your age. So your each is colon on. Then the next way I can concoct in eat is to use like placeholders and kind off. Haven't hurry off variables Too much these placeholder. So I mean, let me just demonstrate here. So I'm going toe open car libraries use a placeholder on that zero and then close carnivorous on. Then what is going to say is I know I'm need to print your name is on. Then I am going to print this static text. But then this, as I said, is acting as a placeholder. So I need toe define something that should be put into this place holder. And what I do is after the block off text I press comma on. Then I can insert a variable. So the variable that I want to be inserted into the placeholder is age. All right on then I'm going to print the salary to the screen. So same treatments console Not right on. Then this time I'm going to do what we call interpolation. Right. So this one I'm going to start off with a dollar sign at the start of the string. So it's dollar sign open and close quotation marks on. Then I'm going to see dollars sign. Um, your salary is colon on. Then interpolation allows me to just use open and close braces and write the variable name right there inside off the static string. So it is salary. So I have one static string. I just have the make sure of the dollar cycles without the dollar sign. Then it will look more like the placeholder code, all right? And that would not be desirable. So with the dollar sign, instead of using a placeholder, I'm just opening the curly brace and putting the variable itself inside off my continuous string. Contiguous means continuous. Right? So all of the string is just one block Onda we have or variable being rendered inside off that. So the resumption in all of these I can guarantee you this one will work on this one will definitely work the based on the version off dotnet framework that you're using on that influences the version off c sharp that you are using. You may or may not have access toe the interpolation, right? So that's why I said, there are many ways to do it, and it's good to understand on appreciate hope. But I can guarantee you, if these two don't work, this one definitely will. And if this one works, this one definitely will. So you can pick your battle pick whichever one you like the most. Andi. Even more specifically, which of a one actually works in your context. So I'm going toe, proceed with the simple one that I know works universally on that. I'm just going to make sure that I print the other two variables. So this one sees your gender is on. Then I'm printing gender. And then the next one sees, Are you working? Are you employed? You know you are employed and we expect to see true or false so than working. All right. Now all of this is going to print out as one big block off text. It's going to look disgusting on the screen, but I'll show you why. So let's test it Also, my start project needs to be set variables and data types directly. Variables and data types set a start a project on then go ahead on and start. All right, So let's take or program for a test drive. So I'm going to see a test user is my name. I'm 22 years old. My salary is 2500.30. Press enter. My gender is m enter on dime. I walk working True, I'm working and when I press enter, you see that? Okay, we just get one long lying everything in one line. No spaces. It's very difficult to read And that's because we wrote with console that right and not console that right line. All right, so that's why we have to be very careful when we're using Console that rights and console that right line. So I'm just going to change this oats toe console that right line and I can just copy and peace Ondo the master placement on. Then let's just run again. Onda, see how the open looks. So we start off with our name on them with the statist female user. Um, her age is 23. Her salary is 5000 coin for it or five. Those end about 500. Um, gender is if on then, but then she's going to unemployed. So working is false on. Then. When we do that? No, we see the printer looking much better. We have a line Perbet off. Information noticed. We have our static text on. Then it is printing back. The value are so the conversion was almost it really only needed to be one way because notice that a console is printing back the values and we didn't have to convert from char to string Onda. We didn't have to do that. The concatenation onto the string, right? So it's it's strict in some aspects and it's flexible in some. So once again, when we're collecting the string from the console will have to convert it into whatever ate a type variable that is receiving the data is all right. So we have to do into conversion to store the int double to store the double, etcetera, etcetera. But then we were printing back to screen. We didn't have to convert from that data type to string to get it well attached to our string. Right, So everything when it's printing out, it will just context only say okay. Yes, I know this is a bullion, but let me just printed
9. Operators in C#: in this video, we're going to be looking at the different operators that are available to us in C. Sharp. No, I already went ahead and created the project on Guy called it C. Sharp Operators once again try to avoid any special characters when naming your projects on solutions on DE. So I just used the word sharp instead off the the special character, which looks like a number sign, right? So C sharp operators and I already took the liberty off kind of writing in some things. In the main functions, they can pause the video, try and create the project yourself and putting these comments, and then you can resume when you are done. No operators are symbols that are used to perform with literal operations. All right, so in any programming language you have access to these operators. When you want to do things that math on, do you have other categories? Were their lawyer to do things that comparisons? But for this introduction, tow operators, they're just going to be dealing with the ones that are probably more mathematical on. We need to know if nothing else. So let's start off with the basic assignment operator. So we have used this already in previous examples, and that is used when we want to give some variable value. So if I have a variable, it's into Gnome, then I want gnome toe have the value off five. Then I want to say Numb, You are assigned the value five. So that's really what that is. It's really on assignment. So one equal sign myself is really seen as an assignment. All right, so we're putting the value of five inside off our beer. We're assigning the value of five toe are variable gnome. All right, so that's the basic on. Very easy one. And we can always just write a console dots right line just to prove that it works. So I'm just going to see a sane assigned value to variable. All right, on, then I'm going to use my interpolation, so we should know how to concoct in its string already. So since I have this one available to me, this is the one that you're going to see muse more frequently than not, um, otherwise, use the one that's your language or your dotnet framework supports. So once again, we use this a sign of value toe. A variably can be multiple variables with multiple assignments. It can be a static value, so static means that I have corded it in meaning, I put five. So this will never change unless I come back to this screen on change the stakes from 5 to 10. That's the only way this will change. But then that's what makes it static, whereas when we're assigning values from the user input, it makes it more dynamic on. But the principle off assigning doesn't change. All right? No, let's move on to the arithmetic operator. So for arithmetic, operators there usually used to perform, um, mathematical operations. So I'm going to define two variables right off into X, and then I'm going to show you that you can actually do a declaration on assignment in one lines. I can say in X is equal to five. Andi in twi Easy quarter 10 right there. So instead of splitting it in two lines, I could just see into whatever variable is equal to whatever value I want on. Do that in one line. So that's another way you can do your assignment right? But for this section, I want us to do some months. So I'm going to go through different examples and we're going to print the screen. The different all comes off the mosque that we're doing with these. So I went to say console, not console dots. Right line is equal to Andi for each line. I'm going toe inter. Use my interpolations, so I'm going to see a dollar sign open and close quotation marks on. Then I'm going to start off with Edition, but we're going to go through the different examples addition on then I am showing the result off. On addition operation between X and Y. Soto. Add X and Y. We're literally going to use the plus sign. So remember the plus sank abuse for concatenation reasons also, but it's a form off what we call polymorphism, where the plus sign has multiple purposes. The plus sign is seeing here that I'm adding two numbers. So do some math, whereas in the context off a string, when you see block off string, close, string variable, it's seeing I'm adding to string. So technically it's the same addition. But the outcome is going to be different because we don't expect to see 510 At the end of this operation, we expect to see 15 Where whereas if there were strings than you see 510 So it lower about it. Just know that this is arithmetic on. This is how we're adding two numbers. So that's all we add on. I'm just going to duplicate this land because I want to show a subtraction, multiplication and division on. Then this one called models, right? So I'm just going to duplicate this console that right land by holding down control and pressing d. That's a nice, quick way to duplicate your lines in visual studio. And then I'm just going to change the So So this is subtraction on, then. Subtraction. It's the minor side. All right, so where I didn't explosive y on, they were subtracting X minus y. So anybody who understands beating basic mathematics, this should not be toe a nought off, please, for you to understand. So multiplication. We use the asterisk. We don't use the X. You know, when we were writing it on paper, we use expert. As you can see, X is going to be seeing us X. So if you want the operator that handles multiplication. We need to use the asterisk or star all right before doing division, then, um well, most if not all keyboards. And what's just going to go out on a limb and say no keyboard? Has the division signed the way we write it on people. So for division I have to use your slash. So the same slash used for the comments is the same slash that you use for a division. But obviously you're using one off them. Because if I used to, then that's going to start some commenting, right, So one slash that is division. So this is saying that x r five is going to be divided by why are 10 All right, We do have model ISS, and then modelos is the percentage side, so this is basically a division, but then this model is going to return the remainder. Right? So, for instance, if I said food divided by one are you know what, let me actually change told, so I can one with printed oh, to screen you see exactly what I'm saying. So let's say this is three on this is to all right. So when we say three divided by two. We're going to get back one point something, right? That's the 1.5. I will be the decimal, right? But then, if I said modelos, it's going to return one because two goes into 31 time with a remainder off one. All right, so that's the difference between the division and the model is the model s returns to remain there off the division. Operation on division will just tries to divide someone to pause right here. And they were going toe or on this operation and see exactly what's happening on. So we usually go over here in. Our solution is for actual core project insists it a start a project. But you've probably figured out the shorter way to do that by no on that is by selecting from this dropped only. So we're right beside the start button. There's a drop down list with all the projects and can just see which one you want, and then go ahead and press start. All right, so on screen, we're seeing the results off our arithmetic operations. So we are signed. The value that's the 1st 1 on then addition is five goes. Three plus two is five. Subtraction is one, multiplication is six, division is one and modelos is one. So because they're both returning one, you're not really seeing the po off modelos. So I'm going to change this on. I'm going to use I want to switch these numbers, our own someone to use three on. I'm going to use a value like it. So at least you know that when we hit the modelos won't be seeing the same for the division and the same for the models. But actually, you will be sorry. Thomas is my must sleep. Now let me try 11. So 11 divided by three is going to be three times. But then the Mahdi Loss is expected to return to because that's remained from this division operation. So let's try that again. All right, so here we go. So we're getting different values because, well, once again, it's X and y, and whatever we put in as vice off X and Y will influence the math being done. So three plus 11 is 14 3 from 11. That is it. Or if I'm to read it exactly how we wrote it, that 11 miners. Three leaves it multiplication. That is, 11 times three division that is 11. Divided by three is three. So it goes into it three times a notices returning just an int. All right on then I say, give me the model s So it's returning home any you know, the remainder off the division operation off 11 on day three. No, right? No, we're adding two variables. What if we wanted to add a variable toe the value? It's hard in itself as well as another value. So for more context, I wanted to add whatever X is on. Add four more to it. So whatever value X has, I want to add four to it. And that should be the new value off X. So that means I would have to do something like X is no assigned. The value or X is equal toe itself to Los four. Right? So I'm doing this after this operation. So 11 3 those are the values on. And then the matter will be done with the virus off 11 and three. But then I want to change the value of X to be four more than it was when the it was first initialized, right? So, no, I'm adding for whatever value X had because X is a very but I don't know what it has at any point, so I have to retain it in the math. Andi. Then, after that math is done, I assign it to itself so that with that done, I'm just going to use this console right line, and then I'm going to see a new value off X. And then I'm just printing X to the screen. And then, just to prove that, that gave it a new value. I'm going toe print. Reprint All of these addition operations are sorry all of these arithmetic operations using X on, then the expectation is that the values that the initial operations would have yielded should be different from these values after we've changed the value off X. So let's try that. So now we can take a look and we see that Okay, all of these values were the same as we saw last time, but then the new value off exes 15 because, like I said, we added four to whatever value X had on. Then it's will work or different math because the value of X is different. I noticed the division is No. Five because three goes perfectly into 50 and five times on leaves. Nora meanders. No operations. I decided on all the time. Most. You know, you don't always know what values there, so you just retain the variable and do whatever addition or subtraction. Multiplication. It can be done with any other operators. If I wanted to multiply the value of X by for that, and you'll see X is equal to it's of times for And then that's its new value. No, because off that function or the need to do that kind off function, we have what we call compound assignment. So instead of having to type of X is equal Toe X plus four, then we can actually do something like X Close equals five. So I'm just going toe aren't sorry. Four, since we're using for that me use for all the time. So I'm just going to copy this down here and show you that these two lines are seeing the same thing. X Plus Equal four is the same thing as saying X is equal to X plus four on the same applies for any other operation that will be carrying out. So if I wanted to say X should be equal toe X times five. All right, so ex His new value should be itself. Whatever value does at the time, Times five, whatever this year's should be assigned to X, that's the same thing as seeing another and longer we in comments and surely the shorter way X star equals five. All right, So any time we want to do that kind off operation where we're assigning the variable a new value based on what its value is already, then we can write it. Either way, we can write it as X is equal to X Times five, or we can see the variable, which is X in this case, that operator equal. I notice there's no space. There's no gap here. If you put that space there, you might get an arrow. So it is operator assignment operator, and then the value on then that will imply to see sharp that you're doing this operation. All right, so I just wrote out some examples off assignment operators. You can take some time and write these off, but what I'll be doing here is changing the value of X with each operation just showing you that whatever the operation is with the arithmetic, um, symbols, it can be used for the component assignment on as long as you just have that operator and then the equal sign. So that strength again. All right, so we have our first block where we did or assignment. It is the value was 11 on. Then we did the operations 11 and the three, I believe. Yeah, on Then we did the operations with the three and 11 on. Then we change the value off the X variable to 15 and they would in new operations on. By the time we got down there, the value was still 15 because we didn't change the value again until I said, explosive equals five. So 15 plus five is 20. Then I said, X minus equals three, which means whatever value I have, No, which is 20. I want to be equal toe that value minus three. So by variables, value got set no to 17 because it was 20 minus tree on. That's a new value of X. Then we go on to say whatever the value of X is, I want to multiply by two. So I'm seeing 17 times two is No. 34. Then I say, OK, whatever valid that has make it make ECs no equal toe. That divided by three on 34 divided by three is pretty much 11 on. Then we went on to see Give me the model is off three in tow, 11 on, then that gave us the two. So that is so We handle component assignments on arithmetic operations. So, like I said, these are not all off the operators that are available to us in C sharp on by extent any other, uh, programming language. But this is a good place to start and as we go along and look at if statements and four losers start seeing other operators used for other things.
10. Condition Statements: in this lesson, we will be looking at condition statements in C sharp. But I know I'm sure we all have the hang off creating new projects. So I have already created a new project and I'm calling this one condition. Statements on Die already cleaned out the main function to put in some comments so you can pause a video here, go ahead and create your new project inside your solution on. Once again, it is called condition statements. Now a little pre load. Before I get into what condition statements really mean? I want us toe examine something about programming in general. Most, if not all. While every programming language has three control structures, they are sequence decisions or selection on repetition sequence basically refers to the fact that the program is going to execute the commands in the exact order that you placed them. So in previous examples where we printed a prompt and then we waited for input, and then we prompted again notice that it went in the exact sequence that we wrote it so that this sequence, however, when we get to a roadblock on, we need to maybe make a decision to say If something meets a certain criteria than take this action or taken other action, then that is another control structure called selection or decisions. No. In this lesson, we'll be exploring to condition statements that help us to make selections. They are. If statements on switch statements on, then we're going to look at Turnberry statements as a bonus. Now let's get started looking at if statements, a typical reason why we would need to use an if statement is to determine something or some action based on some condition. So let us say that I wanted to print something to the screen If something was the way that I think it may or may not be right. So let us say, I wanted to say, If on I'm going to keep it very simple on I'm going to say something like if two is less than three, so notices a new operator. We did arithmetic operators earlier. No, we'll have the comparison operators. Where we're comparing is to greater than or less than this is less than three. All right, so too is less than three. If two is less than three, then I want to do something. Please note the syntax behind this statement. The keyword is if then I open. Parenthesis is. Then I placed the condition inside off the open parenthesis, and then I closed. So inside off the parent disease, I should have a condition is something maybe equivalent to something is the less than Is it greater than there's so many ways? We can compare things, but I'm just checking. If two is less than three, then I want to print something to the screen so I can say console dot right line and I'm going to see Yes, it is. All right. So then I can see if two is less than three, then print. Yes, it is. What if there was another condition or if I didn't? If that wasn't the condition and I wanted to take another action, so if it is, then prayed print. Yes, it is. And then I can see else. And then after else I need my curly braces again. And then I'm going to say what? What would I do if this condition is not met? Someone to take this console that right line and duplicated just to expedite this and I'm going to say no it is not so. Once again, I am seeing if two is less than three, then print. Yes, it is else print. No, it's not. Now you notice that this line kind of looks a big gray daughter, kind off dull on diesel studio is basically just telling me that based on the condition that I specified here, I would never, ever get to the else. Which is true, because every time I'm going to run that unless I change these values, you will always be less entry. So yes, it is will always get printed. The else would never get printed. But I'm still going to leave the else because I'm going to show you different conditions that can go in here on hold. The court would be executed, so let's execute this and see the end results. Once again, it's going to run in the sequence, and it's going to say, if who is less than three, what should I do? So let's check that out. So remember that you can change your starts or project using this drop down list. Just make sure that it's on condition statements on go ahead and treat. Start on the end result is that it prints. Yes, it is. And that's because the condition was true. No, if I change these values to something that well, I want to say No, it's not. That I can say is 10 less than maybe three are leave the three. So if 10 is less than three, then we want to print. Yes, it is else print. No, it's not now, just looking at it. We know that 10 is not less than three, so we expect that this condition will not evaluate to true those who won't do what is in the first block. But it will do what's in the else block. So else is basically the falls for any condition that is outlined inside off the If so, let's try that again. I'm went toe run the program, and the expectation is that it will say no. It's not because 10 is not less than three. And there we go. No, it's not all right. So that's a very, very simple example. Let's put a bit more meat on it, so I'm going to find some variables. I'm going to define number one and number two so int num one and please note with variables . I may not have mentioned this before, but when you're using variables or when they're declaring variables, you want to avoid spaces in the names and you want to avoid special characters. Almost the same rules is when I suggested hole. You name your projects. You can camel case the names if you have multiple words some people use underscores. But ultimately just avoid any ah, space and any special character. All right, so no one is going to be equal to two Onda. No, um, two is equal to five on DSI. In doing that, I violated my own rules and visual studio is telling me that I'm wrong. So int num two is equal to five. And before I used these two variables in the example, I just want to take this time to list all the other comparison operators that you have to your advantage. So you have on I'm just putting the breeze there so you can see them. So you have the less than you have greater than you have equivalents. So notice to equal science or the equal sign by itself is an assignment operator warier, assigning a value from the right toe, the variable on the left. However, if you want to compare if two things are equal to each other, then you have the equal equal for it. So that's the distinction between a single equal sign on a double equal sides. So we have equal equal. We have hybrid ones, like greater than and equal or less than and equal on. Then you have one that is not equal, which is an exclamations and on an equal sign. All right, so those are operators that you can use inside off your if statements, when you want to compare the value on the left with the value on the right. Okay, so don't we have no one and number two? We did this with static values, but we can also write an if statement with variables. All right. So I could be comparing numb one with the number three, so I can see if no one is less than three. Then I want to take this action. Otherwise I want to take that action. All right, so in this situation, I have no more declared toe, have a value off too. So if I execute this than two is going to be less than three. However, if I said no, I'm five here based on the value off norm five. Then known five or sorry, numb to Sorry. Numb to has the value of five my body. So if I say no to is less than three as long as long to retains the value off five, then we'll know that it is not less than three on, so it will go to the else block and print. No, it's not okay, so you can use variables to compare with static values. And you can also compare two variables. No. One on number two. All right, so here's what I'm going to do. We're going to write a full program where we're accepting data from a user. And then we were on an if statement to know what to print, all based on the input from the user. So I'm going to write a program that's going to ask our users to tell us how many apples they have and home any oranges there. Someone to console? Not right. The first problem to say. How many apples do you have on? Then we'll do a console dot reid line where we get the input. But then off course, we can disconsolate re line This value needs to go into a variable someone to use no one to store the number off apples. So no more uneasy quota console that read line and then we know that no one is an integer. So if we're putting something into the integer from the console input, we need to convert it. Someone to say, convert docked to eat 32 on, then open foreign assists on close parenthesis around that on. Then I'm just going toe copy this line instead of retyping it from scratch. And then I'm going to say, How many oranges do you have on? We're storing oranges in numb to. So no, I have user input for normal one and user input for Norm toe, which of course, will override the values that they were initialized with because I'm not going to trouble the initialization here. Or at least I would always recommend that if you're initializing, you always initialized toe a safe enough value. That's you know it doesn't tamper with your operation going forward. So we're initializing number 120 Onda numb to zero on, then we're prompting for the number off apples and storing that in Norm one on. Then I'm prompting for the number off oranges and stirring that in numb to no, my statement is seeing if numb to is less than No. One, that I want to take a particular action. But then I feel probably more comfortable saying, If numb to is greater than No. One, it really doesn't matter. So, based on your context and based on how you probably would want toe, if you can see it in English or say to yourself, how would I compare these two? Then you can just translate it into court using the appropriate operator. All right, so I'm going to say, If no one is greater than numb to, then that means that I would have more oranges so numb to represents oranges. So if numb to is greater than No. One, that I have more oranges than I have apples, so then my consular line is going to be you have more oranges on then, if I if that is false, meaning numb to is not greater than no more that I went to print. You have more apples Now let's the quick test and see the oh, come off that. So how many apples do you have? I'm going to say I have to. Apple's Andi. I have five oranges, so the expectation is that it prints you have more oranges. All right, let's do another test where I flip the script. This time I have 10 apples and I have five oranges. And so when that evaluates in my statement, I should be getting. You have more apples. There we go. So that's basically what the selection does. It looks at a condition, and then it makes a decision based on the all come whether it's true or falls for that condition. No, the if statement can be expanded a bit more on. I'm just going to do this and then we leave if statements for no, because it's statements are very useful on. As we go along, we will be using them and see more purposes. But I just want to stop an appreciation for how they work and why we will be meeting them going forward. But the statement can also have more than one or more than two blocks. So right now we're just can't. If Condi son on, then else. But then you do have the facility toe have multiple conditions in one big if statement, so I can say if numb to is greater than no more than print. You have more oranges than I can say else if and then state another condition. So I'm going to say if number one is greater than numb to no one, be apples on down to being oranges. So first I'm taking to have more oranges than apples that I can say otherwise. If there are more apples, an oranges that I want to print, you have more apples. What if I wanted to add on another statement? Another conditioners it else. If on instant another condition on, then for that part, I would want to see if they are equivalent. So I can say if no more is equivalent to Nome to then open briefs. Console that right line and then I'm going to see you have apples or let me see the seam. You have the same number off Apple's Onda oranges. No, we have one if statement doing three different conditions. All right, So have, if numb to is greater than no more than bring something else. If no one is greater than known to then print something else if no one is equivalent to Nome to so there are plenty off ways you could have written this. But then I mean, there are so many ways heretics I can never show every single scenario wants You appreciate the concept than you can be more flexible. So in this situation, once again, I'm just checking. If numb to is greater than no one that I want to print, you have more oranges. And then if that doesn't value it true, then I moved on to the next statement to see console Redland. You have more apples, right? And if that evaluates the falls, any moves on to the next condition where it is okay, if their equivalent, then print this statement. No, there are situations where none off these would ever much. And then that's why you have the else. So that else is like a catch. All All right, so if we haven't else, you usually use that to say Well, when every other condition feels then take some action and this is once again is a catch off. So I'm going to say, um, final oxen or will invalid parameters? No. Based on this example, I don't know what I could possibly put in that none off these would get met on further to that, the comparison for greater than and equal toe. Unless that an equal toe would not fit into this scenario on Well, we always expect it. If one of these would be met, then clearly they're not equal, right? But then the case study or the use cases for these rather would be like if you're comparing greens, right? So if the greens start at maybe on a starts at 90 then you can see if you have agreed that is greater than or equal to 90 then you haven't e If they failing, failing greed is 59 then you can see if you have agreed that is less than or equal to 59. Then you're a feeling student, so you could have done the greater than an equal toe for that A. Or I could have no said if you're great is greater than 89 because then we know that anything greater than Internet is probably 90. But then, based on the scenario once again, you may or may not need to use it or you can use multiple accomplish the same thing and the whole let's run a test on this. So I'm going to put in some scenarios where I'm going to put in some values and then we should put in values that will invoke each statement as we go along. Once again, the else statement may never get called because one off these conditions will be met anyway , So let's tryto so firstly, we want a bigger number for numb to then for someone. So we want more oranges than apples. So I have five apples and then I have 10 oranges so numb to is greater than no one. So you have more oranges. All right, let me run this stem and this program again. This time I have more apples than oranges have 10 apples and I only have five oranges. Then it is you have more apple. So this condition for no one being greater than normal numb to being grated on, um one feel so it went on to the else if on then this condition was met so it printed that statement no if I have the same number between the tool fruit types. So that means I have 10 oranges and 10 apples when I press enter. Then it went to the else. If they are equivalent, then you have the same number off apples and oranges. All right, so there some rules surrounding how if statements should look, notice the special tokens that I've been using along the way, the keywords, if on then the parentheses on, then the condition inside the parentheses on they noticed no semicolon at the end off this line on. Then I opened the car libraries and then the c sharp syntax as we've come to know it with all the council right console, read line, whatever it is our doing that those can proceed. But then only between the braces. So I can't put any chord in this line. I can't be. Console that right line in between the if block just like that, right? That is illegal. So when whatever operation it is, you need to carry out a notice, have that red line. So it's in that it's expecting it's incorrect. Sin toxic for everyone. Tell you details about the incorrect syntax but just follow these rules and you should feel comfortable when writing if statements. So once again you have a condition. You haven't the statement. Sorry. You create the condition inside off the part indices, and then you open the curly brace and then you proceed to write all your court inside off your curly braces. Whatever erect inside of the curly braces is what gets done when that particular condition is met. So if numb to is greater than someone, then I want to take this set off actions else. If this other condition is met meaning this one field But then this one gets met, then I want to carry out these actions on. Then I have another else. If if this condition is met, then I have my specific actions. And then my else is the catch. Our meaning that none off these came to pass one of them much. My requirements. So else would be that the catch also, you know, based on the situation, then it may be good. It may not be, but so in this situation, if ever got to the else I know that that's bad because that's not what I expect However, in our first example, you saw that I only had if condition on then else. So when it's a simple condition words Eva or it's it's either this condition is true or it's not true that you can just use an if else. If you have five different scenarios that you need to test or in this case, three different scenarios in to test, then you can write if else if else If And if you know that they're the only three, then you really don't even need the else so it can work without the else in this scenario. Like I said, this is an introduction. So we're just scratching the surface with, if statements. But I'm going to move on to switch statements which help us to make selections. But just do it a bit differently. So the statement gives us the ability to make more complex decisions where we can say if something is greater than we can compare to values or some variable to some other value, whatever it is you're comparing, and then make an action to switch statement over. We have to look at a variable on, then make a decision based on just the value off the variable. So I'm going to give you an example. So the switch statements look something like switch on. Then it takes a variable inside of the parentheses. So what I'm going to do is define another variable. And I'm going to call this one Norm three. So into numb three. Andi, it's going to be equal to zero. All right, so I'm going to say switch numb three, which means assess the value inside off numb three. Then I'm going to see he's on. Then I can give our values. So case one, then colon. So please know the syntax, cause this is very important that you follow the syntax properly, So you say switch open breeze or open parentheses. Put a variable in there on. Then you open your current a breeze and then you make a case is a case, then the value that you want toe assess this variable four. So I'm seeing if the value off numb three is one that I say Colon welcomes after the colon is the action I intend to take so that I went to see console that right line on Let's keep it simple and just say value is one. All right on then. After I say that, then I need a key word so I can list as many actions underneath this. So what comes after the case will be just let welcomes inside the curly braces for the if statement. But then I have to end the case with a keyword. That's his break. All right, so I can see kiss one consort Land value is once. I'm just want to copy this. I can re type it. Then what if I wanted to do something else if the value was too? So I have case to Colon. Then I stayed my action someone to console the right line on. I'm going to change from values one and say something like, I have to know. All right, so I have to know if the values to it will print. I have to know to the screen on. Then, once again, I can print as many things as I want our take as many other actions I need to someone to print this three times or three more times. So on. This is just for example, I'm just showing you that you can have multiple lines coming after me after the case on the value and the colon. And then when you're done, you just make sure you put your keyword. That's his break. If you don't put the brake keyword and let's see, I forgot the break keyword. Then what would happen is that it will just run on so you can have as many cases as you feel the need for right. So as many situations or cases that you think are values that you want to compare this variable against you can have case on whatever that value. Since we're using into Joe, we just put the value of inter. If this was a string, then I would be seeing keys on, then putting in the string, all right, But then we have to keep it types sensitive. So if it's on integer variable that we're assessing that the case value must be an integer . If it's a double, then well, it has been numeric. If it's into jar double, if it's a string that it must be a string, but then otherwise we have similar to the else. In the if statement, we have a a default situation that says Well, if none off the case is much, then I am going to do something. So I'm going toe inside off the default, this console right line on, say, invalid value. All right, So I'm only comparing for one on to, which means that if if number three is never one and nothing, three is never too, then I want the default to come into play and see invalid value on. Then, once again, I break. Once I've done that. Everything is inside off that curly brace. So I'm just going to put in a prompt for a number three at the same time. So after we do with our apples and oranges, I'm going to see how many eyes do you have. That's a good one, that right? It's either have one eye or two eyes, but then I'm just going to say invalid value. If you put any other number, that's not one or two, right? So how many eyes do you have on? Then? I'm going to console that right? Right Read line. Sorry. In tow. Numb three. So let's test that one. Also, I don't want to have to do all of this against I'm just going to comment, wrote my if statement. So the charcoal to comment is control key and see right So holding down after highlighting the lines at the Salon Control Press K prissy on. Then all of those lines that were highlighted got committed. So know when I execute my program is going to ignore the commented lines and just go straight to asking me how many eyes do I have on then taking an action based on our values ? So I have one I. Then I went to say value is one all right, Maybe I can make it a bit more intuitive, so I'm goingto change it on DSI. You have one eye, all right. This is just for educational purposes is not really necessary. So if you don't feel like changing it, then that's fine. But you have one eye on that. I went to try it again, and then I'm going to put in the value tool. So with two, we expect to have four statements being printed and I have to know is printed four times. So once again, you can have as many lines as required underneath the case. In between the case definition on the key word break. Everything in there will get executed. So let's do another one, and we will put in the value off three. So three, since there is no case defined for a value of three will go to the default until us in valid values. So that's basically whole switch statements work. Ah, good use case for suits them. It would be like in a menu sitting where you're expecting certain values. So if you say, like, press one toe, enter grade press tool to have you agreed press three to exit program, then you could just say, um, get the user's input into the variable on. Then you're on a switch against that value on then, based on which case was chosen, are which value was selected. You're on a case for that. You take the actions accordingly on, then, if they enter of many option off five, when you will have three many options, then you have the default. All right? No, As a bonus, I'm going to show you turn Eri statements and I left this one for last because this one can be a bit difficult to grasp on. They are always our own using it, but it can be a bit difficult, but it's very, very convenient once you understand how it works. So I'm going to define another variable, and I'm going to call this one results. So this one is going to be string on. I'm calling this result. All right. Know the purpose of the Turner operators to run a quick comparison and it allows you to decide bright there on the spot what value? To assign toe a variable when that condition is met or not met. So in other words, with our if statement, if I said numb to greater than no one, I could have done an assignment to a variable in here. But then I would have to say Gnome two is equal to no, no more and greater than them to undergo on another assignment here on then on this one after the another assignment, whereas in a Turner operator, I can see result is equal to on. Then what I want to compare is numb to being greater than no one. All right, so this is my condition on. Then I can use Ah, call on on then. I see. And I think usually this needs to go in Cyprus and disease. So is this puts it in parentheses. So then I say, this is my condition on, then. Question Mark. Sorry, question, Mark. And then I'm going to see no more in greater than numb to greater than no more. Then I'm going to see what? What did we assign? More oranges. You have more oranges when that is so you have more oranges and then colon on. Then we're going to say you have more apples. All right, so let me just start over. A lot just happened. So we have a variable, and then I'm seeing what the contents off this variable depend on the old Come off this comparison. So my variable result is going to be assigned some value based on the results off this comparison, which is saying is known to greater than number one. Then I have the question mark to see if this is true. So all of this is saying, if this comparison is true, then I'm assigning that value toe be inside off results, and then the colon says otherwise it is this value. So the quick thing, the good thing about this is that in one line, I just run a mini if else statement. All right. The downside is that that's all we can do. I can't have 1/3 scenario to say if if the greater than are their equivalent. Sorry, I can't have authored scenario, so I just want on one condition and then I say, based on the outcome, if it's true, then I want the 1st 1 to be done. If it's false and I want the last one to be done, not once again, this comes in handy a lot of the times, but the if statements are very easy to do. Such statements are very quick also. But then this. If you understand the concept, you can use this at any point and expedite completing your program that you are offering. So I'm going to leave this program went toe un commend all of these lines so that if you get your hands on the source score, then you will have all of these examples for you and toe on comment. That's control K you. So let me just show you that. So if I am an online and I want to come in to take under state control. Casey on, Then it commence it on, then if I want to on comment it, I can just say control K U. And there you go. So you press k and your can see while holding Don't Control are so that's it for condition statements, that's a brush over. I hope you understand the concepts if you don't feel free to reach out to me. But as you go along, we're going to be using their more and more. So you will get to appreciate riel scenarios where you will need to make selections and decisions based on values that are coming into your system.
11. Repetition Statements: in this video will be looking at repetition statements in C sharp. I have already created the project on this time. The project's name is repetition statements. Andi, I have already set up the commenced the guide what we will be looking at. You may posit video here and go ahead and create the project and set up your court file to the clamp mine. No, we've already reviewed to control structures. We looked at sequence where that is the order in which the court will execute. So from top to bottom, the way you're at, it's the way it will be executed. Then we looked at selection where we get toe a situation where we need to compare to values and then take on action based on the all come off that comparison on that selection more making a decision. So, no, we're going to look at how we repeat an action without having to type the line more than once. So, in our previous example, where we were looking at selection and I'm just going to go over to my condition statements project and click on that program dot CS, we printed the same line four times. We were literally wrote this line four times. Now there is a way that I can repeat this action without needing to repeat the line, because no, if I wanted to change the sentence, then I have to manually go in and change it for different times. Whereas if I could repeat printing the sentence four times without changing the sentence or rather repeated four times, But then I need to change the sentence than I would only need to change the sentence once, but it would still go four times. So that's the advantage that loops bring to us the ability to repeat on action. So you see that I have outlined three types off roofs that will be looking at today. There there is 1/4 and I think there's also 1/5. But then, for no, I'm just looking at these three because these three are means days and they're in every single language that you'll ever do on the other one kind off as special applications, which will be looking at later on. So the four loop is what we call a counter control loop, so counter controlled meaning I am sitting the number off times that I want this repetition four. So if I wanted something to happen for five times, then I can specify that I want to happen five times no fewer and no more. So let us look at the syntax for a for loop. So it starts off with the key word for and then we open parentheses and then inside off the parentheses. We have a few things that we need to do. One, we need to define what we call the counter. So our counter is going to keep track off the number of times it is being executed. So we need to me the counter have of base value. And I'm just going to say I assured for adoration doesn't really matter. You're going to verbal can have any name. You could call it cones, but just make sure you have a variable there, so I'm calling it I so in Thai is equal to zero. So I'm initializing my counter toe, something that is very basic, very low in value on I'm starting it at zero. Then I have a semi colon. Then I see I less than on. Then I'm going to give it the value that it should always run to be less than so. Here's where it can get tricky and I just I'm going to Travis Toe, explain it so that you understand why it's going to be written this way. So I'm going to say I which is going to have an initial value of zero less than five andan semicolon again and then I plus plus. Now let me explain what's happening here. So I have a colander on dime initializing into zero. Then I have a condition where I'm seeing. Repeat as long as this condition is well, as as it's met, then continue to repeat yourself on. Then I have this section, which is the incriminating, saying that the counter variable is counting how many times it runs. So I wanted to run five times on the first run. It will have a value of zero. Then it will do the comparison. C zero is less than five. OK, then it runs on, then it increments. So no, I is one. Then it compares is one less than five it is. Then it runs again and then it will do that and do that until we get toe I being equal to five, and then it will check it. Five is less than five, which we all know is not true. Five is not less than 55 is equal to five, so then it will exit the loop. Now you're probably wondering, OK, so why the zero? And why? Computed it's less than five. Well, if you put your handle right now and do the math and you won't zero, that's one finger. 1234 Then you would have counted all five fingers on your hand. So it's typical practice to actually start off your counter at zero on. Then just let it that the comparison run it until it is less than the number that you wish to stop that right? The alternative would be if I started this at one. Then I would have to change my condition to see that I should be less than or equal to five . So it would have looked at the less than or equal very briefly in the selection section. But what I'm trying to point out is that typically, if you look at any other example, if you pause this video and going Google a four loop example you're going to see more often than not, that they initialize the contract zero on. Then they made the condition run until it is less than the intended number off executions. So it is zero, then turns the one that it turns into it and it turns the three. Then it turns to four out the point, it turns to four. The loop would have executed five times. So when it becomes five, it does this comparison realized that this is no violated on. Then it will exit the loop. So that is part one off writing or for loop. So then, after we do that, then we need our braces. So you notice that curly braces are like a mean state functions need them if statements need them. And no, we see that loops need them, right? So whatever we read inside off the curly braces would constitute the action that is being done each time. So let us say I wanted to print I'm a winner five times. I'm going to say console that right line. Right line on. Then. The text I'm writing to the screen is I, um ah, winner with an exclamation sign in case, the oil capsules and Cleary cough. All right, so I am a winner and I want that printed five times to the screen. So remember that in the early example, I didn't manually by writing the line multiple times This time I'm writing it once, but I wanted to print out five times. Some goingto just switch my start a project with glitter repetition statements. And then we're going to look at this and see if how many times is going to print. All right, so then we see I am a winner being printed five times. So this was the time it got printed while I was zero on. Then it went, are owned. So it did the action and it hit the current breeze. Then he came back toe one increments. So I did an I plus Plus it incriminated from 0 to 1 on. Then he did the comparison to say Is I still less than five? It was So it ran again on. Then it did the same thing came back incriminated. So no, I was too on. Then that comparison happened. It realize it was still less than five on. Then he came back and then it went to three, and then it went to four. And then when it hit the four on, then it incremental did on DNO. I had a value off five. It did. The comparison on five is not less than five. So this this condition was no violated. So it came back to the car libraries and then exited. So whatever else would have been under here on, let's just say I said Toe console that right line on then this is a nice, cool way to get, like, some space. So if you want, like a blank space between two lines, you know, console that right line. I want an empty spaces under store console. That right line on it gives you a blank space right on. I just duplicated by pressing control dissidents by being on the line control de on. That will duplicate the line on that. I'm going to say for Luke finished. All right, so let's try that again, someone to execute this one more time. And then here we see that it executed everything and then, when it exited, continued to execute the rest off the court. So that's once again sequence so it won't move on until all off what it needed to do inside the loop was done, which was to print this five times to run five times and whatever I told it to do 45 times on. Then once it finished, it went on to print the blank space. That's an empty console. That right line on, then print for loop finished? No, I'm going to do a little modification toe What it does inside the for loop. I'm going to print another line. So remember that as many commands as you want to repeat, we can put as long as they're inside the two calibration someone toe console that right line on this time, I want to print the value off the the incremental ing variable or the counter. Right. So this time I'm going to say counter on, I just going to put ah Colon on. Then I'm going to use my interpolation on, then plug in that value. So I'm just going to say I all right, so each time that the loop runs whatever value I has, I am going to be printing into the screen just so you can see that it starts off with zero on goes up to four, but it would ever on five times. All right, so let me try that again. All right? So, no, we see I am a winner on the counter value zero. Then I am a winner again. Counter value goes to one than 2 to 123 then toe four. So 01234 equals five executions. All right, so it's good practice to understand why we start at zero. Because when you get a raise and collections, then you're going to understand that the computer actually starts counting from zero in as human beings, we tend to ignore zero. We start counting out one, Sure, but the computer starts counting at zero. So it's good to appreciate why we would start counting from zero. That being said, I didn't necessarily have to start from zero. I could have started from 50 and counting to 55. And so I could have said the Contra should start off as 55 on git should execute until it hits 80. All right, so whatever number off times it's going to go between 55 80 or if you do the math and say a T minus 55 Whatever number you get, that's the number of times going to execute. All right, So if I just do that quickly, the end result of that is this line. I am a winner and the counter being printed from 55 which is a valued started with up until 79. Which means that when it it 80 it violated this condition on, Jumped Out on. That's pretty much it for four loops. So this is a very simple example, but I hold that you appreciate it. Someone's going to reset this court so that when you look at the example you see it on, I'm just going to add sample printing five times. All right, so now we have some comments to guide what this is doing. All right, so let's no once in a while. So the while loop is what we call a condition controlled loop. And then I appended that with the expression pre check. So when we do the do while, then you see what the pre versus the post really mean? But right now I'm just going to focus on the condition control. So the condition control portion of it means that it is going to continent executing until a condition is violated or it will continue to execute while a condition holds true. So with our for a loop, it was counter control, which means that we're just executing for a finite number of times because we know we wanted to do five times. So we specify are we wrote it in a way to make sure that it doesn't exceed five times. However, if we want something to go on and on and on and on until ah, particular condition is met are violated, then we would use something like a while loop. So the thing is, know that with the wire loop you can run into what we call an in finite loop, where if you write the court for the while, loop on the condition never gets violated, it will never exit that loop on DSO. It will run on and on on on right. There's always the computers on on whatever conditions are required for it to be running our dear, it will just continue to execute. So I'm going to start off by showing you an example off our on in finite loop with the wild for it. So I want to declare a simple variable and call it in. I give it a value of zero that I'm going to say a while and is equivalent to zero on the noticed this in talks, we have the key word while and then we opened the parentheses and then we put in our condition just like all we would have done with the if statement. Right? So the only difference between it and the statement is that instead of saying if we say while right, so we say while open parentheses, put in the condition on, then open curly braces with our actions, right? So the way I've written this and is not going to change from zero. So this is going to say a while and has the value is equivalent to the value zero. I want to do this, someone to write two lines and just copy existing lines. Someone toe print a blank line, and then I'm going to see in while look all right. So while the loop is running, is going to print, say, in wire, Look on, then the blank line. So it's going to see in side inside while loop on. We're printing a blank line after that. Now I'm just going to comment this boat quickly so that it doesn't get executed while we run this program because, no, I want to focus on the while loop and you're going to notice that it's just going to continuously print and never stop. So let me execute. Ensure you on. That's what it looks like. So you see, it's just printing, printing, printing, printing, printing it, saying inside wire loop and you can see that it can as a blank space. You look at the screw piracy that is just going on and on and on and on and on. It's never stopping, and that's what we call an infinite look. These infinite loops are known to break computers, So if your computer crashes, who and you do this? I'm sorry, Um, but I have, ah, slyly powerful machine, so at least I know that this won't bring my machine. But the fact is, if you're right, poor chord in big applications on, Do you get this or end up with this situation? These situations are in finite loops, can bring down systems. All right, so you want to make sure that you understand whole while loops work so that you don't make this mistake at any point. All right, so let's exit that and look at how we should write while loops to make the most off it or to take advantage off the condition while not leading to an infinite loop no or condition is based on the fact that in is equivalent to zero. So it's going to run while L n sorry is equivalent to zero. So what if I change this on? Guy said something like while En is less than five. All right. So while the value off and is less than five, I wanted to do this. No, based on the way it's written, this will still be an infinite loop because any 00 is less than five, and I'm not doing anything to change the value off in on. So it's going to always be less than five months to continue to execute. So that means the one thing I can do to make sure that this gets violated eventually is to make sure that I am changing the value or putting something in place that the value changes . At some point on this compares a will get broken or this condition will be met or violated . Rather, at some points that is convenient to hold. We want a program to work. So I want this to print five times. Yes, we accomplished something like that with a for loop. But I'm just showing that I can accomplish something that that with the while loop. But the past two doing so is going to be very different. So I need to know Make sure that I am changing the value off in tow, be something that it eventually will exceed our value. That makes this condition true. So I'm just going to add 1/3 line toe inside my wire loop on. Then I'm going to say and is equal to end close five. All right. Sorry and is equal to N plus one. So when I say any easy, go to n plus one, remember, we discussed that this could be shortened into just seeing and close equal once. Oh, no. It's going to be adding one to itself each time. Right? So it's going on by one. Until eventually he will execute on end is going to have a value that is not going to make . That is not going to be less than five, and so are a wire loop will exit. So let's test that also execute. And here we see that it literally printed the right number of time. So just like with the for loop where we started off a zero and went ill, it was less than five. We started off with n being zero on then we're just checking on the condition that is less than five. But there is nothing in the loop or in the structure off the loop. That is not naturally, that will help us toe change the value off. And so were to do that manually by putting on accumulations. Will call this an accumulator when you make a variable increment by itself, plus another value. All right, so when I said n equals n plus one that's making it an accumulator are plus equal. So now I'm accumulating value each time it runs on. Then it eventually will be. Um we'll meet this condition now. This is a very simple on not very practical example. I mean, there are situations. You may use this, but I'm more useful Way for use in the while loop is to say, compare user input on get the user input, violate the condition and continue to do this until the user violates the condition on the loop will exit. All right, so So let's take your experience with an A B. M, for example. And a B M will probably always ask you, Do you want to continue? If you say yes, it continues if it says no and then it repeats the same steps on as long as you keep on saying yes. Right? So literally. All they have is really a wire loop that is allowing you to do carry out operations on. Then you can indicate when you want it to stop. And then when you give it the condition that say stop, then you will say, Okay, that condition has been violated. Let me move on. So I'm going to modify this and I'm going to see console that right line on. I want to tell the user input a number, all right, and I'm going to say zero toe four. So I'm telling the user input some number between 0 to 4 on. Then I'm going to do a read line where I'm going to say and is equal to convert to. Sorry spoke. Convert wrong converts to in 32 on day, I'm going to say Console, that's read line. So I'm accepting user input on I'm not going to increment because no, I have the user determining the value off in each time. Right on going to take all this console that rat line. Well, let me put it back. And then I'm going to print the screen you entered on. I want to see you entered that number. All right, so let me just put on my dollar sign interpolation. So what this loop is expected to do is that it's going toe from the user to enter a number . If I enter zero, then if I enter zero, then it will print. You entered zero, and then it's going to come back and it's going to check. The condition is zero less than five. No, it's not. So it's going to say in turn number again on then it's going to continue doing that until I put in a number that clearly is not less than five or well clearly violate this condition. So let's try that. So in put a number on die, put in three percenter. You in. You entered three and then it's prompting me again. So I entered three. It printed that I enter tree on. Then he came, Did the comparison. I realize that this condition is still true because three is less than five. So it went ahead and asked me for another number. If I put in four, it will do it again if I put in one. If I put the negative five as long as a number is not, is a long as the number is less than five, then it will continue toe do this operation. No. If I put in 10 it will exit. All right, so let me put in a proper statement to see it has exited. So I'm just taking this Previously, Juan on commenting it and see keyboard shark was just make it easy to maneuver. Right? So, um, while loop finished. All right, so let's try that again. So we have 0 to 4. I put zero. I put five on, then. Well, okay. So entered five. Did that prematurely, but that's fine. So I entered zero and it just said, Okay, here's us and five prompt him again. Then I entered five. It came to the comparison. Five is not less than five. And so he just went down to this curly brace and then continued with the execution where just said why Luke finished. All right, So that's basically how we can use while lose, because once again, it's going to run against the condition, and we want to make sure that this condition can get violated on legitimately. So because if we don't do that, then all the operations at a while loop is repeating will be repeated until that computer shuts off Andi, I can leave toe lead to some buffer or memory overload and all sorts of things that we don't want. So when writing condition controlled loops, make sure that the condition can be violated at some point and legitimately so now we move on to our do wild Andi. I did say that I would mention the pre versus post, so the reason we call the why look, the pre check is that is going to check this condition before it executes. So if I initialized and toe have a value off five or let's say 10 then the first thing is going to do is check. Is this condition true? That's the pre check is the condition truths? If any Sten on 10 is not less than five, then nothing here will get done. It will just move on to say while you finished. So that's a pre check. The post check will do it before, So it will do the action on Then check the condition. Right. So let me just, um, right the do while. And I'm going to do the same operation inside to do well. But then we'll see the difference in the execution. So the syntax for do while is the word du de, or we open and close the curly braces on. Then on the clothes curly brace, you have to say while on then you put in your condition check. So the wild part looks just like the regular Well, so I'm just going to copy this MPC don't Here it looks the same way. All right. So let the only difference is that we say while on then open the calibrates the do. I will say, Do this action. Do whatever is inside the curly braces on. Then check the condition afterwards on. Then this one requires a semicolon at the end of it. So that's where you can get get kind of tricky, because then you see that or no semicolon and cynical And for this one and there and there . So, really, Angel is just practice that will make you get usedto were to put the semicolons and we're not too all right. And visual studio is always ready to give you that encouraging red line to remind you that Hey, there's something missing here. So then it's selling you semicolon is expected. You put it there, and all is well in the universe after that. So I'm just going to copy these actions quickly. But inside the do while on Patel into space so we can see exactly what's happening on. Then I'll put the statement that tells us when the do while loop is finished. All right, so I'm actually not going to come in this autumn winter. Run it so the value off N is 10. Remember that the pre check is going to check the condition If the condition is true, then it will do what it's what what we told it to do on. Then it will come back and set the condition again. If the condition is not true, then it will not do anything all right, whereas with the Doyle it will do something on and then check. So that means I have the opportunity in this one to change the value off n from 10 on. Then it can. Well, we can violate that, or it can make it true. So it will just say, Oh, this condition is true. Let's do it again So I will do it at least once, so let's try that. So I click, start and notice. The first thing that sprinted the screen is something to tell us to do the while loop is finished. So you know, it just skipped it over. As I said it, just check the condition. 10 less than five that's false are don't bother with that print the blank line on Give me the while who finished. But then the do while is asking me for the number so it's doing the action on, then it's giving me the opportunity to put in the number and then it will check the condition. All right, so if we want to do something at least once, it's are repeatable thing. But you want to make sure that it happens at least once. Then you want to use a do while. All right, So then I want input a number and let's say input three. It will repeat. All right. So when I entered three unprompted, then it hit the condition and trees less than five. So it said, Well, do it again on then, this time with the Enter five or well, I've said 56. That's fine on the entered 56 which is clearly not less than five on DSO. It just went on to say, while loop do I look is finished. So that's the only difference between the while. And to do while the do well does that least once on checks after, whereas the while loop will do, it will check before it does, and then we remember that or four loop and others UN commented. Before we wrap this up, the for loop is set to run for hold. Many times we wanted to run. That's it for petition statements in C Sharp, remember to practice and think off scenarios where you may need them. We're going to be using them or in our project, but for no, this is an introduction.
12. Methods and Return Types: this video is about methods and functions in C. Sharp. I've already created my project. I called it methods on. You can pause here and go ahead and create that project and continue once you have done so in any strongly typed programming languages, There are predominantly two types off functions or methods. I say functions and methods can off interchangeably. They're really the same thing. So ah, function or a method can be void, meaning it doesn't return a value on it can be value returning Now I want to start with avoid because it's really easy to explain why you need a void. But before I do that, I'm going to point out that we already have a function to look at as an example. So we have static, void, mean So I would have said multiple times that we know functions because they take the parenthesis on the and off. The word writes off static. Avoid mean that is our first function. Notice its anatomy on also the fact that this one is also avoid. So if I'm going to be making avoid function, I can use mean as my template. I won't use the word static That's kind of a special word for a special situation. So I just keep it simple and I will say, Avoid print name. So I want my phone shown to be called print name aren't on. Then I need the parent to seize on. Then I need Mike Alibris body. So if you look at it, the only difference between it and the mean is that, well, me and has something inside of the parentheses, and it also has a keyword static, but otherwise they're pretty much the same, and not to me. Now, allow me to explain what each component means. So we start off with our access modifier. So we have static here. That's an example of on access modifier. There are others, but there I'm just going to leave it with that. So that's an example off on access modifier. But then we have the return type. So in this case, once again, it is void and void means that it doesn't need to return anything. Someone to explain what I mean when I keep on seeing that's return something or not so avoid is our return type, which means nothing. We're not returning anything. Then we have the name I'm calling it print name. This one is called mean, but I can't have another function called me That's that's most times illegal. It's a lot on a special circumstances where you have to know exactly what you're doing, and that's called function overloading. But that's a conversation for another time. But it is best to know that they should not have the same name unless it is very, very deliberate. So we avoid the name on. Then we have the parameter lists with their no parameters to be passed in. Then we know that the para the parentheses remain empty. So in the case off mean it is expecting a parameter off type string meaning or string ari, meaning that if I'm going to be calling it Andi, I am providing values. They have to be off type string and in an airy format. So let's continue with our simple void function. That's his print name and all. I'm going to meet. This, too, is. Do a console, that right line. Just get my spelling right, all right, and we're printing my name Travel or Williams. That's all it's going to do. So we have a function called print name on its sole purpose is to run this line that says Print to the console, the name Trib or Williams So function can do many operations, you know, just like Oh, we've looked at the four Loop and the if statement and as many lines off court or commands that you give the program in between the curly braces off any body off court. Then you can do that many operations. I could ask it to run a four loop inside of the function. I could ask you to do an if statement anything I put inside. There it's will do. However, the more important thing after defining it is actually calling it. So this is a definition, so there's a void function definition on. No, we need to actually call it so in the body off the mean I would meet the function call. So what happens is that when we click start, the visual studio is looking for a function called me in, and that's why I would have said, Do not modify anything in that line or the curly braces is looking for this function. If this formation is not there, then you don't have a program. Everything gets executed inside off this function. No, if I tell it, the first line is to look for another function. Then it will pause. Look inside off this class file and noticed I defined the function inside off the class Curly braces. So when we get the classes on objects and you'll appreciate that better. But when defining the function, make sure that it's inside the class called Program, and it's not inside mean it's its own function, and it's actually like a sibling off me. And it's its own person, right? It's capable off many things, but it just does not have the name mean, all right, so when I make a function call, it's going toe. Well, first of the program executes mean. Then it sees the phone showing call on. Then it searches for a form shown called Whatever name I specified. So I call the function print names, so that means a function card has to be to my function print name. And then I have to open and close the braces to specify that this is a function. Now you see that I have a red line underneath my function and basically the error message. If you don't know what you're doing, it is off. No help. So I did say that we have access modifiers on static is one off them. Static is not always necessary. But in this situation, because static void mean is the main function on its not every version off seizure that actually has. It s static. So that's what I didn't want to bring too much attention to. But if you do are are using ah version of seizure that has is a static void mean and is looking for a function. Whether informs any put there to be off equal access modifier value. All right, So it's going to be saying that it can't find your phone sean print name because it doesn't know the access that attest. It's I have toe upend our pre penned this with the world static on. Then our errors go away. So no ar function call is working. We have print name on. The expectation is that when I press start, it's going to start up mean look inside and see that the very first thing you should do is Carless function that is going to jump out. Look for the function and you notice when I click the formation here, it highlighted the corresponding definition on. Then it's going to go to this function and say, What should I do? Then when it's done, it comes back and continues execution. So I'm just going to put a little console right line underneath the function call to see end function call. All right, so let's try that. All right, so there we go. It went into mean, then went to the phone. Sean call did what the function told it to do, which was to print whatever value I gave it on. Then when it was done, it continued the execution off me. And until the end. Now that was pretty cool and easy. What if I wanted to a low the users to enter their names, and then I wanted a function that was dedicated to printing the names that were entered. So now I'm going to do that function over. I think that I mentioned earlier where I'm going to have another function that's a sprint name. But then this time is going to accept what we call parameters, right? So, no, I'm going into two different concepts. One is function overloading where I'm just going to double. I'm just going to duplicate that, all right? And then you notice a visual studio is no crying because it saying it already has a function called that. And then this one is crying because it's seeing I don't know which function I'm supposed to be talking to there. I'm seeing multiple references off the same function. Know, to differentiate the tool I'm going toe add what I call parameters to the 2nd 1 that I created. So parameters are variables that are there variables that I'm declaring inside off the function on them alot to pass values in. And then you can have access to them inside the operation. Right? So let me do it slowly and explain everything as I go along. So I did say I wanted to a low the user to be ableto enter their name. And then the function is going to print whatever they entered. So that means I'm going to have a function with the same name print name, but it is affecting to get some value from the user. So for the parameter, if I'm asking for a name than I can say. String name. All right, so it's a variable. I have to give it the data type on the name just like Oh, any other variable is defined. Alright on, then. Once I've done that, I can use that variable inside off my function to do anything so I can just console that right line name. So once that value wants, that function is called and that values passed in, then I'm just going to print the value and it exits the function on, then comes back to mean So here. So we're going to call this one? No. So remember that when we had print me where both of them are Sprint name with all parameters, this function call given error. So I'm just going toe right. Something quickly to say console dot Right line into your name on. Then I'm going to define a variable on. I'm going to call it and just some string called and and I'm going to write read the input coming from our user. All right, so this may look like a lot off busy work, but it's fine. Um, we're going to be passing. And so whatever comes in through the console once the person enters their name. Are we starting in that I'm going to show you how to get it into the function. All right. So ah, function can be for anything. You can use that function to do anything. I'm using it to do something very simple right now, so I'm actually going to do something differently, right? So that it looks like it makes more sense. So I'm going to print out name in all uppercase. I'm going to say name dot Toe Cooper. All right, so that's another function in our catalogue off string manipulation functions where I'm going to pass in the name from the user. And then it's going to prints name in uppercase. So toe pass it into print near I'm going to do another function. Call on, then look at what visual studio does when I do the brakes. It shows me that I have full versions, so it's seeing one off to you have the version with no parameters, and it says, OK, that's funny. There is one there like that so you you can leave it alone. But then they also tell me of another one where It's expecting some string parameter called name so I can pass in any string value that I want on that will get stored inside name. Once this function is called, it's passing that valuing in in tow the variable called Name on. Then I am performing an operation with that value coming in in name. That being said, you can have multiple functions. I just created too weak and have 10. You can have many as long as you follow those rules with names on the parameters, then you should be fine. So if you wanted one that says print name into lower than I can create another one on, I'm going to say print, name, lower case. So print name and I want to call it lower case. I noticed that visual studio is going toe cause up on it. No, because I have to print names on both of them have only one parameter, so they're identical, right? So, like I said, function overloading, you can have the same name. Just make sure that the parameter listing is different. So I'm not overload in this one. I'm just going to say print name, lower case. All right, so then it's going to receive some some very big called name. I notice I have a very big call name here on a verbal called name here on. It's not illegal. Why? Because this name instances you need to this function and the same for this one is unique to this function, so they don't see each other. These two functions are really curable to each other, right? This one is. It is in its own world and this one is in its old world. On main is really in its own world. Mean is just the one that gets called first. And we have to let me and call the others afterwards, right? So I'm going to just do two more phone chung cause here's someone to say Enter your name on then we print about the name our doing that name being printed the upper case. So I'm going to say and function uppercase name. All right. I didn't want to call print lower this time. Someone to call print name, lower case and the intelligence is picking up contextually. What's in the faster. It's actually helping me with my custom defined function on that I'm seeing in function lower case. So we know exactly what is happening along the way on. Then we just make sure that we're putting this in lower case or named up to lower. That's all we do. Lower kiss on. Then let's set this execution. So for our execution, I don't sell if the are off your but that's fine. So I'm going to say maybe I want something mixed kids. So I'm going to say and funny with a capital T h on or and capital, Why just so that we can see the variations Because we have one that's supposed to do an upper case. I want that supposed to lower case. So the expectation is that everything should be in upper case at one point, and everything should be in lower case. That's another point. So the mixed case is removing the bias towards either case. When I presenter, there we go. We have Anthony. So we we got to the point where we console that red line on. Then it went on to call print name, which it did on. Then it did the upper case give us uppercase Antony. And when it was finished, he came back to me and ended on Randall Command after that. Then it went into the next formation, were then lower cased it. So we got Anthony Lo kiss and that and then we went on to end that on then mean, so mean was running from top to bottom. Like we said in the sequence off everything on the only thing that makes it seem like it was stopping and going elsewhere is because of the form shown cards. So functions are really useful. When you have little operations that you want to do that you don't want to put everything inside the mean. You can just make a function and right, you know, a nice, long, huge block off court elsewhere, and then just make reference to it. Also, if there's an operation that you want to do multiple times, you don't want to be writing essays. Every time I'm copying and pasting the console, that right lines right, No right line is a function, and this function has quite a few lines off court. The C Sharp developers didn't tell you that you have to write the few lines off court every time you want to write putting to the console. Instead, they put it in a function on. They told you, Just call this function whenever you want to access all those lines off court. So that's it for Void. I've done three simple void functions, but then I'm going to go into value returning. And then I went to do some other operations that if we were to repeat them, you know, they're more lines off court that we're going to write in this, and it will see exactly how convenient it is when we just have our functions. Now our value returning functions are very much similar in terms off the anatomy. The only difference, really, is that they're not void, but they actually have data types. So if you want something to return a string, then you have to define it as a string on. I did someone to explain what I mean by return, so let's get started. I want to write a program that is going to accept three numbers from some user on, then determine which one off them is the largest. So I'm getting this party started just by writing three problems for N 12 and three toe get three numbers from our user No to move forward. I want to call a function that's going to take these three numbers and then tell me which one off them is the largest. All right, so I'm going to define a function. I notice I'm doing it outside off me and once again. So I have the comment there to guide me on. I'm just going to see int or whether remember, we have to start off with static and then say int on the largest number. That's the name of my function. So access modifier data type, which is int not void, but in because I'm expecting to get a number from the operation. If I was expecting to get a string that the data type would be string if I was expecting to get a true or false, the data type would be bull. So based on what you're expecting to get out of their phone shown, that will determine the data type that you use. So let's start over access modifier static. The type in this situation is int. Our function name is largest number on, then we have or parameter list and then, or curly braces for the body. No, we're going to see this red line for a while because it's complaining that not all court paths return of values. I keep on using the word return What is returned? Void functions don't need a return. But we have a T word inside off our value returning functions that say's return. So we have tohave that value return. So I'm just going to leave returns here, there just to get rid off the red line for no. So you can do that, get rid off the red line and leave it alone. But that will have to change eventually. Now, I did say that we were building this function to take the three numbers. So if I'm getting the input from my main function on, I want my function. That is not mean, which is largest number to know about these three numbers that I'm going to have to define parameters for each number. So I'm going to have to say in Andi I can call them in one, or I can call them numb one. So I don't have to retain the same name. So you know what is that? We got a variable. We got valiant to the variable end. But then we called it name in our functions, right, because this is generic because regardless, off the name off the variable, when we made the function call, we're really just passing in the value. So this could have been a static string, or it could be any string variable that we call. But our function will just represent that value in the variable called name or whatever we call the parameter. So once again, it doesn't matter what we call the parameter variables as long as you make sure that we pass in the correct values aren't so. I went to see into someone Andi int num two on into numb three. All right, so we have three numbers that were passing in on. Then we're going to use an if statement. So, like I said, we can do any operation inside off a function, so this function is going to be a bit more complicated than the void ones that we've done. But that being said, it's not because one is void on one. His value return why one is more complicated, and the other you can do any number off operations inside off the function as you need toe . Now let's right the court for this function. So I'm firstly going to define my largest as my numb one. So I'm seeing int Largest is Ik Walter No. One. That's just a while Assumption. I don't really know if it's my largest. I just want something to represent the largest number on then know that I'm assuming that it is the largest understand to compare, to see if numb to is greater than it on. If number three is greater than it's, I'm going to say something like if numb, who is greater than largest then my new largest value, as far as I know is known to. So I'm going to assign that value in tow. Largest. So no, I've done one comparison. I've already assumed them one is largest on. Then I'm going to compare it and numb to And if numb to turns out to have a greater value than the current largest, then numb to is my new largest value. No, I need to compare largest with Norm three. So the reason I'm doing it like that is, if this value is the true, then I need a way to compare the value that's in numb to with the Validus in numb three on the value off. Lahm, too, would no be the largest. So I need to say, If numb three is greater than largest, then the new largest is numb. Three. You see that? So let's start over. I am setting the largest value as no more to begin with. Then I'm going to say, if numb to is greater than largest, meaning numb to is greater than the value that's in Nome one, then it is my new largest largest is always my big comparison. So if this doesn't evaluate the truth, the largest is still no more than I'm going to compare whatever values in largest, which at the time is no more in with the value that's in numb three. If known three turns out to be greater than largest, then the largest is no numb three. If both off these fail then largest was no more from the beginning, so know that we have done all off this operation and we know the largest. No, we need to return that value after the function has done its operation, and then that's where that return to you word an operation comes in, so we have to see return and then we're returning largest. So the return data type has too much the data type off the functions. I can be returning full name here. I'm returning a string. Let's see what happens if I put a string their return Hello, then That's just an era. Because it saying I cannot implicitly convert string to end because I'm expecting a return type off integer but you're giving me a string. So I have toe return the data type that is used to define the function. So I'm returning largest, so the largest. With whatever comes out of this operation, I'm returning that value. No, in terms of the function call. We need a way to get that value from the function. So remember that void doesn't return anything, so we can just a function call at any point. I would just mean the function call and leave it there. But then, when it's a value returning because it's sending back a value, I need to be able to catch chapter or store that value. However you want to think about it. So I went after of another look of variable. I want to call it results on the contents. Off results will be whatever is returned from this function Call. So I'm just going to meet the function call Largest number on. Then I'm passing in. Remember I'm expecting to buy. Are expected to pass in no more numb toe Unknown three. So I'm passing in n one into and in three. All right, on, then Semi Colon. So let's look at this execution one more time I get the three numbers from my user and then I'm seeing I want the content off This variable called result to be the result that is returned When I called my function largest number on passing all off those numbers. So when he caused largest number, he gets the three numbers no more numb to numb. Three it does its operation at the end of the operation were sending back the value that is as a result of this operation. And once again, once it's sent Buck, it is no stored inside result. So if I wanted to print to the screen the largest value than I would have to say console that right line largest number colon results. Alright, An amusing interpolation here. So once again that is hold that will work somewhere is going toe execute and it's going to hit. The main function on the main functions is the print name and the print name and and the lower case and everything on. Then it will go to the promise. All right, so I'm executing me and again and it's prompting first for the name. So I'm just going to give it the name test. Let you go by that. So you see detested upper cases on it. Lower case it. That's fine. But then I want my number. So it hits all off those No, Is that a prompt? First number is one second number is five onda third number is 10 on we see largest number 10. Why? Because it hit the phone shown call, did its operation on, then returned the largest which were then stored in results on. Then we printed back to screen. So that's a quick introduction toe methods and functions in C sharp as you go along and we hit the object or enter programming more. You're going to realize that functions are at the basis off this language and any other object oriented language
13. String Manipulation Functions: In this video, we'll be looking at string manipulation in C. Sharp. I've already created my project, and the name of the project is string manipulation. So you may pause the video here and create your console application project called it String Manipulation, and then we can pick up from there now. Today we just want to focus on some of the tips and tricks and quick ways to process strings in C. Sharp. No strings are an essential part off any programming language they represent. Well, I would say 90% of the contents that would be passing through your application everything that is input into your program through the use of a keyboard pretty much comes in as a string, and it is important to understand the tools that C Sharp has available to us now have started off by the clearing a string variable, and I'm calling it full name on DI. Have a sentence here. My name is, and ever spelling error is travel or Williams. Now we've been doing this for a while now, and if not, then that's fine toe print the screen we say console, which is a static class given toes by a c sharp on. Then we have the function inside off that static class called right line on. Then we specify the string to be printed. No, this string can be something that we're going to type specifically on. A string always comes inside quotation marks or it can be the contents off a variable that is off type string. So full name is off type string on off course. You have to make sure we get our cases properly on. Then this will print the contents off for variable toe. The console lets us test how that works, so I'm going to change. My starts are project string manipulation and start. And there we go. Hi, My name is Trevor Williams. So or console that rat line printed. The contents off are variable toe our console. No, let's move on to concatenation now. I just declared to more variables. One called first name one called last name on. Then I would like to print out one contiguous block off string. Contiguous is another way of seeing continue. Waas on that string should have my name is on then have the first name on the last name. So concatenation is the technique by which we join the contents off strings together. So I'm going to console that right line and then I'm going to put in some static string on . Then this is going to say my full name is so when we say static string, that means that unless we come into our court and make an adjustment here, this will never change. However, variables are easier to change because then we can use them toe collect user input on its contents would be defined based on what is being assigned to it. However, there are times when we want to dynamically print what's in a variable alongside something that is static. So concatenation allows us to join static text onto variable text or variable takes until bura bill text or even static text under static text. The underlying principle is that they're all strings now. There are a few ways to con Captain eight on the 1st 1 That I would want to show you is by using a curly brace on. Then we're going to close. So we have credible res zero and then curly brace on that. I want to put the space calibrates one curly breeze. No, in effect, these are what you call placeholders on. They start counting from zero so as many placeholders as you have. Or you're going to create as many placeholders for as many variables as you'd like to con Captain Eight into the static string. And you start counting from 00 will be the 1st 1 Then one the second and the 3rd 1 would be to. But then we only have two variables. So it's zero on one first name on last name. Note. The space is also important because it will print back the string exactly how it is formatted inside off the quotation marks. So after placing are place holders there, we have to come out of the string on press comma on. Then we put in the variables in the very order to match the placeholders as intended. So I want to say my full name is first name and last name. So that means I need to put the variable that has the content for the first name, followed by the variable that has the content for the last name on then. This can happen for as many strings as we have. So many as many string variables as we have another way to contact in it. And I'll just to pick a this line. I just rewrite it. So console that right line and rewrite our sentence. My full name is on. Then I can use the plus sign so I would write. My full name is as my static text press the plus sign and they say first name and then a plus sign on because I would want toe add a space between the first name on the last name values. I would have to put another block off string first space on, enclose that block, clustering on, then close sign and then last name. So essentially I'm doing like string mathematics. I'm just adding as many strings as I need toe. Join them on, then all off, this will be printed out in one continuous line. So I'm going to get a static text. My full name is with that space. See that space between the colon on the in quotation marks? Then I had the first name and then after add another space on. Then I'm adding the last name the third way to contact in it After acting consul. That right line is by doing what we call interpolation. So in this instance, we actually start off or string with a dollar sign. Then we open quotation marks on then this Allah's issues are curly breeze on input are variable directly inside off the quotation marks. So then I can see my full name is space on. Does inject the first name, have a space on, then inject last name all of this still inside off the quotation marks. Let's test all three of these and see that they work the same way. There we go. Three different techniques, same sentence. My full name is trouble for Williams. Noticed the spaces are all there on Once again. It all depends on how you format your text in the static parts. Now let's move on to counting the length off our string. If I wanted to know how many letters were in my first name, then I would need to find the ConEd off the content. So remember that I'm putting it in a variable. Andi, If we were allowing the user to enter content into this variable, I would not know at any point what the value off first name is, however, I do want to know. I wanted to probably build a program that alot people just type in their name and then tell them your name is X characters long. So to find the strength off a string I would need, firstly, an integer variable and understands Call this int length because I need somewhere to store the length value. And then I'm going Just call the string variable by its name. So first name, then I can see dots on. Then I go to the function or property rather length. So the difference Dina function on the properties that you me call functions using the pardon disease, but in their times and you have to use the dot operator. And when we get into the explanation off object or in the programming, then I'll explain that not operator a bit more. But once we say some variable, some string variable and then we say doctor and I just remove the length on. Then we control space. We can see that we have a host off. Property is shown by the wrench on function soon by the Cube. So if I say dot length it is no getting back the length off whatever string or whatever valued this string variable has. So I'm going to run this program, and I believe my name is seven characters long, so I'm going to put that theory to the test. I did that prematurely. I did not put the console that right line, so console that right line on were writing the value off the verbal length. And there we go. So my name is indeed seven characters long, but he's not. That space is also called as values. So if I found the length off full name on, I'll just duplicate these two lines on, then do another prince. But this time I'm going to say, I want the length off. Full name on Get me just see length one, some printing, the value off length one this time on. Then I'm going toe, see what body that deals then My sentence that is in the variable full name is actually 31 characters long because each I comma space That's four characters on. Then all off them add up. So everything that is between the quotation marks will be counted in the calculation off the length C sharp makes it easier for us to replace the contents or parts off a string. But I'm going to explain what I mean by that. So one. Once a string is declared, then you cannot just run the function and change the value off the string. You have to put on operation like a replacement inside off another variable, and then you may use that variable. So I'm going to have to declare a new variable string new name on. Then I'm going to make it equal toe first name, not replace on. Then the replace function takes two characters. It takes the old char on the new char old charm, meaning what letter or character or symbol, whatever it is that is in the string that I want to remove what it is. What is it? So I want toe replace the letter T and then I say comma, I noticed that the char has the single quotation marks, so char is not double quotation marks like string, but it is single quotation marks on. Then we are placing the tea with Let's say K. I've never seen my name spelt like that or any variation off my name that's started their K , but the new name is going to be first name dot Replace any tea with the letter K on. Then let's just write that to the console on. Then see the old put off that operation. All right, there we go. So it looked inside of my name and it found the tea and replaced it with a key. Let's try one with some more letters. So let's say I wanted to replace all the eyes in Williams with holes someone toe. Just repeat this operation. Don't below on, then this. I'm doing it on last name. So last name Dr Place and I'm replacing I with Oh on. Then I'm doing new name one on I'm Printing Union wants to console and let's see what happens. All right, so every instance, often I got replaced by the new char, which is all. Like I said, this can be with any character. So if I wanted to replace the eyes with an at sign or hashtag or a number, whatever it is, as long as you can fit in between these quotation marks and it's just one I'll put if I put 12 let's see what happens if I put 12 then I'm getting on air, seeing too many characters in character, literal. So once again, a character is only singular, all right, so it's either going to be empty hard. It's going to be even if it's empty if I want to. That space after put a space but that can't have it with no content or with more than one, um, characters arts. I leave that that as all now let's move on to comparing strings. No, From time to time, we probably will want to compare something that is entered, maybe by a user, and it's stored in a variable with some string that we know and expect. So we want to do a compare. I want to compare to bits off input, like maybe when somebody types in a pass for the first time, and then they have to re type it the second time. You want to compare the two just to make sure that they type the same thing both times? No. You can always use an if statement. We went through its statement, so you can see if on going to use two variables that I have here already. First name last name. So what if I wanted to compare and see if the first name and last name had the same values ? So I can always say if first name is equivalent to last name. So that's an easy way toe. Compare them so off course, we can see if they are equal. Then we see console that right line. You have the seam name for first and last. All right, so we're telling them the okay, you have the same name for first and last else on. Then I'm going to see you have a different name for first and last. So there is always that option off the if else, that's that's perfectly fine. But then sometimes this might be kind off inaccurate. So then sometimes you want to say something like string on that's kept up its string and you can actually rather come on stream. The C sharp will see them interchangeably on. Then you have an option. That's his compare. So it can say string dot Compare on. Then you pass in the strings that you want to compare string a and string beans. So string a would be first name and that is the one that you want to compare with. Last name. So you see, there are many ways to do the same operation. But then this one is kind off, okay, Straightforward. It's going to say if they are equivalents than do this else do that this is going to return on integer. So I have to store it. The return value inside off a variable. So I want to say into results is equal toe stringed up. Compare first in common. Last name on. Then I'm going to have to do an if statement our own results so you can have three potential results from this operation. It can either be equal to zero. He can be our one or negative one when it is zero. That means that the two strings are equal. All right, so when it's a zero, when this operation stream not compare with two strings returns a zero, so I can see if result is equivalent to zero. Then it would evaluate to the same thing as first name being equal to last name. Then I can print you have the same first name and last name right on. Then I can see else also because then if it's a one or a negative one, though, simply mean that they're not equal. But then, when it's one, it means that the first string is probably bigger or has more characters on the sickle on string. All right, so when it's a want, it just means that, okay, the first string is longer than the second string on when it's negative. One is the other way our own. But the essential thing to note is that if you do this approach, then results should be equal in 20 to certify that the two strings are the same. All right, so these are two is you can compare strings. Um, you can use whichever one is appropriate you at the time. No. Previously, when you were looking at input and output programs, you would see where when you're receiving a number or you intend to receive something to store into ah numeric variable or some variable that is not already a string that you have to convert from the string toe. That data type well, sometimes you will need to do the reverse. So you would have seen that in concatenation earlier that you can actually con continent your string with almost any data type and it will accept it. But there are times when you need to convert to string. So converting to string is really easy though, so I have on integer variable here already. So I'm just going to reuse this variable on. Not if you have a variable already and you want to change the value. Don't have to specify the data type again. I can just see result is equal to zero right here to force it to that are 123456789 All right, I notice have plus signs. So I'm going to have to remove those. Sorry. All right. No spaces. So no, I have result which is an integer being equal toe. Ah, value off 12345789 That's one big integer result. No, If I wanted to sort this in a string, I wanted to say string big number is equal to And then I wanted it to have the contents off result. Then I will get an arrow Simple because it's going to say I cannot convert, are implicitly convert on integer into a string, which is fine. That's fair. So remember that integers are used for muff Pretty much I wanted to string. You won't be doing that kind off muff. So if I wanted to convert my integer in tow, maybe a string, for whatever reason, I can just look at the variable that I intended to convert on. Did say dot on. Then there's a magical function that is too string, so I just say results docked to string, and then it will convert that into a string on the once It's a string. It is less hassle to concocted it because there's not every situation that is going to be so forgiving. They can display grand into into a sentence on it won't through a narrow. All right, so there are times when you will want toe do, ah, conversion. So I'm just going to print my bank a cones. My bank balance is big number, all right, so my bank balance is big number on then that will just print out that sentence with this newly created string. No. Once again, once it's a string, even though it the content looks numeric, I cannot go ahead and perform an operation that is int thought of. Maybe I got a $1 payment today, so I want to add not $1. So I'm going to see one plus and then I can just say one plus big number. All right, that was through a narrow because I'm trying to do arithmetic here where I'm adding the number one toe something that is now a string. I cannot do that. So I will have toe off course. Just keep it into integer in nature on then. If I need total to be a string afterwards, then I can convert by seeing Dr String No, I mean this example with numbers. But then this is not unique to numbers. You can convert chart to string. You can convert double to string. You can convert almost anything to string their restrictions on maybe Ari's and other collection types. But the fact is, once you see this operation, or if you want to know if you have that operation available to you, he can just type you're very boat on, then use a dot operator on. Then you can just see if it is listed there for you in intelligence. All right, so I'm just going to run this program and then we're going to look at all of our examples printing to screen. All right, so just are quick run through. We printed our first sentence, which was formed in the full of the invariable. We looked at the three different types of concatenation, one with the placeholders on with plus signs and one with interpolation. We found the length off our first name variable value as well as the length of your sentence. We did a stringer place on all teas in my name, on all eyes in my surname on. Then we did a string comparison where both results would have had the fact that it's a different name for first and last. After comparing the first name value with the last name value on, then we did a string conversion for our variable for integer variable That represents our balance in closing this less than just going to say that this is just the touch off the surface that C sharps string manipulation capabilities are very extensive on it would require much longer explanation and video toe actually go through every single thing that it is capable off But that being said, I encourage you to experiment and just type boats. Create a string variable on then just use a dot operator to see all off the other functions that are available to you Seek in the severe dot Your variable Sorry, first name and then you say docked on then everything that is here to see a visual studio. Sorry has an explanation as to what it is for. All right. So I encourage you to just go ahead and experiment. Just type of sentence on a run each one of thes and printed a console on see what the outcome is.
14. Handling Exceptions: this video is surrounding exception handling in C Sharp. I have already created the project, and we're calling this one exception handling. Seek and pause right here. Create the project on resume once you have done that. Now, as an exception is a problem that arises during the execution. Off a program on git is not unique to see Sharp. It is pretty much in every single programming language. It's usually a response toe. You know, invalid inputs are undesirable conditions for an operation. So you may have encountered maybe at the A B M, where they asked you to insert your selection and you press one when you would know you campus one. But you press one on. Then if you see a big, nasty aero screen, then that means they're exception. Handling was probably poor because they didn't factor in that. This could potentially happen. Now, when you're writing your application, you are the one who is determining the input on the you know, the old put on do you want? More importantly, you know the desired inputs, so there are situations that you can foresee a potential Aaron. That's what exception handling is all about. You're going to write her court in what we call defensive manner so that you say that under these circumstances, going through a nasty era, don't exit the program but showed the user a nice air. A message to say, Hey, you know, you did something wrong when we could have been a potentially fatal operation for your program. So I've added some notes to this court file about the try, catch finally and throw keywords, all of which are keywords used. When we're talking about exception handling. Know the tribe basically is just a block that says, I'm going to try this operation. I'm going to try and do this the catch Basic callouses catch any aero like stop on check If there's any URL after you tried it, If there's any URL, then I want to take some action. The finally basically says that. Okay, after we tried it and maybe something was caught, maybe something was not caught. I'm still going to do this operation anyway, on then. The throw is a key word that we usedto end the execution so we sometimes in programming you'd try something on, Then you catch up articular kind off exception on they knew the programmer know that if this exception is ever caught, that is going to be, you know, detrimental to whatever operation is about happened. So you can manually throw the program. Haute, as in when you do throw it, will literally throw the era to the screen and exit the program. All right, so let's start this little adventure with a small program. I wrote it already sake in the spas and write it off yourself. So we're just accepting. Two numbers were doing console right line into your first number on. Then I'm parsing my interview in a different method, so we would have done convert dot. So in 32 now I'm doing it another way where there is actually a focus on inside of the into data type called in the Parse on. Then it works pretty much the same way. Except the converts to in 32 has a wider range and the dark parts. But for the purpose off this example, we can just use the dot parse. So we're asking the user for two numbers on, then we're going to do some division and then we're going to display the results. No, I'm going to run this program the first time on. Then we're going to see that 10 divided by two is going to be five. And that's perfect. And we know that OK or application works Now you're probably saying, OK, that's straightforward, That standard, that's what was expected. What is the moral off the story? So let's execute this again. And then this time I'm going to do something that I know is going to throw an exception. So I'm going to see 10 divided by zero on. Anybody who knows me knows that there is no division by zero. No, look at what visual studio does when I pressed Enter it throws what we call an exception. So you see exception on handle. They're telling us that this video, this lesson is called exception handling. And they're telling us that this exception was unhand aled system that divide by zero exception attempt to divide by zero, said Italy. What the exception was. So once again, they're certain situations are no, which your program is just going to free coat because those operations are illegal. All right, so even in programming, even though we're telling the computer to do it, the computer cannot divide by zero, so exception handling would allow us. And so in its in this state, we have to just continue in visual studio. And then it will get back to the States where we can start writing court again. I know we'll have to write, Ah, block that Say's let me handle the exception. So to start off, we try so I can just say Try on, then open with pearly braces on. Then I'm going to try and operation so I can just have try an empty because you know it is a visual studio is telling me that it's incomplete. So what am I going to try? I don't need to try the input because I know that they're going to put in put in put. But I'm going to try the operation, which is the division, right? Someone to try toe divide on. Then I am going to follow that up with a church. No, there are many exceptions, and we just saw a very specific one. We saw one. That's a system that divide by zero exception. You also have a system that I owe exception also have system that sorry time miss much exception you have vote off memory exception, have a number off exceptions. Now, I'm not telling you to remember all of them. I'm just saying that there are a lot So they all come from one base class called exception . All right, so we just type exception. We can literally just cover every base. And they were just going to say exception E. So I'll explain what that is. But when we'd say the keyword exception is like a base class that covers every type of exceptions. So no matter the type of exception that is caught, it will be caught by this class. No, you're probably wondering, OK, so why would I need to specify the type off exception? Well, there are times when, based on the type of exception that is caught, you want to do something different. So, like the divide by zero, you probably want toe from them to enter a bitter dino minuto or not denominated divisor. Sorry, Andi, if it is maybe, ah, system that I owe exception and you want to bring something else so you can actually have multiple catch block. So I'm just going toe feels the catch blocks that that's what the try catch looks like. So when people talk about exception handling or you hear somebody say try, catch, this is what they're talking about. You try an operation, you catch the arrow, and then you can actually judge many errors. I can have multiple catches on each catch would just have a specific exceptions. I can specify that. I want the division by zero exception to be caught in this catch block on. Then I can go on to say, Well, actually, the order in here matters, So I'm sorry about that. If I want to be specific, then I have toe do the specific ones before I catch exception. Because then if I did catch exception before the divide by zero, then catch Exceptional would have been catching any type off exception anyway. And it would never have gotten to divide by zero. Right? So that then the order much is well, like I was seeing. If we just have exception like Visual studio just indicated, then we're catching all off the exceptions possible? No, I did sales and to explain the purpose of the e. So I had e on that. I said E X or whatever. So this is pretty much just an object that represents the exception. So you notice when they exception message came up earlier, they turned some details that are name it. Had the content told us what the air awas. So all of those, you know, components all of those bits of information we can all collect them and sort of inside off some variable or some object called e on then that will allow us to, well, printout whatever we want. So let's say we tried this operation all right on. Then, after trying the operation, we caught the exception that said division by zero. So when I catch that exception, I can choose toe console that right line on print out an error message, illegal operations instead of giving that nasty era where it stopped the operation and started telling us about system that divide by zero. That this and that because that means really nothing to a user, you can give a more elegant Aramis is to say, Hey, man, you know, illegal operation. All right, so I'm going to try the operation, then print the results on Then I'm going to catch any arrow on, then I can say Finally, all right, console that right lines. I'll just talk. We console that right land and see the stopping and piece of that's end of program. So remember that the finally will execute regardless off the trying to catch operations. It's going to try this. If it's successful, that is going to do this. Finally, if it tries it and there is an error that it it catches, then it's going toe. Still do this family. So let's try that again. So we're going to do a good 1 10 on to on. Then you see results on, then end off programs. So it tried it and then it did the finally let's try that again. So this time I'm doing 10 on zero on, then you see illegal operations. So I gave me my error message on. Then it still did the finally someone toe kind off me. The error message, a bit more explicit knows I'm going to say illegal operation on Put a colon, Ondo. Let me just use interpolation because I am going to be printing the actual air. A message from the exception. So exception is generic. Once again, I don't know if it's a divide by zero our system that I owe. Sometimes you get system that I owe exceptions. They have 20 different eras inside of that exception type, All right, so sometimes it is good to know the exact error that is being thrown so that you can be better at debugging the application. But sometimes it's not deliberate. I'm being deliberate. No, but a lot of the times that we write code and we're not being deliberate with the arrows. I were probably missing something, so I'm going to just say e dots on. Then I can see that message. So eat up messages. The actual message attached to the exceptions to the expectation is that when the exception is thrown or cart rather where winter prints illegal Operation Colon and then the actual Aramis it. So let's try that again. So 10 and zero once more and then we're seeing here illegal Operation Colon on. Then they're giving us the message attached to the exception, which is that we attempted to divide by zero. So that's a nice we toe handle errors on. Then once again the final. It will still execute Onda as it will do what he needs to do. Know we did say there's a keyword called Throw on the throw on. I went out to put this through above this console, that right line notice. So it kind of becomes dolar. And if I hover over it, then it's going to be telling me that it is unreachable, ceases, unreachable, core detected because the fact is that the through is just going to kill the program right there. All right, so when I say try on, then catch Exception. If I say through, then it just kills the operation. There's no elegant error message on Let's even test to see if there is even a finally. So I really there is a finally, but let's just validate that. So 10 and zero wants more on. Then you see, it just goes straight to the throws. It just stops. The program comes to the throw on. I mean, visual studio is showing us what the problem is. But then, if you're console application is not executing inside of visual studio. Then there is. There's none of this screen for the user, all right, so it would just exit the program and they would never know white exits of the program. All right? And, well, there's the Unhand Aled exception. Something on this is the era message that would come up for that user if we used through. So once again exception handling Izabal, watching for particular types off errors on handling them elegantly.
15. Classes and Objects: this video is geared towards teaching you a boat. Object oriented programming and more specifically, classes, objects, methods and properties have already created the project and it is called classes and objects. Seek and pause here and go ahead and create this new project under your solution and resume when you are done. No, let's get started with what a class is on a class is a very, very important key word When talking about object oriented programming languages, I would have used that expression multiple times on did so. This is the explanation Now our class is a blueprint for a data type, right? So for a bit more context, we have data types. We have int we have string were bullion. We have char right on DSO. There are times when we probably need our own data type because we need to define something inside of four program that has unique properties. Andi, even a mixture off those other based data types that I just mentioned on DSO A class allows us to define our own data type, which can be a combination of thes other types and properties and methods that we want. All right, so that was a very unofficial explanation off it. Andi. I hope it made sense. But if it is still unclear as to what it is we're about to create a class on, I'm going to explain in detail everything as we go along. So let us say we wanted our own class called box. No, Lord, we already have a class. We have the key word class that tells us Well, this is a class and it's called Program. But I want another class that is not program, so I can't define a class or will. It is generally discouraged to the final class inside off another class. But I do need it inside off the name space. So inside off the curly braces for my name space I'm just going to create another class on this press enter twice on. Then I will say class, Andi, this class is going to be called box, so I want a class called box. I want a data type that, you know, looks like a box. Now I did say it's a blueprint. So when you're defining a class, you need to sit yourself. Okay. I've said I need a class. I need a new data type are given it a name. What does it usually look like? What are the properties? And when I say properties, they are the defining characteristics off this new data type. Well, I can tell you that a box is a three D object. Generally speaking, Onda box usually has length, breadth and height. Right, So we know three properties that this box will have. So to define properties, I will need the access modifier. So we looked at one in when we're doing methods, our called It's static. Well, there's another one. You have well public. You also have private. So in the case, off a class, we can define it as public. Then we give it a data type so the data type can be off another class or it can be off a base data type. So once again, this is me defining idea. Testified another class somewhere else I could use does a day at the time. No. However, I'm keeping it simple. So public on, then my data time for length would probably be something like double on. Then I'm going to call it link. Then I'm just going to say get comma set. All right on. That's pretty much what that looks like. So public, double length on then the getter and setter, meaning this is allowing me to retrieve the value on this is allowing me to set a value. All right, so I can get the value from my property called Length on Die can set a value. And the visual studio is kind of hinting at me that, you know, I'm violating the naming convention. So let me see what they're going to tell me on there, just suggesting that properties off classes should get capital letters. All right, so let's try that again. No, this someone to show you a keyboard shortcut when defining properties inside off a class in C sharp on visual studio, you can write the word prop on, then double top tub, and then he will kind off, you know, complete that line for you. So it's going to highlight the data type. I can express the right double and press top on, then move onto the next one, which is my property on this one. I wanted to see breath, and I'm using the suggested naming convention this time. Then I press enter on. Then it adds it to the property. All right, so what's tried it again? Prop Double tub on, then double on, then tub on. Then this one is going to be height for center on. We're done. All right. And then congratulations. You just defined your first user defined. Ate a type or your first class. Know that I have this user defined data type. What do I do with it? Although I use it. All right. So let us think about how we use regular data types and won the regular data type. We just specify or data type on. Then give it a name and then maybe give it a value. Right. So, like when we wanted a number, we would have said something like, Didn't that's our data type of name on. Then give it a value. True. No. We want to define box as a very bar. Want a variable off type box? So boxes a data time? No. And we want a variable that represents box. So, in orderto initialize a new variable or what we call an object. So I'm just going toe writes a comment here, create object off class type box. Then I'm going to say something like Box. So I'm just going to call it the same way we called. INT is the same way. I'm just calling the data type on. Then I'm going to give it a name, someone to give it common B box. So it's very case sensitive. If I defined class with a camera with a Capital B, then it's going to be seen differently as common B. But it could have been anything I could have said Box B Box B one the same naming convention that we would have used to go for and Holy name or variables would apply. Here are the only exception is that they can look. The case really matters a lot. So I have my data type box and notice that it's kind of cool accorded just like program. So that's a class. And then boxes call accorded the common be boxes Kalakota just like Nome. So that's my variable are in this situation. My object on that I'm going to initialize it toe a new instance off box, and this step is very important. All right, so without this step, then well, it would be it wouldn't work essentially. So if I just said box be on civic Colon visual studio would kind of accept it. But then nothing that we do afterwards would work. It would just throw errors. All right, So every time you're using a new class or a new user defined object are sorry and you'd user defined data type are, which is in the form for class that you would have created, then you need to initialize it by saying that class name, some object name is equal toe a new instance off that class name again on open and close braces. There are times you may see contents in the braces, but this is not one of those times, so I'm not went toe. Bring up that scenario just yet, and we'll just press ahead with our objective. So know that we have our box object or a box variable, and I'm going to start calling it object from No. One, right. I keep on calling variable sick and can Arana mental parallel, but it's really an object. Once it's off a class type on bits, no initialized, it's no on object. All right, so I can say box on then a very, very special operator that I know you seen quite a few times on the probably just never meant much to you. We have our full stop so box dot on then. Look at what the intelligence is doing. It's telling me that I have access toe Ah, property. You see that wrench? That range means property called breath. I have access to a property called height, a property called length all three off, which I just defined inside off my class. All right, so they're telling me that I can get and I can set, which means that I can print box that length if I need toe on. I can also say box that length is equal to some value Someone to do that, someone to set some value. So box that length So box that length is 10 on. Then I'm going to save box that with our sorry breath is equal to 15. And then I'm also going to say box docked height is equal to five. So I'm setting values here, right? So c sharp makes it very easy for you to create an object on then set values for these objects. So when I'm putting or assigning values, that is me setting when I am printing, that is me getting so I can actually do a console dots. Right line. All right, so you see that, Not operator again. And notice the color coding on the word console. It looks just like the color coding on box on on program on every other class that we would have called. So that means the creators off the language C sharp created a class and then this class on I know this because after the dot operator intelligence issue me everything that I can look at in the class, it has many properties, and the methods of the cube represents a method. Right? So have a method called right line. Also have a property called OTA property called era property. Called this a property called that my class only has three properties. This one has so many other properties that I mean, may never use in all honesty, have never used some of these things in here, but the tools are there. So I'm just trying to show you that when you're creating a class, it is up to you. This is a blueprint But then it gets initialized, and then everything becomes real because I can actually interact with the properties and methods you would have defined. So I went to see a console dot right line on. Then I would just want to print out box dimensions are box that length box that breadth Unbox start Height writes. I'm just printing but the violence. So, like I said, when you are trying to view the values you're really doing, I get. But when you're saying the values you're doing are set now, look at what happens if I remove the set from length. Andi. I removed the get from height. All right, so no, have a narrow because I'm trying to set a value on a property that only I lows me to get right. Yes, it's public, but it's saying that you're not a low toe place, a value inside this property on. Then it's giving me that arrow because it's saying, Oh, that's illegal. Its read only you can only retrieve the values, so it's not giving men. Irwin. I'm printing it because I'm only retrieving here. All right, on, then, just the same way for the the height I only put it to set. So, yes, I can put a value there, but I cannot retrieve it. All right? So I'm just pointing that all too. Because when you're going to be interacting with other classes, well, one, when you're defining your own classes, you're goingto be very careful if you know, if necessary to know what level off access you're giving toe. What property? So a human being probably is. Well, let's say you would meet any human being unless you give birth to this human being. You would have met them with the names so you don't just go ahead and set name. You don't set first name and sit last name, but you can get that information, right? So if you were ever to define a class that maybe you call person, that looks like a human being, you know, you're defending with properties off a human being on a property off a human being would be a first name. Another property would be a last name, right? You probably would have first name with only get on last name with only get all right. But then you probably have something else that can get full name on did you would never probably set, set put saris put a set on the first name on the last name are so I put but my sister and my getter on my my properties in my class And now I'm going to execute this program. So let me just quickly switch over the starts of program. All right? So are very simple program. Those printed or box dimensions are 10 15 and five, just like all we defined them. 10 15 5 on. Then we printed out over sentence. So we've been doing a lot off input or puts program. So you could actually modify this to receive the three numbers as input and then set the values and then do the print. So I'm going to do it. I wanted to pause this video right now and attempted to yourself on. Then you on pause and it will go through together. Arts have completed that exercise, So I put in some comments and Cannery airing the court. So it's always nice to have your declarations kind off one place. I mean, I do accept that some things you don't know what variable you need until you get to the point, but when you can just try and put all of them in one area on. Then I prompted for the three numbers, and then I set them on. Then I got them. All right, so let's just run that and CEO that works. So I'm getting the length and this is going to be a box that's 10 by 15 by five on. Then it's just going to print. The dimensions are 10 by 15 by by five. Know what if I wanted to do something with those values? What if my I wanted my box to tell me it's volume, so I know it's length annoys bridge. I know it's height, but then I wanted toe calculate the volume based on the properties. Know what I can do is define a method so I can define a function inside off my class and just for context notice we have the class called Program on. It has a function. So we went through ho to define functions, and I'm just going to use the same principles except this time inside off a class, a new class body, right, So the last off functions we defend them instead of the cast program. But I don't want these methods inside of program. I want them inside off my class called Box on This method is going toe return the volumes I went to call it, Get volume. Alright, someone to say public on then I wanted to return a value, Sir Mobile and its value returning. We have to specify a data type someone to say double on. Then I'm going to call it Get volume on then it is not taking any parameters and I'll explain why it's not taking parameters. It doesn't need to take parameters because it is a part off the class that has the properties. So as as a results, there are like siblings. The properties are siblings. They know about each other. The methods are siblings with the properties, so you can actually just reference them directly inside off the method. You don't have to pass in any parameters because they're all inside off that box. All right? So I mean, like I said, this is a data type definitions. So if I have a box that has a length with ah height, it's also the volume. All of those are still related to this one box so I can think of a ladder. You define the class, you give it properties. Maybe you gave it on method. Our tool phone shuns whatever it is, what they're all apart off. Anyone object off this box? All right, so I'm going to say and they noticed that the red line because it's value returns. I need to return on. Then I'm returning length times, bread, times, height. All right, so that's so we gets the volume off any three D objects, so know that I have this value. I can go back to my main function Or let me put it in the era where I'm getting the properties on, Then I'm just going to say into volumes. I did say it's good toe. Declare variables in one ear unless you you realize you needed somewhere else. Say, just a carrot. So into volume is going to be equal toe our object call box. When I say dot you're going to notice that I have get volume no available to me. So when I say get volume, it will mean that function cars, so when it's executing, it gets the values from our users on. Then it sets the values and then we get the volume. But their notice have on error here because saying I I'm returning a double, but I'm storing it inside off on int. So my bad that needs to be double volume is equal toe. Get volume on. Then what I'm going to do is say box volume is on. Then I'm just sprinting back the value that is inside off my variable called volume. All right, so let's go through this again. We have a box. We declared a data object called box were filling it with values on. Then we're d calculating. Ah, volume. Based on those values, no local need. This school looks imagine if you were to read all of those things one place and he can get messy. So inside off your class, you can have as many functions I could even say public. Um, maybe I have another function that gets the area. So I'm going to say, get area this time off any one side and then era is usually length by breath. So it's not the height writes. I have another function so I can do another one where I see a double area is equal to box that get area. So as many functions as I will need or want, I can define them inside off my class on then on on, you know, on a need need by need basis. I just declare my object, make my phone shown call on, then get the value and you can have void formations also. So it's not always Valley returns. I'm just in tow. Duplicate this line using control de on print out the area that I'm getting back. All right, so let's try and run this program, and I'm going to do something that I know is going to throw an error notice that I carelessly have been using double. So I'm in suggesting that my length, breadth and height can be double. However, I am only doing an into that Pires to the read line. So this is the arrow. If my length was 1.5 and I press enter, then I'm getting an unhand Aled exception error. All right, so we did talk about exception handling where it's good to wrap it up in the try catch to avoid these kinds of errors. But just for simplicity sake. I'm just going to change this. So So if we have in dark Pires, then guess what we also have. We have double dot pars art, and if we're doing a double dot parts, we also need to have the value that is being pirates going into a double. All right, So let's just change these old two doubles. Like I said, it's very strict on. Then we just change the motor double, so we have double N one doing a double lot pars on double, double, double, double, double on everything is no relative to doubles. So let's try this again. So the length is 1.5 on the breath is 3.4 on the height is night does some random dimensions. So no, I'm seeing that the dimensions are those three values the area or the volume Sorry is 45.9 , so that's 1.5 times 3.4 times nine on, then the area is 5.1 because that's 1.5 times 3.4. So, no, we have some data type that represents a box. But guess what? What if we had two boxes or more than one boxes are. We had a collection off boxes. The fact is that we can define as many boxes as we need. So I'm going to say this is box on. Then there's a second box that I need toe be aware of for work. Old dimensions for second see box. Oh, so there is nothing stopping me. So it's just like, Oh, we can have multiple integer variables, multiple string variables you can have multiple objects on. Spelling is defeating me. He can have multiple objects off the same data type as long as the names are not the same. All right, so I can have box box to box five bucks 10 now, imagine trying to fill in the dimensions off. Ah, we're host full off boxes. So you know that their 10 boxes and you have to write a program to allo them to enter the dimensions for 10 boxes. Then you're going tohave box box to box three box for box five and then you would have problems per box because then for box to I'm goingto have toe repeat all of these steps just to make sure that I'm getting for box, too. So there is an easier way. I'm just giving an idea of whole tedious it might get. But I am just saying that that is what at this point, we would have to do so I'm just going to proceed with box too. So we have the promise for devices for the box dimensions and we're sitting and printing the values are really sitting on calculating the virus word box. So I'm just going to duplicate all off this and I'm going toe do it again underneath, Right. But in this time, I'm going to change these old So I'm not going to really clear in one into an entry. So, like I said, if you have to re use a variable, you can actually just reference it by name after it has been declared. Right? So we declared it here. We're not be carrying together. We're just using it against I'm just making reference to it on. Then I'm changing these old till box one. So it's a box one box to Sorry. So we're changing these references. Auto box too on then. Once I'm done this, I'll just explain quickly exactly what is happening here on. Then I'm going toe duplicates all these print lines, We're all right where I'm going to be printing box, too. Dimensions on. Then notes I'm going to after over it. So I'm not going to do it over And what I'm going to show you that you can actually print directly from our return, right. So, upon to note, have suggested that you put the return value sermon, but that get volume is a phone, son that returns something. So when he returns that value I know it said in use a variable. The Stamatis returned the result of this calculation on Then that result of the calculations were to get stored inside volume. No, don't hear imprinting volume. And I'm printing air, which we know where for box. The first box. I'm going to remove those declarations for the second box. So I'm sitting. Is the heights the breath under the length. But then I'm printing box to ducts, and then I'm just going to call the function right there. Get volume on that. I'm going to print again. Box that sorry box, too. Dots get area. All right, So then I'm just showing you that you can print the variable you can do the return call. Get it. Store it in a very bland and manipulated variable which might be more desirable if you have . You know other things you need to do with the value. But then you can also just return it on. Have it print directly inside off your print operation. Right? So you have those two. Ah, And of course, you have to know when you're using which method. So let's run this program once again. Where have two boxes. So I have two boxes off off the data type that I just created. Call box. I'm going to print prompt to enter values for the first box on. Then I'm going to calculate the volume in the area that I went to prompt again the end of us for the second box. So I mean, I can modify the prompt to say for box one. All right, so I'll just do that. And then I repeat that for the problems for box, too. And then after getting the violence or box to, we're just setting the dimensions accordingly on. Then we're just going to print box one dimension box one area about so on volume and then box to box too on box too. So I'm just adding these so that we have some visual cues as to which values are for box one on which valleys are for box too. So let's run this and see holdout works. Alright, so I've already entered some of the values 56 and 34 dimensions for box one on. Then I'm going to say 54 on 10 for dimensions. So box to percenter, and then we can see here Box one dimensions are these three on then box two dimensions are those three and those are the calculations. They see that I'm still getting the area on the volume even though I'm just printing directly into the console. Right lined the return value. All right, So make sure you don't get confused by the book early breeze and that that we're making a function call inside of console that right line here. So when it gets to this line and it starts right in the text, he realizes that Oh, I have to make a phone shown call. Let me go to the fun shown on then that's going to return some value on then whatever it returns is what gets concatenation did in tow the string that is being written to the console. So, like I keep on saying there many ways to do things, I think that, you know, getting it back. Answering it in a variable is much cleaner, much more much easier to read than something looking like this. But once again, you have that method available to you. Now, as a final act, I'm going to display to you the best way to handle your classes. So I would have had to go through the painstaking explanation off off saying why we should not define another class inside off this crafts. Right? So we have class program and I had to go through, you know, quite a bit off record restructuring to make sure that I was not inside this curly brace. What, Inside off the name Space Girl? A breeze on dit is actually best practice. It least a much cleaner looking project files when and court underscored. In general, when you create a new file per class file, so notice that we have a class called program. We have a corresponding file in our project called Program. That's yes, which then means any class that you create, it is best practice to create a new file. Call it that class name dot CS on. Place your definition off a class inside that file. So I'm actually going to do that right now. So I'm going to take this class that we just created, and I'm going to cut it. So all of the court associated with the class understand to do control X, and I'm quoting it from inside me on. Then we forgive visitors to the a few seconds and everything is going to go here where it's going to be like, Oh, I have, ah definition for data type here. That doesn't exist. Ah, and they were going to say, visitors to do It's fine. We know what we're doing. So we're going to go over to our project classes and objects, right? Click. I then click. Either on. Then what we're doing is adding a new class. You see that class? So we click class on once again. Whatever the name of the class is, it is best practice to give the file that names. I'm going to call this glass box, so that is going to go ahead and create a new file. Andi, it's going to even go ahead as faras filling in the name of the class God box because, contextually, it's already naming the class. So I'm just going to replace this pre defined cord on notice it saying for references. And that's because just buy me creating that class on box. No properties or anything in my minute automatically detects that. Yet the data type is back, so I know what I'm doing. But then it has no properties. It has no function. So everything else is still here. Wire. So I'm just going to replace this block off cord with the class that I just created on. Then when I go back over to my mean than everything is fine. So once again, when we're about to create a class is actually better to just go to our project. Right click goto ad, click class give or class and name. So let's say I created a class called person, all right, and then do all of our definitions, all of our properties and I just feel though three properties quickly, I didn't want to bore you with writing them Second pause and write them off if you need toe . But their first name, last name and age on that I'm going to declare another one on this time. I'm using a different access modifier, and this one is going to be called private. So private would be salary for arguments. Exhaust double on Sallah re. All right, so I'm defining these as public on this as private Know when something is private, it means that nothing out said off the class Can Access City cannot directly access this property from the object. All right, so let me just put that into context for it. So I'm going to go back to program. I'm not going to do too many modifications because person is coming on the tail end off this lesson. But let's just see what happens. Someone to say person. I noticed it the text person, because it's in a class file. It's in the same name space, so they know about each other and their siblings. So person on then, as usual, after declare a new object off person, someone to see capital P person, that same of the class common P person that's name off the object is equal to a new instance off a person. All right, so I can say person the object on say dot on then intelligence is showing me exactly what I can access. I can access the age because that was public. I can access the first name and last name because those who are also public. But I cannot access the salary because it is private, right? So if I go as far as writing salary because I'm defying visual studio and see sharp and I'm going to say right, you know, I want to see salary, then is going to say that you cannot. It is not accessible because of its protection level. So because I said it's private, that's private information. I can't just go on and say I want to see salary. All right, so usually when you have private properties are private member feels inside off a class, then you me what you call getter function. So we're already defined that we can have a default, get a default set once we define our public, um, property. But then when it's private, you probably don't want to do it like that so you can actually do something like defining a function. And I'm adjusting to call this public on. Call it double on going to see get salary and then this public function that I should be able to access When I say person dot will return celery. So return celery. All right on then. What if I needed to sit the value for salary? Remember, it's private and it doesn't have any getter and setter, so usually they would actually create another function. And they call this one sit so you can see public double get salary and this is going to return. But then this one can be void because I don't need to return anything when I'm sitting the salary, however, I'm going to require that you tell me home with salary I'm sitting for this person. So I need a value from you. So I'm going to say send me a parameter on. I'm going to say give me celery with. Come on s on, then. No return, but I'm sitting. The capital s salary, which is my private member field toe, have the same value as salary that is coming in through the parameter. So salaries he got a salary, so Ah, good naming conviction. Actually, when it comes to private members is that use underscore on not capital letters. So the public accessible ones would have the capital letter. But once it's private to use an underscore and common case. So let me just change that boat on. I mean, you saw that it worked either way. So it's not about off it. Working versus not working is just a matter off. This is the standard. So somebody else comes along, looks at your core. They know exactly what they're looking at at a glance. All right, so private double salary that is our one private member field on. We need a function to get the value that is there, and we need a function to set of value in there. So back in the program, I can save person dot on I will only have access to any methods or properties that have the public identify. Ire are access modifier. Sorry. Ah, indicator on there. The clary son. So that's it for this? Very, very so that's it for this quick introduction to classes and objects in C sharp. I'm going to encourage you to just think off anything. Think off your pit on. Then the final class called Pit on. Then start writing all the different properties. Andi. I like to think of the properties as the knowns on the methods as the verbs. Right? So the known would be like something that is there is so a box is always in turbulent. It's always went after birth. It's always going to have a height, but then getting the volume and getting the area, or in the upper in the institution of the person, getting the salary or setting the salary or even getting full name. So let me just type that one. I want you to complete this string public string, get full name. So I want you to define this function toe, get this person's full name on. I wanted to share your court with me on. Then we can go through any difficulty you had together. So once again, the methods are more diverse because this is an action. Getting the full name is an action, but the name is always there. That's more like a note. All right, so that's a challenge for you. Let me know if you have any
16. Arrays and Lists: Hey, guys, welcome back in this video will be looking at hurries and collections in C sharp. No, I said Ari's and collections, because well, Array is the most widely used type of collection in most, if not all, languages. So by understanding honoree in C sharp, you can apply pretty much the same concepts to C plus plus Java and every other programming language. That being said, however, there are other kinds off collection data types that exist on today. Since we're using C Sharp, we're looking at our ease on. We'll also be looking at lists on. Then I'll give you a sneak peek at the others. But the reality is that they all kind of behave the same way. Andi at the end of the day is almost a month off preference, which one you will choose? No, to get started. I'm going to ask you to the spas right here. Go ahead and create your new project in your solution called Ari's and Collections. And once you've done that, then you can continue in the video. So let's take some time to define on area, and I just put in some visual guides in four of comments so that we know exactly what we'll be accomplishing as we go along. But Honore is a collection off objects off the same date attack. Or if you wanted a B word to use right there. You see, it's ah, homogeneous collection off data. So that really means that you can have, ah, bunch off numbers all directly related to each other, um, or a bunch of strings all directly related to each other. In all the advantage to using honoree instead off Terrible's like we've been using upon to this point would be that if I wanted a program to collect the grades for 10 students in a class, all right, I would have to declare 10 variables. That would be great one great to grade three through 10 to collect 10 grades. Instead of doing that, however, I can be clear on hurry for 10 are so going to get started with our first area by the clearing on a ready to store five grades for some students. All right, so, once again, if we wanted to store fibers than originally, would have been declaring five variables. Great. Long agreed to agree to grade for Grade five However, we can actually see the dates of type, which in this case would be something like int before starting grades into spine on. Then we would have square brackets that indicates that we want on a race. So we want an artery or collection off integers. All right, then we give our our A and name. So in this case, like I said, we're stirring grades, so int square bracket grades on, then we have to initialize it. So we say is equal toe a new int on then. Well, this is the section we're doing a fixed size ary, so we know that we want five grades. So inside of the square brackets, we're going to see five. All right on, then the Symbicort. And so you see that this initialization kind off looks a little bit like, Oh, we initialized our classes right into objects. Sorry. So we have the data type on the ari. The square brackets indicate that this injury So it looks just like a variable declaration . Really? Except we're initializing it like, Oh, we did it with the objects and where they can't classes. So we see, Give me a collection off integers Andi. I want upto five. Okay, so now that we have it declared, we can start assigning values and their, ah, few ways to do that. But we'll do that later on in this section where we assigned by this fix, Terry. But then you see that I have a fixed hurry section and then I have a variable size A resection. So this is when I know that I have five grades toe collect our you know, actually, I'm going to rearrange it, so I'm going to try and do each one in its own section. So I'm going to show you a photo initialize and I really don't necessarily know how many you're going tohave on the number centenarian should come in so that I can do with the fixed size one time and then the very besides after another points, but pressing I had when I don't necessarily know how many greats expect, right. So if you're building, this application may be to sell toe various schools. Some schools may have 10 students. Some have filed some of 20. You don't want to put in a fixed size because you can't go above the fixed size on If you go any less than the fixed size, then you're basically wasting space because is going toe reserve five spaces in memory for grades. So this time I'm just going to say int greens and I'm going to love this one grids one on DWI Remember I really asleep in square brackets, greens one on, then semi colon. So right here I'm seeing that I want on ary on by Not necessarily sure Homewood speaks. I'm going to need for this every but just know that any gunnery All right, so I'm just into diskette man Woman that me organize this. All right, So just did a quick organization off the comments. If you didn't rather commence, that's fine. I'm just doing it as a bizarre It's weak. And b Caris What activity were completing, at which point? So when we're dealing with an r a off fixed sighs, what happens once again is that we have to declare ah, home in space. We need Noto assign values to this area. Have two options. I can either see the Iranian, which is agreed on, then choose the subscript. So what happens is that by seeing five we have reserved five spaces in memory for this array or for the values that should going into this. Sorry. So then our addresses for the values would be what would cause sub scripts, Right? So eat subscript has, ah, speed. Ah, name on. They start from zero and they go upto one less than the size. All right, so administrator comments explaining that. So when you have five spaces, it means that you have addresses zero through four. So address zero is one. Then one is then on the second space than two is the third space threes. The fourth space on then for is the fifth space. So in Ari's zero is very, very, very valuable. All right, so, you know, in regular my few mean up cones eras a space, but in computer zero ver ver speeds. So, technically, if or mathematically if you have an area off size 10 then you're going to go from 0 to 9 or whatever size you declare. It is basically zero to n minus one where it is the size. All right, that's a little formula. All right, So I did another comment. If it is the size off theory, then you're Ari addresses are zero two and minus one. So once again, if if five is the size, then we have zero toe, five minutes, one, which is four. All right, So to assign the values, then I would just call each one by its specific address. So if I wanted to put something in the 1st 1 then I say grids, square brackets, putting zeros a subscript or the address on that I would see equals that say one. All right, so that's the first agreed on. Then I'm just going to duplicate this to speed it up so I can see grades subscript Juan subscript tool subscript three. So script four. All right on, then. I can use a sign the different values here. All right, so we have some students doing well and some doing party. That's fine. All right. So another way that you can assign values would be to see grades is equal to on. Then you have to declare that you are ascending a new hurry on, then use the curly braces. Sorry. A new injury for about the data types will have to say new on then the data type off the type off are really so off course, If I put a different data type here, I'm going to get an error because we already declared that breeds is on integer ari type. Right? So that's what we mean by its homogeneous in an area can be stirring strings on integers. All right. It has to be one at the time. So I would say grades on then you on, then the ins with the Kurt with the square races and then in the current breezes. Then I can spell loads the values. So if I wanted so script zero to have a particle of value which in this case, I would put 10 on the comma on the 20 comma 30 common 40 common 50 right to know I have 123 for five values and then they kind of implicitly assigned them to the appropriate address. So this is the first value it will go into subscript zero. So I'm sitting values here, and then I'm sitting by this here. What I'm really doing is overriding these values with this set up right here. All right, so I don't mean to confuse them to showing you that we could have done it this way on. We also could have initialized the ari that we write, so I can actually just comment. Well, whichever one you want to come in tow know the dancer would be in assigning an extra one. So if I had put in an extra value where they're putting an extra dress like I said breeds five on give it another value or or I sit coma 60 here. I said I have five addresses, but I'm assigning six values. 123456 are counting here. 123456 Now that is going to be a problem. And if I attempts toe, I'm going to execute this, unsure you the problem that we're going to run into, all right? So that it was executed. Then it's telling me that I have a system that index Altaf range exception. So they're telling me that this index was outside index is another word for subscript r address. Right. So this index was outside the bones off Ari. So once again, if you set it for five, then you're expected to have no more than five. You can have fewer, but no more than all right. So that is how strict Ari's are when they are assessing the data that is being put in. I'm just going to remove these values that well would throw an arrow. No, The other dimension toe assigning values on Ari would be the facts that you wouldn't sit down. Andi, you know, hired, called these violets like Scenario Give initially was that you're building a program to give to a school or some institution to be able to collect the values from, Well, the program is going toe alot the users that input the values into the Ari. All right, so we already know how to input already know how to open it? We already know Hold the store Values inside off are variable when we allowed to use it in port later, so it doesn't extend that knowledge a bit to the re's. So if I told an institution that if they have five students, I could give them a program. Some Chris Minto comment these lines outs. Those are no examples, but I'm giving them a program that they can that's will put in there five students. All right, so what I would want to do then is have something like a four loops. I could say four So inaudible. Hoops on a four loop is basically perfect for on Ari. So let's go through this together. So I'm going to say four int eyes, Equality zero, and remember that all arrays start counting at zero. Right? So I starts off with a value of zero on. Then I'm going to say I want this program or this foretell brother to run for less than five. So remember that if we say less than five, you will run from zero toe five minus one that's over familiar at 0 to 5, minus one. All right, on. Then I will say, I want I to increment after each run. So what this four loop is going to help us to do is go through each off the the sub scripts are the index's off our ari quite quickly, right? Parts I just wrote in to console that rat lines. Two very easily. Answer it. At this point, that shouldn't be very hard for you. Sick and pause the video right them off. But then what I'm doing here is just prompting uses some seeing enter student grades on that I'm just seeing console. Right. So this is a right line where we breathe the line and in the right is always happened for as many times as the loop is run. So if I run it right, no is going to ask you to enter the greed fighting. So you see inter student breeds and then the right Integrate, integrate, integrate, integrate off course. The rights doesn't breathe the line. So that's why I did that. However, um, oddity. Those sprints. Obviously, that's not what we're here for. We wanted to actually cause a low me to input. Agreed, maybe percenter. And then it proceeds, right? So what I would do here is one say that I want the grade with the index off whatever value I ha's So remember, if I started off a zero on all a restart off zero, then instead of seeing greed with, ah, hardcore that index here, like we did when we said grade zeros this period want is agree to is that block we can actually just they agreed with the subscript R index value off I. So that means whatever value I has at the time that the four group is running. That is the index inside off Ari that I want to manipulate on. The manipulation here would be toe except inputs. So off course you know that toe. Accept input. Really? Console read line. All right on, then. Because greed is an integer. All right? Oh, sorry. Not green, but breeds my butt. So breeds represents an integer We're getting that red line, so we we need toe part. So we say in that pars whatever value we get from the console. All right, so once again, we're just seeing integrated on. Then we pause for inputs. That's what this line is doing. So a pause for input to the console that re line when the person presses enter. Then we parse it to be an integer on. Then, having parse that we are sign it toe the corresponding index in the Ari based on the value that I haas. So this now lows are still run from zero to whatever value we put there, which, if its end and it's in minus one. All right, so let's try that told All right, So we see here that we printed the console that right line integrates and then where it integrated. This is the first run on. Then it's waiting. So I'm going to put 10. And when I presenter runs again No, it's counted up. No eyes one. Some put 20 and 30 then 40 on then 50. That's 11 sir. 2345 values on. Then when I press enter, we see that it stops running. All right, so that is how we dynamically except values into the area. And as you can see, it's kind off in a straight line. Zero to the end. It's not putting the 1st 1 Then the 3rd 1 Then maybe the 4th 1 didn't go back to the 2nd 1 No, it's from start to finish, right? No, that was just inserting the values into the greed. So by the end of this for you, all off the violence would have been stored inside off grades similar to that sports off, where it's not hard core. Any would be relative to whatever the user would have put in at the time. No. When the person turned their finished, I'm sure they would want to see the grades that they entered. So I'm just going toe. Copy this cord. All right. Because then the four who follows us toe go back through the area again. So instead, off writing inte grades, I'm going to see you get the grades you intern are for were Well, I can beat that on. Then I'm going to see instead off pausing for inputs, understand? To clean out this for lube altogether. And then what I'm going to say in this for is console docked rights line. And then I want to write the value that is inside off each subscript off my grades. Hurry. All right, so then each line is just going to print. Okay? Whatever value I has at the time, that's the values go into print. So let's try that again. All right? So normal to enter the grades. And I'm going to be a bits more big this time, so you can see I'm not rigging it, so I'm putting in random greets 50 84th 6 30 52 on 30 on. Then we print all the greats of the grades you entered are in the exact order that they were entered, right for all five. No, if you don't write your for loop properly and Then you end up trying to put in an extra great here in the area of five or the fixed size ary. Then you will give you one of those boats off. Bones are safe. You're seeing on a lot of bones errors. It means that you are trans reference and index that does not exist based on your declaration off your Ari. All right, so now that we looked at, asshole, we deal with the fixed size Ari, let us take a look at how we deal with the variable Sizer. So they're variables mean that they're prone to change or they're basically unknown until a certain situation. So when we say the variable size, it means that I'm giving this program toe school that I don't know the number off students they have that don't know the number of grades that after input. So I can't be as rigid as seeing five right or 10 or 100. I don't want to say 100,000 when they will have 50 students on. Then the computer crashes under the pressure when it really didn't have toe. Right? So what I'm doing here is just saying that, OK, am I know I need honoree. I don't know what size it's going to be, but then later on, you know, based on the size are based on values that are put in. Then I can make a determination. So the Ari Declaration are started. The area assignments value assignment in this situation with more model. This one right, we're let me just copy it and paste it so that we can see it within its context We're talking about reads one right, so because we didn't give it a size Initially, I can see grades one is equal to Onda. Whatever is in the curly brace, whatever number off values I put in, it will accept that as its sights. If I put in five, then it knows it's an aerial. Satisfied if I put in 10 all right, so I just put in five more values. Looks cheap, but let's just work with it then It knows that its size is now 10. So that's why we call it the variable size are re right? No, unfortunately, based on the nature off these dynamically sized arrays, um, the based on the strict nature of our is Actually it is kind of difficult to actually dynamically assign values. So after you've defined it, city they define it here in this land where I would have said is equal to new into with all of this, or I would define its here. But then, you know, putting it in a for loop on populating like that, it's kind off more difficult. So, actually see, Shar gives us other collection types, and one such time is a list. All right, so I'm going to use this opportunity no, to show you the list. So I'm really actually printing the re's in the variable are printing the value. Sorry. In the very, very is very similar semi. Just do that quickly where? I'm just going to take this, um, court from the previous Sorry on. Then what we can do is actually say something like the Iranian, which is grades one. Then I can see it docked on, then see length. Right. So, by seeing grades one dots length, I am saying that I am not sure how many values are here. But whatever number off buddies there are, which is the picture by the value length, right? So if you just over it. You say it gets the total number off elements in the dam in all the dimensions off the ari . All right, so you get the length off the entire area on, then you run from zero up until the counter is less than the length off the area. And so lengthier acts the same way as well. If we had defined the size so because it didn't find a size or guarantees off the number of eyes you put there to find out how many are there, you just see that links, All right. But once again, the weakness here is that I had to pre define these values on. That is not necessarily the best way to do it. So we have another bit of time, and it's called a list or another collection time, and it's called a list. So firstly, we're going to do if the clear a list. So a list declaration is very similar. Except there no square brackets, I would say, least on. Then little see that I want a list off string. So this time I want to be storing the names off the students in the school seconds. A list on in the data type, which is string. All right on give It's the names A list names. There's a list of type string names is equal to on. Then we just initialize it the way that we know how. Least string so you'll see a little nuances with the initialization is based on the data types and the collection types that we're dealing with. But, you know, as you practice more and more, you'll get used to the different four months. No, you notice a type of red line here, so after include another libraries. So by hovering, clicking the light bulb, I will get system, not collections that generic. So when I click that you will include it on. Then we can use the list data type. Alright, Noto added to a list. So this is the clearing the list toe add values to the list. All right, let me just put in the comment at values toe assist. In order to do that, we simply just say names, which is the name of the list dot on. Then when we say that we see that we have a bunch off, you know, functions available to us, we can find we can get the cones. So Khan told me in that whatever, it's just that length. So Collins does the same thing as length because, as you can see, I don't know how many values will be in the list. So the list is also of variable size. We don't know. It could be one. It could be a few. It could be not right. So control tell us that. But add something. We simply do the name of the list, which is names that had All right on. Then, when we opened the braces, we'll see that it's a function that is expecting a parameter off the data type that the list is representing. So the list is off type string, so it is expecting string some string value. So I can just hired code a string there if I want, so I can see name. And I just put my name as the 1st 1 all right, but then, in the same way we can actually do Ah, loop that will allow us to add something from the console now in this situation because it is not counter control or we don't know how many we want to add or not I wouldn't want his afford because afford goes upto a limit at any point so I could use a while Loop on dso. What I will do is say something like Ah, define a variable called name on initialized to a blank string. So I had some variables up here that I was using for something else. He can ignore them in case you're concerned about what they were for. That's fine. I removed them so that they don't confuse you. But right now I'm just going to define a variable called need. So the purpose off name is the store. The value that the user is going to enter into the console, right? Uhm, I'm going to set the escape character to be like negative one. All right, so that means I'm going to use a while loop to say a while. The value off name is not our name on. I'm going to use up some string manipulation knows the name is a string. So it has a name that I can get a bunch of things about it, right? So I can see if name that equals on. Then I'm just going toe give it the values. So I want to say if name that equals negative one. Meaning if the value in name equals negative one. Um, sorry. Well, it is not equal dated one. So then we just do that. All right, so this is a bullion, right? So this is going to say, is name equal to the value negative one, and then I'm seeing while that is not true, then I want to continue the execution. Right. So let me just put my prompt. This bar this scored instead of writing it all vote on. I'm just going to see enter names. All right, on then. Below. I'm going to say name is equal to console dots. Read line. Right. So I'm allowing the user to enter and need into the console, um, value into the console, and I'm storing it in the name variable Right on. Then I'm going to see if on which actually copy this logic. So this is called nesting? No, this is where you have one control structure hosing another one. So you have a loop, which is the wild lupine of any statement inside the loops, they have nested an if statement inside a while on if statements and while act on conditions, I'm going to use the same condition, right? So if name is not equal to the value of negative one, then I want to add the value that was entered toe the least off names, right? So we've added one name so far. So this one is just going to be hard coded. But then dynamically, I want to say that names, which is my list, not add on. Then the value in passing in is the string value that was entered into the name variable. All right, so it's going to do that while name is not equal to negative one. So that means on the first turn, if I enter Emily, then it's this is going to invite it the follows our true rather So we add amulet. And afterwards, if I say negative one, then it will skip over this, and then it will come back and realize that violated the condition on then is it aren't on then The same way that we could print values in the array is the same way that we can print values in the list. So they're actually quite a few ways one will be using a for loop. Right? So would saying names, not cones instead. So I'm seeing that on the mucus changes salt because I'm just boring courts. I just want to make sure I'm consistent. So the students student names you entered are on then for I. So the seals subscript rules apply toe lists, right? So I can actually see names on, then call it by its exact index. So once something goes into the list, it doesn't index just like it would have had in the Ari. All right, So I can use a for loop to go through each index for the cones or the size off the list or the number off items in the list on. Then it would just iterate through that. There's also another way called A for each. Right. So this is probably what you see more often than not where you say for each on, then what you're saying this is I want each item in the list or each item in the collection . So what? I would say something like each item is expected to be a stream. So after the clear, um, the item as the data type that I'm expecting for from the string, right. If I'm not so sure, I can actually use my keyword vier here. So I've been skirting around using fire, and that's fine. But you can use var, or you can use the strong type. That's no problem. So in this case, I use a strong time just the travel wind home. But I'm seeing for each string on. Then I'm calling it item rights for each string. Item in the list on the list here is called names, Right? So for each one off them, I want to console. I mean a copy that one Emirates told Console that right line on. Then I would print the item. Right. So what happens with this move is that each time it goes through, it will take whatever is inside off names, store it in item on. Then you would be able to print it to the screen on. Then, when it runs out off, all off the items inside, it just exits the loop by itself. All right, so this one is using the for loop. France on this one is using the for each loop. All right, so That's the difference between lists on verbal. So when you try to have autonomy, sorry lists on Barry's. So when you try to have a dynamic curry, it might not. Work holds best based on its nature. It's very strict, like I said before. But then when you have a need for a dynamic collection, you can always call on the list on. Then you have other data types you have innumerable. You have a quarry obl e of my collection, I observed. We have a bunch off them on. The reality is that if you want complex data types like a class so we looked at classes and objects. If you are a class that you needed to store the list off objects off that class, then you could use the list. All right, so just added some comments just annotate what each block off good is doing. Now what we're going to do is run, and then we run through each and every one off them. So the dynamic area fix Terry on the list. All right, so we're starting off with our fix story off five grades, so we know that this is only going to run for five, Uh, data also compressing space are not enter. Right. So that would also be a problem for Chris Piece are putting some other string. That is not, you know, can be deciphered as a number. Then it will cause a problem. But we know the start, right? So just putting in five grids, and then it shows you that. Okay. The five greed, the greed you entered are on the grades. You entered our on. Okay, I'm seeing an error here where it is seeing that I haven't Index wrote off Bones, Earl. So let me stop this on DSI. Exactly what went wrong? All right, so I see my mistakes. I was reprinting greed. So what happened is that I have the length off grades one for the for loop. But then I didn't change this from grades degrees one. So that's my mistake. If you hit that arrow, apologize. It's greats. One for that. Hurry. All right, so let's try that again. All right? So we proceed to enter our five breeds. All right, so here we see, we print reprinted our five grades. That's good on. Then we printed Oh, it's the 10 grades from the dynamic curry right on then it. It's the problems where it's asking us to enter names. Alright, someone to enter a name and it's Emily. Uhm, I'm going to see Rauenzahn on the fin re Andi want more than he's earned. When I press enter and I write negative one as my value, then it exits the look right? So negative one. It was the exit character on here. We're reprinting all off the names in the list. Remember Trip or we added that one manually, right? But then all the others we entered as we went along on then This is with the for each loop and you can see that the results are pretty much the same. But for me, the sin tops for the four. Each is just much easier to write. All right, so that's basically with with the lists and collections. That's, ah, broad idea. When you get more advanced, you see that you can actually use, um collections and more so lists with bigger data types, more complex data types on. Don't overwhelm being this. Listen, but as you go along, you will get more advanced
17. Create a Winforms Project: in this video will be setting up a C sharp wind forms application. Already have visual studio open and to get started. What we will do is go ahead, create a new project and from the templates were looking for wind forms. I can just search quickly and look for wind forms on. Then I want a wind forms or windows forms up dot net framework. So that's one we're working with today. So I select that template click next. Then I give my project the name and this one is going to be my first win forms are you know , I'm not going to do it. That's simply what I'm going to do is simulate a real world application. So let's say we wanted to build a forms to support a car rental business. I'm going to see car in tow. So that is our project. Name on. Then that will be our solution name. Also, let me just correct my spelling here. Car rental up on. We can leave it at the dotnet framework so you can see that you have a few versions of them . A dotnet framework. If you have installed visual studio out of the box. You probably have up to 4.72 Or maybe you have 4.8. Any off those work? What I leave behind at 4.7 point 21 you can put your so that version also so that we're on the same beach on then I can go ahead and click create when this project is created. Then you see something looking like this and I call this the canvas Cities Off Farm upon which we will be putting visual elements. But before we get into all of that, let's take some time to appreciate the interface on Look at the more important parts. So if we look in our solution explorer on minus the left years might be to the right by default. But I have my results to your set up that minus to the left on If you look in there, you see her solution. You see your project. But then your project fires really comprise the farm one dot CS on program dot CS No form one dot CS represents this visual representation that you're seeing here. So this is actually the form ones. If I close this by any accident, you might, of course, the top if you double click form one that says, that is what is going to get along. So this is your form. Each form that you're going to add is going to look like this until you start putting things on that farm next important files program, that's yes. I can just click it so we can see inside, and then you see that it looks just like a console program. The console programs come with a static class program by default the name space, which is the folder that it's in on then the static avoid mean except this one doesn't take any arguments. But if you're not familiar with consul programming than that, so problem. The most important thing to note is that you don't want to be editing this file right, so program, not CS. Later on, when you know what you're doing, you can edit it, and usually you'd be editing this fire, this line and beyond. But the things that are here, these functions are initialize er's Onda. I'm not going to spend time telling exactly what they're initializing this more. Please don't comment or delete them on. Then this one is what actually says what should be launched when the program launches. So we have form one That's years. It is seeing launch a new instance off farm one on. When it does that, it will launch our form that we are designing. All right, so we're going to perform a quick activity. We're just with the start this application, we haven't made any changes. We just have it here on. We're just going to click. Start on. DWI will see this window lawn. So you see this window will launch. And if I hold it up side by side, that canvas You see that? Well, the canvases empty. My new window is empty heart. So that is what wind forms looks like. At least out of the box. No, I just want to point out mistake that I have seen people make over the years and then they get all they spend time debugging something are trying to solve a problem that they didn't really have. So it's a common mistake to minimize this window inadvertently on. Then you're trying to do an operation and trying to change for your tryingto operate some way somehow and anything. Visual studio is not working because it's still indeed bold mood. So when you click started goes into deep both Mordor seeing all these diagnostics and OK, maybe you're new to visual studio. Those didn't stand out to you, But then you're tryingto, you know, change the court and it's not changing any think that visual studio is in an er state? What happens is that as long as you're in people more, you can't make court changes. So if your visual studio looks like this, even though the window is not up on the screen, then you're still in debug mode. So you need to make sure you press stop or when you close the window. So I minimized it. Just No. But if I close it as unlikely, the X then it exits. Debug boats are common. Mistake is that people forget, you know, that launches tested on then they forget toe close it on. Then they go back to results today and trying to make whatever adjustments we need to make and they can't and they think that it's an Arab, but there really still in the bubbles. I'm just pointing that out in case you know, when you're going along you so that you can be aware off that common mistake Now, the next most important part off this wind forms development set off features would be the toolbox. So I have my toolbox pain kind off, um UNP in the Orwell ducts to the side here. If you don't see a toolbox pain, then you can always go to view on. Then you just look for two box are at their stool box, or you can memorize that keyboard shark. What's if you want? But the fact is, it's there. If you don't see readily, just go to view on click to Box and large it. If you close it by accident, you go to view, trick to box and launch it. All right, so at any point, if I have a pain that you do in tough, you simply need to go to view, find that pain and open it. So let's start looking at toolbox so in toolbar to see that there some categories off controls on for our activity today. I want to stay focused on the common control, so if you just, you know, expand common controls and take a while and looking and pauses video and just take a while and look through the different controls. You can pause it right here and do that, but I'm going to continue on the first thing that I would want to do when somebody launches my application is let them know what application they have launched. I mean, I'm sure they went and click DoubleClick the application to launch it. But then you still want a visual representation, right? So I would bring over a label. All right, So win forms development at least below the interfaces. Dragon Trump's so I can drag over the label, place it somewhere on. For some reason I get this error, you might not. If you do, they need a slick. Okay, but it was still allowed to proceed. I'm not entirely sure where I get that Arab, but I'll be getting it on me. Need to check the my insulation of visual studio, but pushing, pushing on. We look at the label here on, then we want toe. You know, we see the label. It's not ideal. It saves label one. Um, the text this tiny and so on, and then your first instinct might be okay. I need toe this text, make it bigger or change the takes that's in it. On your second instinct might have you double click on. Then you double clicked on. Then you ended up in the sea Sharp court files. So this is what I call the court behind. So now I want to explain two things to you on I You know, this is just to help you be careful when you're doing these activities. So I'm going to go back to the design on. Then what I'm going to show you is that when you double click our control a visual element what's what is happening is that it is going to generate a method stub on it is going to generate, in this case, a quick event. So a quick event means that when I click this label, this corn is going to fire. No, I don't need any quick event on my label, Really. And truly, I don't need anything happen when this Liveris clicked. But I'm just pointing that out because when you double click these visual elements, all you're doing is generating method stops so on once again, the reason for you having double clicked, it would be toe change its properties. So we want to change the properties. We want to change the text that's in there. I want to change, you know, the whatever it is about it, we want to change. So to do that we're after, right. Click it on both properties cause once again, when we double click, it will just generate a method you will generate some event on. That's not necessarily what we want. All right, So when you right click the element in this case, the label on then I goto properties. Then it will launch another pin that my pain was there already. But for you it may not have been visible, but it will launch a pain on then. This pain allows us to change the properties off this visual elements. Let me take this time to explain to you that everything in C sharp is a class. The very visual elements that you're dragging over in from the toolbox there listed here, the button is a class, but when you drag it over, you've instance she ate it and object off this class. So in the context, off the label label is a c sharp class. You drag it over, you know, have on object called label one. And this object has properties that can be changed on these properties are all encapsulated inside off the object. Andi, by extension, the class type. All right, So I have this object here on my win forms called label one. And it's off the type are the class type label. And then I'm going to change the properties so I can change the funds. So I hope you pause the video on. Look through all the possible properties that could be changed. There. Quite a few have not want to go through every single one. I'm just with the focus on the one that is important, which is fund. So I want to change the funds. I expand the funds property I see name that would be the type of funds I can change that to . Maybe Well, you may have other funds. Are I have funds that you don't have, but I'm going to make some changes so you can style it. How you see fit. I'm just changing it out. So I changed the name a ginger size, and as you can see, that the changes are being revealed real time on. Then I'm going to change the text that's being displayed because I wanted not to say label one, but to say something like, Ah, car or in tow system. All right, on, then current. Our system goes off so I can just pull it back in. I wanted to be sent. I like that. You can see it's more well, it feels all the label continous. I'm going to drag it to the center, off the form itself. On there. I have my label car into our system. All right, so I took the label. Andi, I change the property that I wanted. I know I can see the manifestation off those changes, All right, so let's start dragging over some actual form elements. And if you have any experience building forms you know in Web applications, then it's the same concept. You have a form you have controls that allow the user to enter information. Andi, off course based on visual element to use. You are controlling the type of information that you're expecting from the user on. Then ultimately will a submit. But so for a car rental system will be collecting the customer name the dates that the rental is being me. The date it is supposed the car supposed to return on the type of car that is being rented on. Then we'll ever submit, but nice and simple. But we are using a few form elements and it along the way. We'll see. Exactly whole. Each one works. So let's start with the customer names. So I'm going to drug over the text box for that. So I see a text box, and that, I think, is the most ideal control. And there's a little air again. But I think that the textbooks is the most ideal control to, you know, collect the customer. Name the name of the customer who is taking the car. All right. No, I just dragged over the text box. I have it on the form, but I don't know if if if I come back tomorrow I made for I may forget why have the textbooks there if somebody comes, sees application? It's a random text box. They don't know what information there I'm expecting from them. So it is prudent of us to make sure that we have a label above each control. Right? So I have that label on going to change the properties. So this labor is going to see costumer name Bryant on. Then it is letting you know that. Well, this is a visual cue as to what this text boxes for, right? So for each control, we have ah, label that is going to kind of signal to the user. Hey, this information I'm required from you for this control are so where? Of course, the more name. And if you want to change other properties, they maybe I want the fun to be a bit bigger. So I want this to be 12. All right, on, then you can see that. So it was kind of hedging over the textbook self force industry. Position it so that everything looks good and uniformed. Alright, so have customer name on then. We have been in date outside chose dating and data. What are the dates that cars being are interred in the data just returned? Because I want us to look at the date time picker. Um, so I drugged that over on. Then I put it where I want to go on and this one. No will actually give us a calendar, so I'm just going to expand this. They can see the full dating there on copy and pieces of beautiful things. So I want to calendar controls one for the dates. It's bigger entered, and one for the data is being returned. So I can just copy and paste. I just use control C control V after selecting it. And there we go. So we have to on then. The label already have this label. It has the fun size I want instead of dragging over brand new label on going through the properties every time. I'm just going to copy this label twice. All right, So copy and paste on position. So that's that one. This one on. Then I can restrict the label on change the text. All right. So I'm just helping you along to be efficient in this, because it can be time consuming when you're going to the toolbox and dragging over every time, changing properties every time. So if you can just get comfortable, realize copying on basting is not such a bad thing. And you just know what to change. Then eventually you become a master. All right, so we're changing this toe date or in Ted. Andi, I'm changing this one toe. Be it returned. So the texts is the it returned. All right, The next thing that we're doing is ah, list off the type off cars and one that is being printed. So for that, I'm going to use a combo box so that drop don't. This is probably what you using html and that's fine in wind forms. It's called a combo box. They don't call it a dropped only start. But then you may see other lists because you have the list box. You also have the list view. So well, the list box is probably the more parallel wants a combo box because this is a drop down, and this is just that area with at least in it. So I'm going to choose the combo box, since that's what you're probably more familiar with. Both as a programmer are a web programmer, Andi. I did to the user off any application. So this one looks similar to ah, you know, text box. Except you see that little don't arrow there, which suggests that it can be a drop down list, Understand? Expand this on. I'm wanted. Make sure that all my controls kind of have the same withs. Everything looks, you know, uniform. All right, I'm going to copy one off my existing labels. Move it down on, then change the text. So this one is type off car, all right? No, I skipped this step earlier, and I just show, you know, So we need to kind of text box. You're going to see this is the black ira appear at the top. So let's say this is a text box. It's malt. It's single line, and you're probably wondering, OK, how do I make it longer are hold. I make it taller because you notice can only adjust the which only left to right. So if you click that little black arrow, you can make it multi line, which then makes it. You know, if you wanted to put in a paragraph or facility the interest inter off a paragraph, um, amount of text, then you can do multi line. So this would be perfect for, like, comments or something like that, but for cost of a name, I don't need a tall Oh, I don't need a paragraph looking structure. I just need a text box from lifter rides. So in the same way, when we click the combo box, you see that little black arrow on When we click it, it will, as it will allow us to say edit items. So I'm going to click edit items on. Then I'm going to fill it with the types off cars that I expect to show up when we select the drop down box. All right, so the types off course could be Honda. Ah, Toyota, Andi on a very popular car back here back home was Lotte on. We also had the bogey. Alright, So Honda Toyota lot and bogey. Those are the cars that we have in our our entire system on. Then toe complete this exercise, we are going to have a submit buttons. So I go back to my toolbox, get my button, drug it over whether my error, but it's on the screen. I can meet this button as big on ball as I want. I mean, I can resize it. I can go back to properties and change the funds. So I'm going to change this fun to look just like the funds at the top. That's the mature, empty script capitals. And the size off the text is going to be some of that. 25. All right on, then. I don't want it to say, but one. That's okay. So I wanted to see Submit. So this is going to be the submit button. So let's just take a look at the form. We have the customer name where the data intend at the data return on the type off car. So let's just click Start, which is going to save all the changes. Compile everything on, then load off. All right. And here we are, so ever car rental system on. I can test the torch. All right, So I can enter the customer name. I can use my date picker and select the date. That looks really cool. So you see, I got a date picker with very little effort. If you do a program, you know that that is not that easy on the type of car the drop down on I can select. But then you notice that type of car That combo box allows me the type. It's not on auto complete It's just the behavior off it. It allows me the actually type in a value that might not have been in the list, which is not necessarily idea because the point of a drop down list is that you don't want . You want to control the person, see selection. Let's allo them toe enter in the end, anything on? Then we click the submit button, and we see that well, even though it looked good in the design, the rendering doesn't look that we write. Some went after modified, maybe the width so that the T doesn't get chopped off, so I can't disclose that on. Then we go back, just the submit. All right, on, then, what I'm going to do with type off Karnow the combo box, and we go back to our property. So if you lost the properties, you can always distracted the control freak properties on. Then what we're going to do is change the behavior, so the drop down style has three more days. It's as simple when I click simply, you notice that it no longer looks like I dropped down. Oh, it looks just like a text box. That's not what we want. So I'm going to go back and change the changes to the other style A soon as I can find it. All right. And I'm not finding because I don't have this. Have the farm selected. So you have to make sure you're selecting the correct control when you're looking over by the properties are it's so simple. Drop down and then drop down lists. No, actually looks more like what I'm expecting. So it looks not. It looks unedited ble from the design on. I'm sure that when we run again, it will be just a drop down list that doesn't allow me to type anything in there. All right, so let's just click start again on take a look. All right, so this looks a bit better. And also somebody showing properly on type of car is giving me the list without allowing me to type anything in there. All right, so if I feel out this form Andi, I select my type off car and then I click submits. My expectation is that something is going to happen. It's going to give me some feedback, letting me know that I submitted something. So that is where events. Programming comes in, all right. So we did discuss earlier that when we inadvertently double clicked the label and by extension, dual double might double click any other control it will actually generate on method on that's That's not the way I want you to do it. I want to be very are far more deliberate when generating, um events so that you don't run into problems later on. So I'm going to show you one era that might that you can, you know, prepare for when you have on events that you didn't necessarily plan for on. Then you try to do a cleanup. I you're going to get this arrow on. Then I'm going to cut the video here, and in the next video, we're going to start looking at whole toe deliberately create events that actually do stuff . So when you're in the court behind, I call it the core behind. So let's say we didn't have it up on. I wanted to get to the court file for the form one that CS I can right click on the form directly, pretty much anywhere in the interface here in the design on I will see the option. That's his view. Quotes it directly caress of you code, if you do it over there are produced on the Farm View Court is going to appear. So when I click view court, it brings up that court file non, the less I know you don't want to edit much in this file just yet. As it stands, the only thing that's really, really important, or rather everything here, is important. But this method stop because once again, this is the method, so that was generated as a quick event on the label. One. So that means when I click this, this method is going toe fire. It's empty, so clicking it won't have any effect. But then, obviously doesn't make sense. I have these empty methods. It makes according messy and harder to maintain as you go along on. So I would clean it up. So I made it. I made the mistake. I'm going to clean tops. I delete it all right now, here is the problem that you might run into when deleting that, and if you delete anything, make sure that's all you delete. Everything else here should stay the same way. Public partial class. Form one That's name off the farm on the court file on it is inheriting from a base class called form. And then we have a constructor on that is just running this initialized component. If you don't, don't remove this. If you remove anything else, please don't remove these lines that are there already. All right, so I removed the click event on. Then I click start, and then we're going to get an error. All right, so it's sending us. There were built errors. Do you want to go to the last successful built? To me, that doesn't make sense to do because I want to see their I don't want the continent using the program in its previous state. So I'm going to click No. And then the ERA list is going to tell me that it does not contain a definition for something, something, something. So that's a mournful. But when I double click, it is going to bring me to the airline on what it is saying to me, No, this is the designer dot CS file. So this airline is enough file that we did not right. We haven't been authoring any quote already does your gun drop butts the engine? The whole development studio is generating this score for us. So each control that we drugged over is represented here in court. All right, on, then, what it is What it is saying is that we had created a click event on Associate it with label one Andi. So it's looking for that given toward that function, that's a function I just deleted. So we all should know. But I know that are, If you don't, that's fine. Whenever you have a function and you're calling that function, if the function does not exist, then you're going to get an error. So you have to define a function on. Then you can call set functions. So we had the function definition. It was there, we deleted it. But we still have the phone. Sean, call in the Arctic generated cord. So what? We can do it. Then you just remove this line because really, we don't need to call the function because you just deleted it on. Then, by making that adjustment, I can click start again on then I'm in business. So that's just another thing I wanted to help you through because maybe you would have generated some click events are some events that you didn't necessarily wanted it by accident, and that's fine. That's understandable. But then you remove them and then you're getting the errors. Are not sure ho to go through the eras. That is whole. So this is us designing Form one in next video will be looking at renaming our controls and the importance of doing so on hold. We will actually collect the information that was input into the form.
18. Build A Form and : you guys were continuing the development off our car rental wind forms application on were no at the stage where we want to actually start collecting the information from the form upon. Submit. No. I'm going to use this about a minute to explain a very important concept that can save you a lot of time in your development activity. And this is renaming your controls. So when we're designing this form, we went to the toolbox. We dragged over the controls on replace them. We wanted we changed a few properties. One property we did not change was the name. So if we click on the text box, we would see that it has a name, property, and every control that you're going to click on has a name, property and the name here is six bucks. One No. By default, it's going to always be counting. So if this is the first text box, it's going to be textbooks won. The second text box is going to be textbooks to the 3rd 4th etcetera etcetera. That is not idea when you have a few text boxes because you want to know which text box exactly has the customer name. So by leaving them as the default name textbooks on textbooks toe, etcetera. Every time you're going to want to know the information, they're going to have to come back over, click on the text box, look at the name and say, Oh, yeah, that's the one I want. So I'm going to give you a little technique, not his best practice. Actually, that will help you to reduce the amount off investigation on do you know, time you spend trying to find that the control you want on that is simply to change the name. So I have a simple convention where I use a prefix to detective determined or toe the picked rather the type off control that is being referenced on. Then I give it a name that is taking toe the information that it's supposed tohave. So let's do the text box. So I have the text box here that is collecting customer name. So my ideal name for this is not text box one, but it would be something like T B, which is short for text box. Or it could say txt, whatever it is. But some prefix that says this is ah takes the box, right? So I'm seeing TB customer, customer me. All right. Now that this is me naming the objects, remember I said that each control is an object. So by saying TV customer name and not text box one, This object is no called TB customer name the same rules that apply toe naming a variable Apply here no spaces, no special characters or set of the underscore Just on the simpler the better. All right, So no need to have any long, complicated name with 50 characters. Somebody asked me one time if there was a character limit. I said there is known but I'm not entirely sure why what he had in mind for a name. But, you know, the simpler the better. So TV customer name is saying me that this is a text box support that should be holding the customer name on. I'm going to repeat that for every other control because then I want to be able to reference them easily from court. But let's just start with the TV customer name on then I want Toby. I want that when the person click submit, I'm going to print the contents off the cost. Germany in text box in a you know, another windows. I'm going to acknowledge that. Okay. Car has been rented. Toe that person are so that means like, click needs the work myself a bit button needs to do something when it is clicked. So I was talking about being very deliberate when creating your events, and I'm going to show you exactly what I meant. So we click our button, we go to properties on. Then you notice that property spin. We have this, like paper with a range on top. It may look, you know, shoddy on my display, but it may look better on yours. But that paper with that range, that's the properties window are the property section off the pain on. Then we have this lightning bolt, and if we over, we see events. So when we hover over lightning bolts, we see the menu switch on that. We have a bunch off options for events on. Once again, an event is like a reaction to an action. Right? So a click is an action. What do you want to do when I am clicked? That is basically what an event is. So I have the button on. I want to determine that something should happen when set button is clicked. So for that I need a method stub so I can click this. Make sure you have click on double clicking this looking, creditable looking area. When I do that, it generates that method stop, just like it did with the label when we double clicked it in our previous video art. But once again I was very deliberate. I knew exactly what I was doing. So long. Search are the shortest way to make a quick event for a button. For sure, it's just double click it and it will generate that event. No problem. But I want you to be very deliberate when you're doing that. So you know exactly what you're doing. It you're not making any mistakes along the way are reducing the mistakes to make along the way. So we wanted a quick event, so now it brings us over to the court file or the court behind on. Then it's this. Just take a look at what this quick event looks like it is. It's a private void on then it has the name off the button, but no one on the score. Click. All right. So that's another reason you want to rename your controls. Because if I had six buttons, I wouldn't want but one underscore click button to underscore. Click instead. If I called this button, submit on then I said, And then I generated a quick event. Sorry. So if I had renamed it in the properties, which I didn't and I won't since, where are this fire head? I'm just explained the concept you had. I changed the name, toe submit or something like Bt and submit right and then generated the events. Then I would have gotten Bt and submit Underscore Creek. So whatever the name of the control is on the score, the type off events, that's the naming convention that will always be used when generating these methods stops. All right on, then the it is taking too Toe parameters, Objects sender and event R e. I'm not going to get into what those mean, at least not yet. As you go along, you see their purpose. But right know what I want to do is displace some form off message to see. Thank you for submitting your information. So I have a simple message box in C sharp so I can see a message box docked. It is a method called show, so if you're familiar with the console, that right line it's very similar. Console that right land rights to the console message box. That show shows a message box with a message in it on a message has to be a string. All right, so what is the message you want to display? You just see, Thank you for renting. All right, so let's just leave it. That's back. Thank you for anything for no. And let's go see how these methods work Or this Cricket mint. So when I click, start all right and then I fill out my form. So I'm just putting random data so that attack off car it's rented on the first, and it's going back Monday, and then I click submit, Then you'll see the message box. Thank you for renting. All right, so that's what happened when I clicked. Then it called the phone shot. So that's the event because of phone. Son is there, but notes is it didn't fire until the click happened. All right, so That's the poor feature off events driven programming. So no, I can inject the text coming from the textbook, So I'm going to use interpolation and do a little concatenation here. So I'm going to put the dollar sign before the string and then inside the street. I'm just going to inject. I want the text from the text box. What's the name off the text box? Remember that we renamed the text box to be TB customer names. So when I want to make reference to that control, I just need to make reference the name off the control TV. That customer need no TV, that customer name. If I hover over it, you see that it is a text box. That's it's a class type, right? But remember that these are objects off type whatever control they are. So this is an object off type text box, and it has all of these properties. The property that gives me the data that was entered into the text box would be text. All right, so there is a text property. It's we didn't fill it all. It's because, well, you don't want takes to be inside of your textbooks. at the start, but in once either use. I have inter text into the textbooks. Then you want to text from it. So we need to see we want t be customer name. That's our textbooks control, not the property fixed. So this will give us the text that was entered into the text box during runtime. So let's try that again. So I'm going to click. Start on, then I'm going toe. See? I'll be Gil Walters on. Ah, a big, ill water. Walters spelling. Sorry. All right, Abigail Water Walters. And then I click. Submit on, then. There we go. So we have the message box showing. Thank you. Where thank you renting. Thank you for inviting. All right. So I can just modify all of that anyway, so thank you for anything. Such poor English I apologize on. Then I can just say Colon Andi, the customer name that was entered into the text box. All right, so that is it. So I'm going to put a break point on this line just so we can see exactly what's happening . So when I trick line Well, for me lying 20 my bear. Different line for you, but I click the name off the method or the line that the method definition starts on. I'm thinking this is a grape in between the left, most being on the line, the cold line numbers on. Then that little red dot will appear. So that's a debug or a break point rather on. Then I click start. So what happens that this brake point is going to get hit on a certain circumstances? So for as long as the program is running, the break point isn't being. Kids were doing all of these things. That very point isn't being hit until we hit. Submit and then we see that. Okay, that is we're really cause a function. It pauses in execution. So once you see that yellow arrow, it's pointing to the line that it's no act on def. We, you know, sit down and take. Sometimes I says, What's going on here? When we're in debug mode, we can actually see what's inside our variables. So if we hover over center, you see that sender is telling us that there was some submit right? Some some submit, are some control with the text submit, sent the call to this event, right? And if you look in the you see some other stuff once again, I'm not going to get to watch in tow what all of those mean? But the fact is that visual Studio Llosa's toe paws are courting execution, you know, go line by line and see something. So in here you're running into program. You know that logic arrows are something like that. I just want to see exactly where your mistake Waas You can use these break points a kind of pause and step through. I won't go through the step through just yet, but I'm just going to remove the break points just by clicking back where the red circle was. That turns it on and off on. Then I can just click Continue on, then it will continue with the execution on. Then it says thank you for anything on whatever random text was entered into using into the customer new. So following that principle, I want you to stop this video right now on. Then go ahead and rename the other controls on, Then try and you know, produce a message box that principle the contents off each of these controls. Andi so you would have something like some order New rental customer name? Is this the day rented? Is that the be returned? Is that on the type of current? It is that. And that's what I expect to see in your message box. All right, so I hope you at least pause the video and attempted what? I'm going to walk you through changes that I just made. So I continued on Die renamed my Control. So I changed my date picker, the time picker to DT rented. I call so that data into that changed DT rented the one for the returned I say DT returned . So if you didn't do it and you want to follow along this you know you can follow along This pauses. You need to go along for the combo box. I tended to CB type of car so one second have the prefix for the type off the control on then the name so CB four combo box DT for daytime TB for textbooks. I mean, it's up to you. I'm not telling you to use those prefixes. Exactly. You can choose your own. Just a humble suggestion that will help you along the way. All right on then I modified my submit buttons. So let's look at some of the hold I wrote in the submit and you see some very different chord on. I'm going to walk you through exactly what's happening here, so I like that I'm ableto generate variable on, then store the value from the control inside the variable. No, the advantage for that are for me is that when I have it in a variable, I can do anything I want. So it's easier to manipulate when I put it in a variable than to be writing. If I needed to write the text five more times after it has been entered in into the text box, I don't want to be saying TV that takes TB customer name that takes every time. So if I put it in a nice, convenient variable, I can use this terrible along the way and do any other computation I want. So my humble suggestion to you is that for each control that you're going to be taking data from, you have, ah, corresponding variable toe. Accept it on. Then you can manipulate the data through the verbal as you wish. All right, so I have string user name collecting the text for the user name textbooks. I have string date out and then look at what I'm doing here. No. So the the date time picker is going to be bringing back a value off type date time, But I intend to print a string to screen so I can't put a date time on into the string just as easily. So I have to do a little type conversion. Where? Just see the string. So I'm getting about the value, which would have been water very set inside off the text box or the date time picker. That's the date on then I'm just seeing give me the date as a string. Now there is much more we can do. What? I'm going to keep it simple right now on I do the same for it in string. Date in on. Then I say DT returned the value on Then I say to string some converting these to date values in tow strings so that I can print them. No, In the fourth line, I have a different variable time that we have never looked at our rallies I haven't looked at with you on. That is called var. No, this is not really a data type. This is not a data type at all. Vier is like it is just like an agnostic, um, declaration that we can use unavailable on. Then this variable will be relative to water is being assigned to it. So let me just make some space between them. So notice for every other one. I said this is supposed to be a string, so we know it's a string. As we look at it, we know that these are strings. When I say fire, I am seeing fire because I'm not sure what kind of value would be put into it so I can declare of your blessed bar. And then if I assigned the number one to this, then car type becomes an integer C there. So I said fire. But then I said, car type is equal to one. It is not an integer, so it knows that one is an inter Just so the variable or the variable type is no on interject friend said 1.1 which you noisy that wr afloat and I over. No, it's a double. So if I send a string, then car type is no a string. All right, so the point is that bar is a very, very, very, very convert, Like, actually love using virus so much that it took me a while toe. Remember that I can write the word string so far is very, very convenient. Very flexible. You can use it if you wish. Um, you will cut down the time off trying to figure what data type you're very blurrier. Object needs to be, can just see fire. Give it a name equals and whatever. Yes, I intuited just most that that is the data type it's working with at that moment. All right, so that's very convenient. So I just use this opportunity to introduce that too. So I said, Vier car type is equal to the combo box, which I call CB type off car dot selected item because I want that when you select the one from the droplets, that's one I'm interested in getting back on. Then I have to do a two string because selected item is, ah, type off objects. So then I have to see convert that the string on. Then it gives me about the string value off whatever was selected. And then with all off that done, I can no se message box. So this is the ultimate objective message box show. Andi, What I'm going to be showing is on. I just used my interpretation and read my nice paragraph someone to say something like costumer Name Poland on Then instead of having to ride TV custom or name that text I can two c customer name because I stored the value from the text box inside off this variable. Alright on then I can do ah line break here. So what I want is each value in its own line. So to do that in C sharp in a string, I have to do something like, um backslash and back slash are so this is the break. This is so I get that break rights is almost like customer name. Enter then the next bit off information interest. Each time you see this, it's like an in turkey first. All right, so customer name in one line that I bring Klein that I can press enter and it will automatically just I just the string Calumet, continue typing. So I'm doing this because I don't want to type too much. Still a right and off the screen. So I of course the name on. Then I'm injecting the variable on. Then I have beat are in Ted Colon on them. I'm going to inject the variable date. Oat. All right, then I went to break again. So to me this fast, I'm just want to copy. They said after type it every time break, go to the next line on then did returned colon on. Then this would be did in all right and then say I generated properly. So case matters very, very much Bricklin again on then the car type. All right, on. Then we inject the variable, which is car type. All right on then. So the expectation was that when we fill out our form and then we click submit, We're supposed to see all this information on. Let me just add one more line, which is just says thank you for your business. All right, so let's try that. So I pressed start. All right, So I have cost ammonium tester entered the rent. It is Saturday from where one day to be returned is the fourth on the type of cars a lotta on. Then I click submit And then there we go. We have customer name, the texture of the text box. We have the be rented on the date and time. The one time the car type is Lotte. Andi, the thank you notes Heart. So that it So we go about getting information from or form on, well, manipulating it thereafter. So this manipulation is really just to present it. The manipulation could be toe, you know, Um do, ah, computation on the values that are there. It could be that we submitted to the database. Whatever it is, the fact is, no, we know how to get the information. So later on, we look at how we add things that validation on all other features, like sending it to the database
19. Add Validations To Winforms Data: as in our previous video, we started building some amount off intelligence store windows form. We added a click event such that when we fill out the in formation and so that type of car click submit. Then we get the message box displaying the data that was entered. Now, in this video, what we want to do is add some more functionality or some or intelligence. The submission. We want to add some validation things like me with a customer name Onda. So we want to make sure that when the person click submits the customer name on the car type, we need to make sure that values have bean enter full costume indium and selected for the car type will also want to make sure that the beats are least maybe the beat. Return should not be in the past or further in the past and be Trent. You know, things that that's a deterrent. It kind of a future deeds when compared toa return. So we're going to I had some intelligence for that on. We'll also look at potential exceptions that could occur while we're doing our operation. So to start this off, I'm goingto modify my form a bit, and what I'm going to do is add a new field on. I'm going to call this field. Cost someone to go to their toolbox. I'm going toe Sorry. The display. Some. For some reason, he'd my tools. So I know it's displaying many more than I need, but ultimate directly, 10 clicks show all on. Then it brought all of them. So if you if your toolbox are to the selections inside of your toolbox disappear, you can right click in the pain and click show all our go to choose items and select the ones you want. Right now, I'm interested in common controls because I'm about to bring over a new text box, drag it over to get my hair. Oh, that's fine on I'm going to use this text box toe alot the user to enter the costs on the stake in textbooks. I'm copying an existing label, you know, So I don't have to do, you know, putting on the rework, changing the property, change in the text from customer name to cost. All right. So the expectation is that when the person is entering this record, they enter the customer name the cost. The deer into the returned on the type off car. No, I'm going to rename the text box. As we know we have developed the discipline to do because I don't want to have to know if it's textbooks one or not. So using my naming convention, which is toe put the prefix TV. So I went to see TB cost. All right, so I know I have a cost textbooks or textbooks named cost or for the cost. So no, I'm just going to modify my cord on to get the cost. I'm storing my cost as a double. All right, so I say double cost is equal toe on. Then it would be so easy to this. A TBI costs not text, right? Because that's all we know. We get the text from the text box, but here's a problem. We're seeing that wherever red Line and its city cannot implicitly convert type string to double. That's because text is a string on. I'm trying to store the cost inside off a numeric NATO type, which we know is illegal because C sharp is so strongly typed. So I need to do a conversion so I went to say convert dot to double on, Then I'm passing in the value that is in the text box. So what this does is it gets the text from the text box, then he converts it to a double on. Then we store it inside off are variable double costs. Remember that we could actually just call these variables virus we wanted to. So if you want to change the mode, that's fine. I won't do it, but at least we know the options that we have now after adding the cost, what I'm going to do is add that data to the message box that's being printed, so understand to duplicate one off the lines and change the text accordingly. Cost some had control. D on this line will duplicate the line on all I'm doing, exchanging votes, the data so no cost this printed in my message box. Now, before I get to the message box, though, I want to make sure that all the data is available or is provided before it gets to this. So this should only get printed When everything is in place. Everything is successfully entered into the form, so I need to check and make sure that the customer name is available and that the type off car, at the very least those two should have been selected. So the tool I can use for this is a simple if statement. I can simply say if the string was provided or if the value is provided then or if it wasn't provided, rather than there's an error. All right, so what I can see. And I love to check that negatives first in my court, I can see if customer name right on then, if you have programming experience, are from other languages than maybe other languages would allow you to. To see is equivalent to know Andi. That seems fine because we're checking is to see if the customer name Field, which restored in the customer name field, which restored in the variable customer name we're checking. If the value is no, that seems fine. But then, if it is a blank string than it's technically not know, so that comparison would not yield the result that we really want. So see sharp actually used or some string manipulation, um, fools, so I can see. I can call it the data type string, and then I can say dots on. Then I will see that I have to functions that have one that says is null are empty, indicates where, whether the specified string is Knoller empty or is what Knoller White Space, which indicates if it is no empty or consists off only whitespace characters, right? Someone to choose is no or white space. They can choose either, but Isla y space seems a bit more comprehensive in terms of what we're checking someone to use is no longer white space, then pass in the string that I'm checking to see. If this string is no or contents wide spaces are. It's just empty. Whatever it is that is supposed to to return to our falls, right? Someone to say if this obtains, if this is true or if the car type also is lower empties, I can see if either one of these is no lower white space. There is no other white space. So if either one of these is no or contains white space or is empty, then I want to display a message box that say's Please provide machine data. Please enter missing data all right. And then, as against you, I'm not, You know, based on the father time checking for both off them at the same time. Um, I it limits my ability to be specific, right? So if I had an if statement are conditioned for the customer name alone that I could specifically say you're missing the customer name if I had one for the car type alone that I could specifically say you're missing the car type. The don't side, however, is that I don't want to show a message box for every single error that is there. One message box that or lines all the errors would be nice. Right? So where? I don't know. I'm just seeing one message box with a generic error message based on the the if either one off these conditions evaluates to true. All right. Another check that I indicated I wanted to do was on the dates, because I'm getting bit in and I'm getting date boats or NATO tended inert. So I want to make sure that the date oat doesn't have a greater data value than be it in. Because that would make sense. It would make sense that I see the deterrent. It is the 20th of February, but the date returned was the first of February. That's not practical, right? So that is illegal somewhere at another. If statement on, I'm going to say so. No, I'm going to specifically check for this one and have a specific message box error for this . And then later we're going to refract to the court and make it a bit easier toe or obits more efficient in displaying the error messages. But for no, let's just work on it the way you know, kiss my keys. So I'm going to see if and then these two are date values, so it's easier for you as a human. Just compare to date is February 20 a greater dates than February 1 on. That would be true for Bird. 20 is a greeted it and for border one. So if you wanted to be this comparison, I could say if the debt owed is greater than the date in, then we have an error, right? So did all. To be in the Beit rented should never be greater than the date in being the date returned, so I can say if bit boat is greater than the it in on. Then we display a message boxing illegal date selections, and we just copy MPs that one and change the text. Illegal date selection. All right, but then you notice that I have a red line in my line 34 with this comparison on. That's because we're comparing two strings it's seeing. You cannot use the greater than operator when comparing string, so this greater than operator is usually used for numbers. Like if I want to check if one number is greater than the other. If one numeric variable is greater in value than the other, then fine. But then I can't compare two strings, which then tells me that the way I'm handling my date values is inhibiting my ability toe properly manipulate these values. So I'm going to go back Change of my court a bit. What I'm going to do is change these olds to beat time. So the going back toward design or it Pekar control actually returns a beat time value by default, right? So I was the one trying to convert it to a string here, but the value itself if we over overvalue see that? It says beat time and date time can also be compared. Can be compared just like all integers and doubles and other numeric values can be compared . So what I'm going to do is change my coat a bit. And I'm not going to convert it to a string since that I'm getting the value and then I'm getting an error because we're putting that date time inside off a string. So I need to change my data types. So no, I'm just going to sugarcoat it and use var. All right, So far is going toe meet my variable take on the identity off the value being assigned to it. So I'm assigning idea times over over the variable, and all of a sudden it is I did time. All right. I noticed that my error it went right away. So I'm checking if this daytime value is greater than this daytime value, so date oat should not be greater than the it in on. If it is, then I'm going to display the illegal message. All right. The next thing that I'm going to do in terms off the logical floor is try and put something in place that it won't display the success message if these conditions are true. All right, you know instinctively are probably going to say Oh, but we could have said if then else if and then else on did you wouldn't be wrong But then I wouldn't write it that way. And I'm not winter edited away because when we have an if then else if then else only one block can evaluate the truth. So that means if we say if then else if then else, then we would only get to see one error message that is wrong with the form. So it's philosophical, right? I mean, there's no one way to do this because you could do it that way. And you could say, If this is true, then nothing else executes. If this is false else, if this is true, then nothing else executes on. Then if neither one of these evaluates to true, then the else would trigger on. Then this would be true and this would be displayed. However, I want to make it that I want to show the user everything that is wrong with the form when something is wrong with the form. So that's why I made this statement and in this message box of show on then this is statements this one will show also if both of these are wrong. But then I don't want this to show at all. So I'm just going to put in a new variable and I'm going to call it Vier is valid on going to set this the true right. So I'm declaring a bullion called is valid, right? I'm calling. It is really There's no particular reason for me naming that way. It's just indicative off the fact that it is tracking if the form is in a valid state or not. Right. So Vier is valid on because I send value. True, it is a bullion know if this condition is true on where no, we're going to display an error. Then we know that the farm is not valid, right? So I'm going to change. The value of is valid. If the statement is true than is well, it is no longer true. All right, so is valid is no equal toe falls. So once again we start off by setting it us. True. We want it to be true. We hope that this form is violet. But then we are going to check no for the invalid parameters on the first of which is if the customer name or the car type was not provided, then we're going to say, Well, it is not valid. If this was not met, then is valid is still true. Because then if this condition wasn't met, then he didn't go in and change the valid state or value. But then, if the data is greater than the date in the in, the form is not valid. So is valued is false here. All right, so both of these will check and say that with the is valid status falls on. Then what I'm going to do in this last night is say, if he is valid is equivalent to true. Then we want to proceed. All right, So then if if is while it is true, then we print our success. Mrs. I just put the success message inside off this block. No, in C sharp, you can actually write this as if is valued. So you have two ways to write that you can see a limited straighter comment. You can say if is valid, is equivalent to truth taken right off the entire statement. If it is true, but and when you're dealing with a bullion you're really seeing, if this is true and then if you want to take it, it's far as you can see if it is not true. But I just put in the exclamations and instead of seeing if is valid, is equivalent to falls right so you can write it that shark Otway That's a nice school it or, at the most programming languages obliterated this way on. I'm just leaving this comment there so you can see exactly the alternatives you have. So if you feel more comfortable writing this way, that's entirely fine. So let's go through this one more time. So we set up flag to check the validity or four form I'm just saying is valid is true. So when we collect all the information from the farm where assuming that everything is intact under validity is true, then we start seeing if the customer name is not provided or the car type is not provided, then the is valid is no falls, and we display there a message we also check if the dates out is greater than the date in. If that was true, then it is invalid. And so we changed our flag on display. An error message. Now, at the end off all of that, we're checking to see if the is valid. Value is still true, because if neither off these conditions was met, then his valid is still true on Def is valid is still true. Then we display our success as long as his Val IDs value didn't change from true until he got here. Then it is success. All right, so let's test out our new logic. So we just click, start, and then I'm going to fill o the farm first, so I'm going to keep it, you know, based on what we know. So put in the customer, never put in the cost. I put in the type of car click submit, and then it's going to say the illegal did selection. And that's because I'm checking that the data rented should be greater than the deat returned right here. I didn't change the values on their both defaulting to the seem big, So if I made that adjustment So we see that one off. Our validations is working a click. Submit on. Then we see that everything is OK. All right, You know, let's start breaking it. If I change the bait rented to the fifth on the the returned is the third or the dates boat is no greater than the date in. Then when I click, submit illegal date selection. All right, so we're seeing that it is hitting this. If statement, if we want to be more more strategic in seeing exactly what the court is doing, we can at a break point. So we're already in deep boat moored. We can go back to visual studio out of break points. I'm goingto at this break point to the for Let's Put It to the last variable that creation . All right, on, then, let's try this again. So I'm going to take all cost Germany in this time, and then I'm going to click Submit on, then what it happens is that it hits that on. Then using F 11 you can go line by line. So when you see that lion highlighted in yellow, it means that your break point or your court execution is Know what that line. If you hit 11 it will go to the next line and then line by line. You can trap what's happening. So if I look in this variable, I see that it has no data, writes the customer name Blank string. If I looking car type, I see that Honda is provided. However, when I stepped through and I go to F 11 again, it no goes into this if statement because then this condition was just evaluated to be true . And so where it seemed that the farm is not valued on. Then if 11 and then it goes to the message box and we see message box Inter missing data, click. OK, but then the execution isn't done on, it continues and we say F 11 on. Then we see that the date boat, which is the fifth of February, is greater than the date in, which is 1/3 off February. On def. We f 11 he goes in again and make sure that he knows that the date the validity is farce and then it displays that message box again. So we have to message boxes being displayed on. Then he continues, so f 11 and they will go to is valid. Know if is valid, is checking if it's valid is true, right? Right now it is false. Because if you wasn't set the first time, it was definitely set the second time. And so because it's false, it actually skips over that. If statement on then continues. No, at this stage, we can just hit f five for the continent execution, or we can just click continue on. So we would have gotten the two message boxes because we're two errors presence. All right, so we see that our validation court is working. Doesn't seem way. If I didn't provide a valid for a type of car, we would have gotten that Our message about entering missing data? No. I'm going to run one more experiment and then show you another way that we need to go called for validation. So I'm not going to provide a cost. All right, so no cost will be provided on. Then I'm going toe hit, submit and then you're going to see my court brick right before your eyes. So we're getting some form of exception where seeing the input string was not in a cart for months. All right, So what is happening here is that we did not provide a string for the cost on. Then it is trying to convert it to double. So essentially, this is saying that I cannot comfort nothing. Toe a number? Yes, As a human, you probably seen, but nothing as a number zero, but the computer seeing zero as something and nothing as literally nothing. Right. So it is seeing here that I cannot do this conversion. I don't know what to do here on then. These are what we call exceptions. So when we have an exception, it can break our program. If I could continue to see that it exits the program automatically. Right? So when you're using your applications and you get those fetal airs a click, OK, and then the entire program closes. Those are exceptions. We want to reduce the number of exceptions or the likelihood that exceptions will occur and end the run time or the running off our program. So we use exception handling because we can cord defensively to some extent, and we might not be able to call defensively toe. You know how much further extent. So the try catch is literally the catcher that helps us toe navigate are owned. Those occurance is So what I'm going to introduce toe this button click event is a track it . So going back to my first line, I'm going to see try. So I just right try and then I'm going to press stuff twice, and it generates the entire block for me. So I'm trying on operation on, then I'm checking on exception. Right. So let me just start by taking everything that we were doing inside off this application inside off this trick event. So just the court that we wrote. So be careful with the braces. Just spoke to this last brace off this statement. You caught that and you put it inside off the tray. All right, so we're trying all of these operations and not it It stopped executing on the 27th plan or underline. We're converting the double that the text to double. So that is the problem line. So it doesn't have to be a problem, but that's the one we've detected so far. The point is that I'm trying all of these wherever it feels in attempting any off this scored than we go over to the catch. So when one of those errors, like we just saw with the conversion, when that kind of error occurs, then we check. It's we catch that error. No, I'm going to extend this church on. I'm going to put the exception into an object called E X, which allows me to get the details off exception being thrown. So that error message that visual studio showed about invalid system error and this and that and that I can actually get that wording from this object that I'm touching to the exception, right? So then I can display a message box specifically inside off the exception on then I can say that the exception or the message that I want to show is E X. That's object for the exception dot on. Then it has message. All right, so then I can display the message saying that, you know, this is the problem that you're encountering. The throw would actually end the program. Know you can make a decision. Do you want in the program when there's an exception, or do you want to continue the program even though there's an exception, so I can just remove the throughout commented told for no. But you can believe it if you want, but the throw once again will end the program. If you don't have the throw, it will display the message box. You click OK at but then you still have your program running. So let's see how that works. So I go back to start on then this time I want to test and make sure that my exception handling work. So I'm going to provide the data that I know what needs to be provided. Change the deeds on then click submit and then you see input string was not in a cart format. So what happened was it tried the operation. It got up to the part where tried the conversion on. Then it realised that OK, I can't do this. You went down to catch the exception on then that is the message inside off the exception because we didn't type anything in a message box to see this. Right? So this message boxes displaying the message for that particular exception. So you see, even though we can call defensively against feels that we're expecting or conditions that we're expecting business rules that were enforcing. Sometimes the system itself may have on error that you could not have accounted for. So that is the where the try catch comes in handy. And we can catch certain exceptions on Do not let them in the program, you know, completely. No. As a final act, I'm just going to modify the way we're displaying the message boxes. All right, so instead, off this being a message box per error, what I'm going to do is have a string where I'm going to compile the errors on. Then if is Validus falls, then we display that message box with all the compiled sentences off Earth. So right now we have to Harris situations. We have this one. I will have this one. We have to message boxes. What I'm going to do is this clear? A new variable var error message. But I'm making this one equal to an empty string. So I'm starting off with an empty Aramis it because up until this point, we're assuming that the form is valid right on. Then the error message would be empty. So instead of saying, ERM my message box. That show I'm going to see instead error message on. Then we can do. Ah, little operation here. Was there close equals? All right, so we're just seeing we're compiling this stream. All right, so we're seeing error message Close equals, Please enter. Missing data on the number is going to have the new line operator right there. Right. So in case there are multiple lines that will be there, don't want all the lines so I can refine this a bit so I can see error. Please enter missing data. So if the cost of the name and the current type are not provided, then I see the validity off the form is no falls on. Then I'm adding on error message toe the variable that I already created. Right? So it starts off blank on then. If this is true, then no, it has some content. So I'm seeing whatever was there before at that. This are at this string toe. Whatever was there before, that's what The PLO sequel. It's an accumulation, right? So we will probably be used to this with math. Well, in C sharp, he can use it against strings also, So I'm going to do the same thing. Don't below. I'm going to see if the date oat is greater than the date. And then I also want to accumulate this error message onto the string. So I'm just going to see this new land character, put it into the string. Take all this extra breeze on DSO. If either one of these or if both of them are successful, there were just compiling the error message on the is valid would have changed anyway. No, I can put in that. If Elstone here to say if is valid, then display a message bots with the success on, then I'm going to see else I want a message box. So message box not show on. Then I am went to display the arrow message. Whatever the contents off her message are upon to this point, I want to print them out inside off the message box. So that means if you add another scenario tomorrow where you have another if statement for the validity, then I can just add era message and see what is wrong. So we just for what to put in a word error there. All right, so I'm just throwing it out there so many ways. You can do this. I'm not saying this is the way to do it. I'm not saying this is a standard. I'm not saying that this is, You know, there's no other way. I want you to have your individuality. Sure, I'm just showing you that there so many ways to do this once you accomplished what you need to do. Andi. With maybe fewer lines off court, the jury is out on whether fewer lines of court means more efficient or not. But the fact is, if you can accomplish it, then that's good. So let us test and see what this would look like. So if I start the application all right on, then I submit all what happened when I submit is that it's going to bomb out on this land. So if I submit with everything blank, it tries to convert the empty string toe cost on. It is just going to catch the era. So when I click, submit is he dumped straight to the catch and is displaying that era, so that's fine. But let's provide a cost and then it's drag. And if I click submit. No, it's seeing object, not, said toe. Instance off on object reference on symptoms. It's sense off on object, which I can tell you we didn't write that error, so that's another exception. So, no, we need to debug and see exactly what we're missing, Why we're getting that error. So if I put my break point at the starts off the line, it's already those minimize. Put my break points and no, I'm bringing back on my window and I click submit. So I'm going to step through until I see where jumps old. So as far as you can see, once it goes to the next line, everything is okay, right? So it's going to the next line on then I think it's this line. It's saying that the object reference is not set to anything. So I'm here trying toe convert. Nothing to string, right? So the selected item is no. I'm trying to convert notice tree on. Then I am guaranteeing that when oppressive, it's going to go straight down to the catch, and there we go. All right, so then that's another thing that we didn't cone for. All right, so that means we need to kind of change the way that we're getting the value from the combo box. So another way to get the value from the combo box could be the duck texts. I'll use the duck text so doc text would get the value from the combo box on. I made a court change while I'm in debug mode, so I actually have to restart application, right? So Doc text would give me the text that is being displayed in the combo box. If there's no text being displayed, then it's a blank string That is safer as we're seeing. Um, if we tried the way that we had it before, we're going to get an exception on. We may not be ableto properly tell the person Sorry about that. Romance will be able to properly tell the person what's wrong. So using the doc text is probably easier. So let me just restart this application Once again, we restarted because if we make a court change while we're in debug mode, it doesn't register on the to restart all that. It was quick. This, um, are circular beside the stop button and possible. All right, so let's try this again. So I'm going to change the cost. We saw that the cost validation work did come to the exception. On day I went to click Submit. But before I let me take off my bird point because I don't want to break points, I don't have to step through. So taking on the break point I'm clicking. Submit on then. We're seeing our message box with our errors. Error, Please enter missing data error. Illegal date selection. All right, if I change one off them So I put in the customer name and click submit, then I'm only going to see Oh, well, it's still seeing enter missing data. So it's so That's another thing, too, because I'm checking for both customer name and type of car in the same statement. It's hard toe be individual rights. I mean, I could probably miss it. Um, Phil Littleton say, please enter customer name on the type off car. I could be explicit. So obviously, if I have one and not the other than the user could deduce, so that's fine. You could change the message, or it could change the number off. If blocks I won't prioritize Iran. No, but let me just produce the type of car click submit. And then we're seeing one error message nosing illegal date selection. And then we change that. It's a three and then we click submit, and no, we have a valid form on divided dates of being inserted. All right, so that's pretty much it for building in some validations. Once again, this is not an exact science. This is not an exact like, this is the way it has to be done. This is just a guy giving you some ideas. That's the whole You could go about validating what you need to look out for, how it so properly handled when they're genuine errors that you have not accounted for with those ending the run time off your program.
20. Create SQL Database : in this video, we're going to start designing our database to store the data coming from our form. So for our database, we will be using SQL Server Express on DWI will be designing at least two tables based on what we have here. Want to store the rental record and another one to store the type off car because, well, this is a drop down list and that list off cars is subject to change. Maybe one gets added. One gets taking away, you know? So we want to create a table where we're actually storing the list off cars available for rental. So our first step is to install SQL Express Edition. So if you already have SQL server than you're fine. But if you don't, then you can proceed to go to Microsoft dot com slash i mean, I'll give you the link or you can just google SQL server on. Then when you get to this society too slick known loads and the one that we're interested in is expressed so you can get developer. You can get Express express is you know it's free on its ah smaller edition, so it takes less the install this time, and the fewer resource is. But then developer is fully featured, and it's, you know, it's takes a longer time. So for this purpose, I would encourage that you just get express. You won't be losing much along the way anyway. On then. It's a straightforward installation step. You can check out my YouTube video on installing it if you need some more guidance, but you can go ahead and install it on the management studio on then. I'm wanted to speak up from the fact that the management studio has been installed. Server has been installed and we start designing or database No. After installing SQL Server Express, you be ableto open the management studio on. Then you be able to log into your server so you see the name off your PC a backslash onda, the name SQL Express on. Once you see that, you can just connect. If you don't see that, then you probably want to ikan drop me a line and asked me for some guidance. Otherwise, if you click connect on your unable to connect, then you probably want to go into your services. Check for SQL Express and make sure that that service is running on, then you can try again. Either way, feel free to reach out. Form to me for further detail. The ones you have connected to your server you can start building your database, so I'm going to just right click and click new data basis. This launches a dialog box, someone to call my database car rental. And then I think, OK, once that database is created will appear in the object Explorer to your left. So I'm just going to drill down on both the tables, right? Click and click new on. I'm creating a new table. So the first table that I'm creating, like I said, this car until soc Um well, the table is for the car unwto record. So the data that I intend to collect would be in keeping with what I would have designed my form toe collect. So I want the customer name the cost the bear rented there. It didn't returned on the type off car. It's a type of car will be a foreign key. So at this stage, I'm hoping everybody understands basic database design. And why were foreign keys and primary keys? If not, then you can check out my other course on database development. I have Microsoft SQL Server for everyone and I have my SQL database development and mastery . So both those courses bring you enter end with database development and explain why we are foreign keys and relationships and all those things. So I'm not going to spend this scores going through those details. I'm going to continue assuming that you have the basic information. So we need table to collect these fields and then we have another section for a foreign key . And we remember we're creating another table just to keep the types off cars on. That will inform the drop down list here. So back in my SQL Server Management studio, I went to go ahead and create the car rental table first with an idea off type into on It will be foreign key opera murky. Sorry. So I right click, say said primary key on. I wanted to be auto incriminating. So I go down to I d toe identity Selection on DSI is identities. Yes, I just don't know and it becomes yes. And that means it will count automatically, right? The other field I want is customer name on this one can be an environ char maybe. Ah 100 characters long Next I want the d it's rented and SQL actually has a deed time beat the type also one beats returned on. This also will be a bit time data type. I do believe I had cost, so I won't cost on. This would be a double. So they do have a double. Do they have double? Some of double somehow floats stimulus Verify that is a data type It is not. It's actually decimal cerebral that so in for costs were using decimal on. We can leave that the fall sitting there on. Then I'm going to see car or type of car ID's of type off car I D. And that's integer We can leave it at that. Then we can click save so we can play it too safe here or control s and that were receiving this s car in tow record. All right, so the table name is car until record, and once again it would have all the basic details surrounding on actual car in tow. So if I walk into the place for inter car and they put me in that application the expectations that when they fill this form with my information on the details of the rental , it will get saved in this table. All right, so the next thing that I want to do is create another table, and that is just for the list off cars. So I go back to object Explorer, I click open a new table, and then I want an I D on. This is integer right. Click Cedatos Primary key on, then make it identity. Strolling through the property is going toe identity selection dropping the his identity should be Yes. All right, on then. I want the name off the vehicle, so I just want idea Name. That's literally all I want. I want each cart of its own idee unique identifier on its name. Right on. I went to me that an environ char off size 50. All right. And then I see this table on I'm calling these types off cars, so that's all that want. The next thing I need to do is make a relationship between the i. D. Or the primary key for the type off car on the the column that is supposed to be the foreign key for the type off car, so I just right click anywhere inside off the design view for either table. Really. And then I see relationships will actually, no, not for either tip. I wanted for car entire reports. I rightly inside off car Intel record on. Then I go to relationships. Take ad And then I dropped down the tables and specifications when I don't need to drop it on industry. Need the ellipsis? Really? So I just click it this ellipsis button on? Then it's going to say Where is the primary key table? The primary key is coming from the type off car table or the types off car stable on the column that is the primary key is I D on then the field in car rental that it much is toe is type off car I D. So I dropped down and then I'm just making the link, something that a primary key from the types off car stable should reflect in the type off car I D column in their car into record table right, and I click. OK, click clothes on, then save on, then all things being equal. Everything should be all right. If you get an era that point, just make sure that they're both in. So here, in the foreign key table or in the car, entire court table, disobedient on that the primary key should also be into If those steps are foreign to your you're not familiar with them. Once again, I encourage you to just go through my course, my SQL development database development and mastery or a Microsoft SQL server for everyone . Those two courses will get you up to speed with everything you need to know about database design. So, for no, that's it for designing our database that were, you know, we developed a company or win forms application. In the next video, we will look at how we actually link this application to the database.
21. Connect to SQL Database : in this little will be going through adding our data based our project. So in the previous video would have downloaded and installed Microsoft SQL Server Express. If you didn't already have it on, you would have used it to design two tables, one for the Kyra until recording one for the types off cars that are the data bees. So we want to know, add the references to this database over project, and so we're going to go through that step by step on, and it Microsoft makes it relatively easy to do that through the use off radio dot net So we'll see exactly how that works. So the first step is to go into your solution. Explorer, right Click on the project on the click at. So we're adding a new item, and then you go to the data category. Soc visual C sharp items you go to data on. Then we're is choosing a dio .net entity data model. So that's the one that we're using on. Then I'm going to give it a name. So the name I'm going to use is car and, uh, TV. All right, so the name off this data model is car rental DB, and then I take ad. The next screen asks me the type off model that I want. So if I want the designer from database and to design a model empty cold, first model or court first from database, flicking on each will give you a description. But the default one that is selected is one that I'm interested in. And if you want to read up on what it does, it says, that's it's just in tow. Create a model based on an existing database. He was sort of the connection. String on everything so that we can interact with the database through this model. So that's the one we want. E f. Designer from database. We click next on, then we move on to a screen where it wants us. This shoes are connection. So I had built on application using North Wind in the past. So its hope on that connection. But that's not what I'm interested in. Right? Someone's going to go. It's a new connection on. Then I'm going to type in the server names. Remember that your server name would be your PC name. Box slash SQL. Sir, Extra express rather, but a short cut could be just the put a dot the box slash ansi SQL express. So our doctor is equivalent to your machine name pretty much in this situation on then leaving that there you can go down to the part where you select or inter database name. You drop this down and you see all of the databases that you have on Escalate Express. I have two. Or you may have few because you have these default ones. Or maybe you've been using it from before, but the one you're interested in is car until because that's the one that we're using for this project. So I select car in tow. I can click test a connection just to make sure everything is okay. But then I click OK to proceed once that is the new C that the context kind of changes and is showing you the connection string that is going to be generated on. I'm going to actually just walking through what you're looking at in this section because it looks confusing. But it's really simple once you know what you're looking for. So the metal later section is just saying that, Okay, we're adding. This is like some stuff to declare the model that will be added. That's auto generated. You don't have to worry about that. The major parts of itto would be the part that's his connection. String is equal to data source. So data sources talking about the server you're looking at. So when I say surveyor looking at right now, we're looking at our local server Our own PC with SQL Express database instance, is hosting our database In the event that this database was not on your PC, then this would not be the same value. This would be that maybe the I. P or the machine name off that other computer where that database is. So I'm going through this to tell you that if you're developing this in an enterprise setting where you know you are, you have your PC on the databases on another PC somewhere else in the company, and you need to connect your application to that central database. Then the data source would be the name off the machine or the I P address toe that particulary machine. All right. The catalogue talks about the database that you're connecting toe on then well integrated security that can remain true. Multiple active results. It's that is very important for multiple persons to be connected the database simultaneously. But as you can see, those are already there and they're already set the truth. So the two parts I want to focus on me and they are the data source on the initial catalogue. Once again, data sources the name or the address off the machine that has the database on the initial catalogue would be the database. In an enterprise sitting, you would be probably shut down by security, as in access to that central database would be metered by security. So in other situations, you year went up by user idea and a postured inside the connection string to be able to commit. But that's for a later date. So right now we have a local. We have our connection string. It will be saved in the file up the conflict so you can see up. Doc config is one of the fires that we got with our projects, so that will be modified toe have this connection string, but we can click next, and then we can choose the latest into different word. That's fine. We click next again. Now. This screen isn't asked us which objects that we want to import into our model from the database. So if I drop down into tables, I'm going to see all of the tables listed in a database, which is all off the tool that we just created. So I can a stick tables. So if you had more tables or you're using another database any of other tables, you want all the tables, you teak tables, you want all of your musically views. You want all of his third procedures. You take them. Whatever is in the database, this entity framework model is ableto import it and put it in the context off your project . All right, so I'm going to take tables on I leave these default settings, notice the name off the name Space car intermodal. That's fine re click finish, and then it will go ahead and do its magic. So once it's finished doing that, loading any creation and we see our models. So let me just take some time to explain exactly what you're looking at here. If you're familiar with database design, then you're familiar with diagrams and one off the diagrams as very important would be the entity relationship diagram. So the entity relationship diagram shows, you know, like a visual representation off your table, the fields in there, Maybe the data types in some extent on also any release son ships between each tables each tables and entity on. Then you have the relationships between them. Hence the name entity relationship diagram. Now, if you're looking at this and you're familiar with what a drd looks like the new notice that this looks just like any already I have the two entities in my database are the two tables. Andi. I see the relationship between them. When I click, this line is going to highlight that the I d in types of cars is related to type off car I d. On it also shows you that you can directly access the data inside of type of car through that relationship will get into that later. But the point is that the model is giving you direct access to the tables within the context off C sharp. So once you've added this model and you reach this fire, I encourage you to do a build on the build can be done using the keyboard, shark, oats, control shift and be, Or you can just go to build on build solutions. So it's the same thing, But you're not actually starting the application. You don't need to start application. You just want to do control, shift and be Let everything re compile so that everything knows that. Okay, Now the database connection is there. All right, So once we have seen that, we have a successful build and we can proceed with the rest off our development before. All right, any court I want to just walk you through this diagram and watching. Need to know. So I'm going to right click in the empty space Click Properties on, then I'm going to say All right. In the properties, we have two very important news. One more important, they'll go. So we have entity container name, which is called Khar in tow entities. And then we have names space called car rental model. So whenever we need to access our data models here, we need to know for these two names, all right. When that time comes, you will see, however, another important thing that I want to note. Let's go to the solution Explorer. And then you see that some references got added on you. See, the new file got added and you see that they're also class files with the same names as the tables. So these class files were actually generated per table. So you see current I record dot CS When you click on it, you look at it. It was other generated. When you look at it, it looks just like a C sharp plus. So the point is that when we want to get data or insert data are manipulate data were literally just doing all of it. Karen interferes, called delinquent. You look at later but an interface called Link that will interact with these more native C sharp classes on once we're interacting with them were actually interacting with our database. So, Idi oh dot net gives us a nice little interferes Taconic toe our database without actually needing toe worry airboats, sq and other types off dialects that would be needed for that kind of operation. But we can keep everything homogeneous and everything looking like c sharp chord. No, The last thing that we're going to do in this video is actually very data related. And that is the insert, some default values so that when we start hooking up to the database, we don't have any problems. So notice that we need to specify the type off car from or form. So what we want to do eventually is so bind, we go to the design, want to bind the items that are in this list toe what is in the database. So we went to the money or step off taking this carrots, going to edit items and inserting these. What I want to do is have these in the database whether more than these are fewer than these. But these values, I don't want them hard coded, because if the Honda is old tomorrow, I want to be able to control it from the database instead of having to come into the design every time and change that option. Right? So how much are you going to clean these old from here from the selection. Click. OK, on. Then I'm going to go over to my database and then I'm going to insert these values into that table. Sorry. Click on tables, they expand. I click on types off cars, right click on Be caught any top 200 rows. So later on, we'll actually developed an interface that allows sister manage the types of cars in the days of this. But for no, just humor me and tied them in manually. So we have Honda. We have Lada. We have Bogey Onda, a Toyota. Those were the values that I had in my form for the types of cars. So we have Honda, Lada, Bogey and Toyota. Those are the types of cars in our database. So in their next video, we will actually come back to our code behind on Run a query that retrieves the types of cars on, then binds it toe the list off items available inside off our humble box on. Then we'll go ahead and see Hoe weekend, collect data and submit to the database
22. Executing A Select Query Using LINQ: Hey, guys. Fooling or previous activity off adding or data model to your project, we will know, proceed to start running queries, toe collect data from our database and some whole presented in our application. So our first activity is to deal with well existing did. So we have No, We have data in our table. We put it there in the types off cars table where we have four cars. Toby selected from on the objective here is toe modify our list for the drop down box on buying it directly toe watches, school from the database. So I went into the design. I went to edit atoms and right. No, there is nothing inside of type off car at his empty, and that's just verify how empty it is. So I'm looking at the application itself, and you see, there's nothing in there, so the expectation is that after off, our operation will see the cars in there. All right, so let me just close that, go over to the court behind, and then we can get started. So the first thing that I want to do is creates an object that represents our dates on models. So remember arrested that there's a few names here that we need. Toe knows Iraq. Click water properties on. Then we need to know the name off our entity container, which is car in tow. Entities. Right. So in the court, behind under the public, partial and right above the control. The constructor. Sorry, I'm going to enter privates on, Say, read only some declaring an object off type car rental entities. So this object embodies this entire species is entire space that you're looking at the entire diagram. Every entity represented in the diagram is encapsulated by the type car rental entities. So by making reference the car intel entities giving it an object names I'm going to call Parenteau entities without Come on. See, on day I went toe just to semi colon on then So this object is going to give me access to every single entity that is inside off my model or every table that I would have inserted into the model. All right, So back in record behind the next thing I want to do is actually initialize it. So this is a declaration, but it's not initialized right now. This is no right. So I'm just going to say car rental entities is equal to a new instance off on car rental entities. Please note Vier wouldn't work here. You can't be clear parts of a class as far. All right, So you can do variables. As far we can't do properties off a class as far so this is a private property off our form . All right, so once again, we have declared a private property off type car rental entities, car rental entities being the name off our entire database model. All right, on, then I give it the object name with the same name, just with a common see on Dhere in the constructor. I'm just initializing it's toe a new instance. So it's no longer know it's Greedo because, no, it's not being used. But don't worry about that just yet, aren't now the next thing I need to do is the figure out hope is it? I'm going to retrieve the data from the data beast so that it is present by the time the form comes up. So the expectation is that when I opened this application of form loads on did it will load with the drop down list fully equipped with the data it needs to carry out operation. So that means I need toe query the database, get the information from the database, Have it presented in the form. All of that. By the time you the user would be ready to click the drop down box. So I'm going to add what we call a farm load events. I'm just taking the form notice. I click form on the box itself, or the canvas itself is selected. And then it goes of properties. The lightning bolt on, going to look for the Lord event. See? So I'm I want something to happen when the form is loaded. So when the farm Lords, I want to run that query very quickly. All right, so I'm going to generate a formal defense. I'm just wanted to look like in that. And it's seeing form one on the score, Lord. So then it seeing what do you want to happen when the form lords? So when the form loads once again, I want to query the database, so I'm going to say something. I've ire cars is equal toe, so I'm declaring some variable called var. I'm calling it cars are sorry it some variable off type of our calling it cars on, then I need to assign something to it on what I intend to assigned to it is records from the database from the table types off cars. So when I declared this entity up top here, or this object rather off type car into entities, this represents my beat obese, So I can then see car rental entities on. Then use my doctor cousin object so I can use my not operated to see the properties there in. And when I say dot you'll notice that I have my table being made references of car rental records. If I go back to my database on Look Car into other record was a name off the table, but then it has many records. So that's one of the things among the model does by default tries to plural eyes the table names because, I mean, I call it a table name current our record, but really our current or records system. So I really should have called it car into records. But that's beside the point. The fact is that using my daughter operate, I can access this table. So that's not the table I want, though. I want type types off cars. All right on. Then. I want this in the form off a list somewhere to say that toe list open and close parentheses on DSO. What this is doing is using a language or a mechanism we call Link, so it's a native C sharp, but the library it's of it's called Link on. It is a nice way to Korir database using native C sharp chord. So once again, I would have basically established, on instance off a connection to my database to the declaration off this property. I initialized it in the constructor that that's all constructors are for the initialized things. Our initial ISI initialize component there. Well, im initializing my database to a new instance on. Then, when I want to call in the database, I use that objects that was previously declared and initialized his without operator on. Then I can access my table and then basically, that's it. So if you're usedto dit early, that's it on. I'm just converting into our lists. I want in a specific date that I've called a C sharp list on. That is why I have the to list. So if you're used to database development, this is the same thing as saying Select star from types off cars. But then this is SQL. This is not native C. Sharp Soto be able to write this kind of SQL would have taken far more lines on. Do you know, I would have to initialize a data reader than a data table to call it the objects and a bunch of things. But Link makes it so easy because you just it's called the database model. Call the table on then, if you want it in a listed a city to list, and it's easier to just say to this because that's a more universal at the time on, it's easier to maneuver with. So know that we have the list off cars stored inside of for variable cars. We need to put it to you. So what I want to do, once again, is when the farm loads, which is going toe fire. This event we want to get the list of cars from the database on, then have the combo box display them. All right, so the next step is to see Call our combo box at a CB type off car Right on. Then we're going to say this plane members. So there two properties that we're going to set. First we want to set the display member on that That is going to be equal toe name. So this play member is a text that you see, Art. So remember that when we had it set up the way setting the items and you would see the text Honda, we would see the text Toyota, etcetera. This play member is the text that you see on. So if you look back at your database design, you notice that I d is 1234 You don't want the person to be seeing 1234 That means nothing to them. But the text that you want them to see is the name Honda Lada body Toyota. Right? So name is the display member on the value that we're interested in would be the i d. So we have value member, they display member, and you have value member because it's actually the value that we want to store, not the display. Once again, we have a car into our card that has a type off car I d off type into, which means we only want to store the idea associated to the type of car. We don't need to know the name again because that's why we created the table to just store the names on the I. D. S. So once we make reference to that, I d. From the current I record, we can look back and see that. Oh, if it was I d three, then we know it's bogey or the person selects bogey. We want to store I d three on the record. All right, So the value member would be what you intend to really store, and that's in the background right on. Then, after sitting those toe, I am going to see that the data source for the items to be put into the combo box so that C B type of card data source is going to be equal toe cars. So let's go through this again. I'm quarry in the database for the list off cars. And then I'm saying that I want my combo box, whatever data source it gets, whatever bit off the take. It's just look for a display member for a name, said the value member to be I. D. On then said the data source, to be the cars. So when I get the list off cars, they're all coming over with idee and name someone to display the name I went toe store the I d on d I'm going toe set that this list off cars with idea and name should be the source for the list off items coming into this combo box. So let's start and see how that works on Douala. So, no, you're seeing a list off items for our form or four or drop down. So remember, I clean the moat earlier, so it loaded. There was nothing in there. No, you're seeing that data is actually in there on this data is coming directly from the database. And to prove that I'm going to close that window, go over to the database at one more car. So we added one more car tore fleet on this one is a super rule. All right, so, no, we have five cars in our fleet on I'm going to go back execute again, and when we look in the list, the expectation is that we're going to see the Subaru. That was just a bit So that is your first step towards connecting to your database. So let's just review what we did. We set up ah, Farm Lord events because we realize that there's certain data coming from the database that needs to be present in our form by the time the form loads. So after initializing a connection to the database through the use off, my objects are referenced that the entities models. So we have our into t model. I initialized on object off the type off that entity model. All right, so the clearly tapir initialized it here on. Then I realized that I needed data from that database before the form loaded. So I set up ah form Lord event which worried the table. Onda based Ecologist did the initial steps towards setting off the combo box. So these steps are pretty much universal. This is not is not unique to the fact that I'm using the entity model and this and that This is how you set up a combo box to inherit from a list of items, whether it's a static list, our list coming from the database so they're using Link or you're using the other methods that are there. The fact is that this is how you data bind or you buying the objects to your controller are to your combo box. Rather. All right, so once again, we ran or Corey got a list off cars from or database literally. If you read from read a list that's left that started seeing, Give me a list off the times off cars from the database on Store it in this variable on. Then, later on, we say that we want to display the name Onda and store the I D on. Then the data source should be from the list off the cars that we just retreated from the database. And so when our form loads, we see that it is running that quarry on getting all that data. If you want to put our break point here on restart application and just go step by step to see exactly what happens, just to be sure, then you can feel free to do so. But that is our first step to connecting to the database in the next video, we will be looking at hope when we click. Submit grand. No. All we do when we click submit is, you know, display the information next time we want actually save it in our car rental record table. So that's all we'll be doing in the next video.
23. Save Data Entered in the Form: Hey, guys, in this video will be building all the functionality for saving our form data to the database. So, up until now, what we've accomplished was that we built our database. One, we imported a data model into our projects to represent our database on Ben. We looked at how we actually invoke a connection to the database. So in our previous lesson, we would have initialized the objects that connects store database. We we made a reference it or we declared it, and then we initialized in a constructor on. Then don't. Here in a farm one lord function, we would have run our first query using link. Andi. So I'm just showing you that this is the same as things like star from types off cars. That is the same thing as this line on. Then we just feel the combo box with a list off cars in the database. No, The expectation is that when we click, submit on destroy a preview off the form. When we click submits, The expectation is that we're going to be saving the detail that was entered toe our form. All right, so I'm going to modify the quarter bit So we have a button click events that we've been using toe. Get the data from the form. All right, And then do some validations on. Then if it's valid, then we display a message. If not, then we display. Ah, well, if its value than with display a favorable message. If it's not, then we display error messages. All right, so what we're going to do is modify the is valid portion. So if it is valid and I want to save the data on, then I can display the message. If I can't see if the data, then clearly I don't want to display the message. So when we want to save data, we need to declare an object off the type off legacy, obey, stick and experience. So when I go back to my model, so if you you know he closed the model by accident, that's fine. You just go back to solution exper And then you see that that e d m X file second is double . Click that and relaunch your model, right? No. Whatever the boxes name is, it's usually much. Is the name off the table? All right. So you see the box There, you see current on record. You see, this one is types off car. And if I look in the solution Explorer, if I drop down the e DMX the ah file, then you'll see that you have a dot CS files her table. So you have car intel record and you have types off cars, no types off car. Sorry. So those files represent class files that represent your table. These are classes that are directly mopping every field in your table so that when we did the car entire record, we know we set up on I d customer name, the rented, all of the fees are we put in the database I right here in our class file. So the cool thing about this process and using Lincoln video dot net is that I can create a new object off this class type on, Then I can save this object into the database. All right, so let's go through this step by step. The first thing I want to do is in the block that is valid. I want to declare a new object that I'm going to save. Our Parenteau record is equal to a new instance off car or intolerant court. Right? So once again, this class directly much is the car Intel record table that we created Here are all the fields that are available to us on day directly, much the columns that are in our database, which means that I need to fill these fields with the data that I intend to store inside off the database fields. All right, So, Well, all of them with the exception off, I'd because we created I d to be auto increments. I don't need to provide a value for I d. But cost German name is going to be entered in on the form. They will go to the design. Costa, my name is going to be entered into the form in the court. Behind were already collecting the customer name on Syrians in a variable customer name on dso. What I'm going to do is try and mop whatever values being stored in this variable Muppet in tow, my newer inter record off type car into record. All right, so I know that I have a object that represents my table. I'm going to see that the rental record dots customer name field. So the customer name field in my table should know have the value off the customer name value that I just collected from my form. All right, so once again, this is the table. This is the text box. Sorry. Storing customer name in the wind forms I'm collecting the text on. I'm storing it in my local Very because customer name? No, no, that I've collected locally. I want to push it through the database. I'm going to declare an object off the table that I intend to fill with the data. And then I'm going to say that this objects field called customer name which directly mops toe the database field. I wanted to have the value coming from my wind forms application. Alright on then. I want to repeat that process for every other ones or rental record docked theater printed is going to be equal to On which field? What do I have? Storing data rented being parented value is being stored in my variable date. Oat. So I cannot deliberately named them kind off sparsely so you could see the difference and not get confused. So data rented is going toe have the value that is stored inside off date oat right in the same way. On adjusted control detail duplicate that line. Did it returned? Is going to have the value that is being stored in it in. All right, so you just build oh, to your your model. So for every field that you intend to store data for in your database for ever feel that is no present in your object, you just feel them over the corresponding data for it. So rental record dock's on. Then the next one will be cost. That is equal toe my variable costs on. Then the final one is the car data. So, no, I have a little error here with my cost. So I'm storing Costas double, But then the class type is decimal because I declared it as decimal inside off my database . All right, so cost is distant. Mile. All right, let me just see. Oh, I could have used float so I could change the data type here just to make it OK, but it's probably easier. I just cost It's here, so I can probably see some tonight decimal dot and then I will just do a parsa disemboweled up parts on them parsing cost all rights and that will take here off. Okay, a slight miscalculation there that did not take care of that because the pirates function is expecting a string on dime passing in a double. So let's re gather our thoughts. I think maybe I can use an implicit cost here and just see this in mile that see if that works. So sometimes you can actually do a casting light This So we looked at two other ways to do casting. You can do data type, doctor parse or you can see convert dot to on. We use one of those functions. Here's another way to do our casting. So you just used Prentice's the data type you want to cost toe on. Then the value comes afterwards, writes this works sometimes so situational. You just find the one that works on you. Move along, right. So know we have the cost on the final value that we need to go into our table, which is coming from or win forms would be the type off car I d. Right. So the type of car I d is an integer because it is the foreign key value that needs the mat back to the primary key value off the car, which is 12 the 12345 based on the data that we have here. So we already know that we're loading up the combo box with the name on the I D. But what we're collecting from the combo box is the text. So if I look at it, I'm really collect in the text. I need to collect the actual i d behind the combo box. So to get the selected value literally, the combo box is going toe have a property called selected value. Right. So I'm going to go back down here to my type of car I d on then I want to say CB type off car. So this is my combo box property. Sorry. This is my combo box control. Right? And then I say dot Then I'm going to see that there is a property that's a selected volume so I can scroll through if I'm not sure. Ho to tie people selected value, all right. And then you think that that's all visibly hover over it. You know, it is that this is just a generic object type, and we're trying to insert it into an integer so we can use another implicit data type conversion. There were just the open bracket. Give me an inter present Asian off. Whatever the selected value is on the selected value once again would be whatever value is marked toe the item that is selected based on the fact that we have value member being I d . All right, So what I'm going to do is create a break point on the is valid, and then I'm going to run on. Then we're going to step through and just see hold our table. Object gets filled old. All right, So I feel the daughter. The customer name is test user cost is 25 conjured. Let me change dates so that we don't violate any off the validation on. I want to go with body, someone to click, submit we hit. Our break point is valid is true. So we can just use f 11 to step through and then we see here one we're declaring on objects that represents our table. All right, so no, we've declared object. No, we start filling it on understand to step through. Don't type off car I d on defy hover over my object and just drilled on Introduce See all of the values cost Got set Test user God said the two days got said the type of car ideas not set yet because I stopped on that line. So if I press f 11 and it goes beyond that line, then you can see that the type off car idea is three. All right, so that is how you get the actual i. D. That much is the value that was selected from the drug. Known once again. That's important because we're actually going to be storing the I D. Instead off the actual name off the car aren't on dso I can express their five Onda. We go on to see our success message. No, When I look in the database, I'm not want to see the data. All right, so we just do it and sure it. So when I select stop 1000 rows from this table, I'm not want to see the data in this table because where we didn't really save. So we filled up the table, but we didn't do anything with the data that we fill it up with. So I'm going toe come out of the book more than then, we're going to finish hope what we need to do. So know that I have filled the table object with all of the data needs. I know need toe, pass it over to the database on, then save on that. That's really two steps. All right, so I'm just going to say something like Onda them. I just remind myself off the name off the database objects. So Kyra enter entities that represents my database, some with just a car until entities dot on then the name off the table or collection off records from the table is Kyra until records. Then I say dot again Number one toe ad. And then what I'm adding is on is expecting on object off car rental record. Guess what I have on object off car until record type in the form of for Intel record. Right. So I created the object your entire record. It is off type car into record. Which much is the table on? DSO I'm going toe Add this object that I just built just finished you know, constructing and putting all the data in. I'm just adding it. Toe the collection off records for that table. All right, on then. I'm going to see it. Abyss. I want toe save changes. Now. Once I do those two lines on, then I click. Start again. I'm going toe Philo the form. Make sure everything is valid on. Then click. Submit on. Then let's step through one more time. So I'm just going to do F 11 through all these lines and then that's filling or objects. So if I hover over the objects once again, look at the data that I'm going to see that the really fun feels are filled. You see one with no Onda. Later on, we'll get to why that is no and why it doesn't matter at the time were creating the object are creating the record. He doesn't matter that this is no and it doesn't matter that this is zero since I'm creating because it's going toe caught a increments anyway, right on. Then I went to F 11 so it no gets the part words adding into the collection we f 11 on. Then we save changes on then it moves on to mix lines, Understand? F five on. Then we see our Oh, are, you know, success message and we click. OK, but then we see we saw off that before what we're looking for that's different. No. Is the actual record being in our database? So if I go back over toe esque UAL management studio on Die rerun this square where select the items from the table, then I'm going to see my record in the database. And if you look, you see that we have one as the i d. Because once again, auto increment the customer name which was a poor attempt at spelling the word test on we have the date rented the date, return the cost on the type off car I d on once again this i d much is back, so over records. So I kind of I d six. If there is no six on, that's why binding the combo box like we did on at the former Lord. That's why binding it is very important. All right, so as many times as a fellow, this form and putting information and submit the expectation is that it will create a new record. Fill the record with the data on, then save. It's all right. In our next video, we'll look at building old other windows so that we can see, you know, like the types of cars that are in the database as well as the rental records in the database. We won't be designing or right in the logic. It will just be looking at extending the design off our application from just one farm to maybe many forms. I can show us the date that we want.
24. Creating Additional Forms and Windows: in this video, we're going to be looking at designing additional forms in our current application. Now, the reality of the situation is that we're writing an application for a current of business . So far, we have one form on. Let me just bring it up quickly, and this form is really just collecting data that goes towards creating I rental record over. I'm sure if you sit down, I think about that. There's much more to our current of business, then just, you know, logging the fact that somebody that named the cost that day it returned and rented the type of current clicking. So there's more to the business and that there are other things that need to get done. No, the problem is that you don't want to try and do everything on one form, because this is one form we are using this one form to call it our car in Tyler record. So win forms allows us to build multiple forms, and each form can have its own, uh, functionality on pretty much they're all autonomous, meaning they're all able to act on their own. So in this video, we're just going to explore what goes into creating a brand new form, Onda Hole. We would go about launching it from another farm. So back in our project, we're going to right click on our project file in our solution Explorer, remember where the solution and then we have this project, right? So I'm right. Click on the project and then I'm going to click. Add on, then I can see new item or can say form. Either way, I'm going to get the same dialog box. But ultimately I'm going for the form someone to start off with New item and then in new item, I can look for a form, but then I can go to the sub categories to the left and see windows forms on. Then what I'm going to do is select the form template, which is a blank windows forms. Win forms former, and it's so seeing you get a blank form, a blank template. It's also asking for a name, so our very first form that we would have started the project with what's called form one. That doesn't really tell me what's going to be on that form, does it? So if I have up to 10 different forms doing 10 different things. I don't want form one for him to form three. I treat naming my forms the same way I treats naming my controls. So I'm going to build this new form on. I'm going to call it me and Window and I soon explain why I'm calling it me and window, because I am actually going to be using this new form for something very special. So we call it me and window and then we click, add, and then a low visual studio toe. Create that new form and give us this brand new canvas CC. It looks just like all form one look before we started dragging over new controls on such right now the fact that we named it in me and window it comes deep by default with the word meaning to open the title bar. You'll also notice that the new file is called me and window and every other file associated with it is also called me and window. I'm just going toe adequately able to it. So I'm just going to go back to my form one design and I'm just going to copy this label. I'm just boring this label, putting it on the main window on. Then I'm going to change the text that's in it. So I just right click it goes of properties on. Then I'm just going to change the text to see me and windows so that when it launches, I know exactly where I am. Right. So me and window Sorry, man. Window There we go on also I don't want mean window to just be printed like that in camel cased in the in the title bar. Maybe I want something else. I'm just going to click the form itself, Right? Click, go to properties on then I'm going to change the text that is being displayed off their some distance to see means be swindle Press enter and then that looks a bit better on the eyes right now Let's see how this mean window looks. So when I click start it is still going to launch form one. No, this is Form one. We've been working with Form one now have a new form on. I would like to see this new form on the window. However, I don't necessarily know how to get to that form So I need to put something in police that I lost me to launch this form when I need it. Right. So coming out of the boat moored just clicking Stop. What I'm going to do on form one is bring over another button. So I'm going to show you how you can go about launching another form from from one form launching another form. So we have form one. It's always going to come up with form one by default. But I want that when I click this button, I see another form. And in this case, I have mean window as my next form or my next window that can be displayed, right? So I'm just going to go to Toolbox, quickly, go to my controls, get a button on. I'm just putting a random button there. Nothing spectacular. This is just purely experimental. It's only temporary, but I just want to prove I get the point across that you can launch window from another window. So I'm just going to change one or two properties on this button on Don't let it sea. Launch me and window. Launch me and window. That's the text in this button all right, so this button is simply supposed to launch the main window right now. It would have to do that on click. So that means if I'm going to expect the bottom toe, do something when it is clicked that I need a quick events, right? So if you finish that sentence, there's no kudos to you. So I right click on my button, go back to properties the lightning bolt on double click in the creek era. So there, two ways. Now you can just double click the button that will automatically generate a click event. Or you could do all of what I just did by going to properties, Get in the lightning bolt and and creating the tick event. As I said before, always tried to be very deliberate, all right. No, I have my button. I noticed that I have but no one click here and then I have but one underscore. Click Underscore one. So that's where the conflict ing names coming because I didn't rename this button on this button is also but no one. I know it's two buttons with similar names with similar click events, so once again it is best if you rename your controls. All right, so I have the button click event for my new button on. Then I'm just going to write two lines off court that are going toe. Tell me or tell the button that when you are clicked, launched this new window or launched this new form, right? So I'm going to say me and window. So remember that everything in C Sharp is a class. So farm one is a class. If we look at it, Form one is a class that is inheriting from a base class called form, right. If I go into me and window, it also has its own court files. I can right click me and window go to view cord on. Then I'm going to see that mean window is also a class off itself. It's called me and we know because that's what we call the file on. It is also inheriting from the base class form, right? So what I need to do to launch on instance off a mean window is the declare an object off type me and window. So this is me declaring object on initializing It's a new me and window right, remember and see Sharpest. So we initialize on object off a class on. Then I am going to call a function inside this object called Show. So similar to hole we show our message box were always said Mrs Box, not show, right. And then that's what pops of the message box to the screen. While all I'm saying here is that I want an instance off me and window on. Then I want to show this window once it's created on. Then we just discuss using virus. I could just save I right there instead of seeing me and window. The object is equal to right. So you have the two ways you can write it, whichever one you feel more comfortable, if that's fine. So no, the expectations that when I click this new button, this experiment all button in this car into the system window, it will launch ah window for the main window or launch off the main window form. So let's try. So that starts. All right. So I have my car in tow, form up and then I click Launch main window. Andi, there we go. All right. So when I click, launch me and window. It does appear just a prism and other monitor notice that each time I click, it is going toe. Pop up this me and window So you can bake in some intelligence at some point to see if an instance off it is already open. Then don't open anymore, right? But for no, we see that we can launch another window any time we want. Once we have the proper court in place. Now that's pretty much it for whole. We Global launching an additional form from an existing form, understands that Close all the windows Arkin dispersed Stop toe exit, debug mode. All together I'm going to do one more thing in keeping with the best practices that I'm trying to promote in my video. So far on that is the rename form one from Just form one that CS because, yes, it was the first form. Yes, it was the first thing that was there. So by default, it's form one. However, when we start building in other forms, you don't want to have to look at the solution expert and guess that form one is the car into a window the same way we can just look at this and deduce that mean window represents . I mean, window is the same. We should We should be able to look at the found him for the car rental. I know that it is the car entire record window. So I'm going to rename this form on by renaming this form. Visual studio is going to allow me toe change out every reference to this form one class throughout my solution. So the new name, so to rename, I can right click the file and see Rename on. Then I'm going to call it rental record are Let's be more specific odd Brent record. Right? So remember that this window is all about allowing or still insert our entire reports. Are your entire record percenter then visual studio is asking me or telling me I'm renaming a file. No, it's asking me, Would I like to rename all references the farm one in this entire file on? I'm going to say yes. I do want to do that and notice how old is a student is refract us all the references for me so that when I build control, shift and be there are no errors in my court. Also, the bigger advantages that when I look in my solution Explorer, I can directly are immediately detect all the forms and what they are for aren't so. The last thing that I'm going to do is look in my program that CS file No. In our programme that CS file, we have all the core that runs, as at the time the program is started. When I click start what really happens is that visual studio or will the engine looks still programmed RCs file, which executes this main function. So if you were doing the console applications and you would be familiar with the fact that there is a main function at all times right on, then what it does is initialized some visual stars, some text rendering stuff. I don't know. I'm not going to sit there and teacher where these two lines, though, I'll just advise you that if you're going to move them, be sure you know what you're doing. Otherwise you can leave them alone with one that you can modify is application dot run where it is running on understanding a new instance off. Well, we just changed the name from form one toe after entire records. So what happens is that when you click start, it does. This line does this line on. Then it runs a new window off a torrent I record, which is why that's the first form that comes up. So I know that we have a mean window form that we want to make the landing window for our enter application. We can change this from utterance. I record to me and window No, What's been topping is up. When the program runs, it's going toe launch the mean windows. So let's try that. So I click. Start on. What comes up first is the mean window, All right, so, no, the next us that we have is to transform over entire application into what we call a multi document interface or an M D. I application, where our main window with holes menu strip that allows us to go between all the features on the respective forms that will be needed to support a functionality for this application . Other functionality would include maybe, like an archive off all our entire records. All right now, we're only adding, but we don't view them unless you go to the database, you don't want to send people to a database. You want people to be able to view them in the application. Likewise, we need to be able to manage list of cars on. There are other things that we maybe want to do in our current application that we cannot do right? No. So are me. And we know will no be the landing window or the 1st 1 of that launches to show us all of our options thereafter. On by clicking each option, we would launch the respective form for that feature.
25. Create an MDI Application: In our previous video, we started making some modifications to the floor for application. Most notably, the fact that we created a new window are Neuf win form on. Then we set It's Toby, the initial form to be launched when our application start. So when I click start Onda, we were would traditionally see the current off form. No, we're seeing over me and window. So no, we need to start transforming mean window into an actual mean window and a container. We started looking at the concept off multi document interfaces. And so that's what we're going to do in this video. I'm relaunching me and Windows design and I'm going to remove this label on. I'm going to just meet this mean window a bit bigger, so I can always just you know, I just the width are the natural with off the window with an height off. Worse, by dragging it are by, you know, expanding it are collapsing it as accordingly. No, What I want from the mean window is to be able to launch other windows. And right now I really only have one other. But you know, we're building out an entire application I'm sure whether you sketched it or you conceptualized it, you can imagine that there are a few windows that we will probably need to build before we can have an application that will probably going sell. Tow a car, Inter agency. Now let's get into the meat off the matter. So what I want to do is go to my toolbox on. I will want a menu. So I'm going to bring over Ah, many a strip on day. I'm going to just add it to the top here. And then this ministry will allow me to start building a menu. So as with most Windows applications, you know you have filed have editor view. It's it for etcetera. I'm actually using the CME technology that use you this kind off menu that is the menu strip, right? So I'm just I just dragged over this menu strip on then. No, it's allowing me to start typing the menu options I have so I can say manage vehicle listing. All right, so that's like I mean, you know, that will be my file and any Tampa multi level. So for the next level, I'm going tohave add vehicle. I'm going tohave removes vehicle maybe, um, and it vehicle Onda view listing. All right, so those are the many options that I envisioned for the managing off vehicles. I mean, as you go along with probably will take also Olga, because maybe we can get at least three off. These don't from one form, but for no, I'm just showing you that That's what you can go about building a menu, right? So that's money vehicle listing on. Then I'm going to say money to rent our records right on. Then the next level would be toe odd our Intel record. Ah, weekends view archive. So this would be the archive off all their in toes that's have occurred in the database so far on. Then we'll probably want to edit rental record, right? So the Russian I'm not going to put deletes because maybe you would want your used to be ableto adder entire record, which we have a screen for already. That's always started. We would want them to view all our entire records that have happened on. Maybe you'd want them to Eddie Tarrant. I record because you know, if somebody change their mind about a rental agreements they were supposed to bring about Tuesday, and they want to bring back Wednesday instead. And they should be able to go in and edit that rental record. You don't stuff like that, so I'm going to leave it at this for no on. Let's just stick, start and see what that looks like. All right? So, no, we have or May and Window. And we see that we have our menu on when we click these buttons while nothing is happening , right? So we still have some work to do in order to get these menu options actually useful. So the 1st 1 that I think we should work on is the utter entire record, since that's the real feature that we've been building upon to know anyway. So what I'm going to do is go back to my menu, expand it, Andi, I don't know some people. I have this problem like I do, but when you click it, it's supposed to expand. If it doesn't, then well, I click it until it's creditable on. There are double picks just now breaking my own cardinal rule. But when I click it and it becomes a little bit presenter, and then it drops down. So if you having the difficulty getting it to drop down like I just did, then that's what I used to go our own. That I don't know. I'm a visual studio is behaving this way, but I'm just working with it. Otherwise, you shouldn't have a problem. So what we want is a quick event on this menu item, because the the deduction would be that when I come to manage your entire records and I click utter entire record. And the expectation is that my rental record window shouldn't entrance. So once again click event. I can always right click Goto Properties Goto Landing Boat and changed our I can just double click it, and it will generate that click. Events stopped for me. I noticed the naming convention is going to tell me the text that is in the Barton, and then it's a tool strip menu item. Underscore the click. All right, so that's the naming convention on because I inadvertently, though, clicks the mean one. I got the managed rental records, quick events showing up. No, remember that if you get are wrong, click event and you just delete it. You're going to have to do some amount of cleaner because if you leave it at one time on, then you go back to your design. You're going to end up with this nasty looking error, which is really worse than it really is, because you can just click Go to court over in the section. Here on it will show you the line in the generated court that is trying to call the function that you just delete it so you can just remove that line on. Then you should have no problems thereafter. Off course, As you go along, you just do control shift and be to continue to build your application to make sure that you have no air. Say, see, my builder's successful on I can close any unwanted tabs and my design view has returned to normal. So where were we? We were looking at setting up the click events for over many strip are to strip many items , So if you remember how we launched me and window from or car intel form, then you can write the court. Otherwise, I'm just going to go and review it one more time. So what we did in our outer entire record was to put in this button purely experimental. But what it did was it launched the main windows. I'm actually going to remove the button because I don't need it. It's not useful or it's not necessary going forward. So I removed it from the design. But I'm going to the court behind on. I'm just going to review the core that we wrote in the button click event. I hope you'll notice also that removing the control does not remove the events that was attached to it. So the Adventist in there it just has zero references because there's nothing that is calling it art. So inside off this quick event, what we did was launch an instance off the window or the form that we want to launch on. Then we called the Dark Show function inside of that objects. I'm actually just going to remove this unwanted click event, so if you had it, you can remove it. If you feel like you want to keep it, that's no problem. You leave it alone, but I'm going back over to my main window, my click event on the window that I intend to launch is add rental records. So I need a variable. And I'm going to use wire this time. Someone to save ire ID Parenteau record. All right, so just speak some spelling errors here. Our rental record is equal toe a new instance off my class. Add Renta record. So notice off. Course that. Yes, it's the same name, but one is common case. A oneness. Capital A. This one is a class name. This one is object. Name. All right. So does make sure that you adhere to the strict rules where it comes. Toe casing. All right, So know that I would be cleared this object to the next thing I need to do is they add a rent a record docked, so all right. And then I can save on, then press start on. Then when our window launches, we're going to test the thought. So I click. Add her entire record, and there it is. Once again, it came upon my other screen. So no, we have our button working on. It's going to work the same way as many times as I'm going to click. It is going to launch that window right, so that's not desirable. One and two notice that this form he came upon my other screen because it's can come at any point where it feels comfortable. And that's not necessarily the experience that we want. We would prefer that this form is kind off tethered to the mean window, and that when I closed the main window, everything closes. Andi. Everything is tethered to the me in windows. So then we have two other things that we need to do. So I'm going to go back to my main window design on. I'm going to change a very important option where the making the M D I containers concerned . So multi document interface means that each form would be seen as a document pretty much on then the MD I appearance or that multi document interferes parent or container would be the storage place for all off. Those documents are all of those forms. So as soon as a form is created, it is created inside off the periods and it cannot exist outside off appearance. So what we need to do is in me and window we right, click goes or properties and then we look for our property is MD I container. So you'll find that under windows style on. Then you can change. It is MD I container. Give me that. True. So notice the difference in its appearance When it's falls, it looks, you know, lively, bright, that more vibrant. It looks more alive over when I see Truell. Then it looks great out and kind off dead. Alright, but then that that's is one of those necessary evils to get it to be the MD I container. Now we've made it the container. But we have one more step where when we're launching our window, we need to tell the window that it's peer into spirit. Who would appearance is rather right. So right now it just launches. Even though this is the MD I appearance, the window that is being launched doesn't know that it is an MBI I child. All right, so I'm going to add this other function where I'm going to say your entire record dots or what? I'm going to change a property rather so I'm seeing uttering tell record dot m The I appearance right is equal toe this. All right, so let me explain exactly what's happening here. So one we already know that we're launching our window. We know that we're showing So we're declaring the object and then we're showing it in between. Though I'm telling you that having created that object, you should know that your MD I appearance is this. And this is a key word in C sharp that means whatever class you're in. So me and window is being represented by the keyword that this because MD I parent expects some form or some objects off a form to be assigned. So when I say this, this is like a manifestation are an object off the main window. It is a key word in any class. So if I go to the other entire records and I see this inside off the Eloranta record class , this represents at rental record. So it's always relative toe the class it is being used in. So when I say this inside off the main window, notice when I click it, it highlights me and window. But C sharp is automatically saying that. Okay, I know that I represent the mean window. So I'm saying that this mean window is the MD appearance for the A rental record. Ah, other entire court object on Then I want to show it. So let us see what difference that means. So when I click start or window launches and we can see that dead green, we can see the difference on Then I'm going to launch Arent a record. That's what I want to do A notice it launched directly inside off the window. This time I didn't have to go and drag it over the screen. Onda also noticed that it is completely contained within this mean window. It cannot go anywhere no matter what it does, it cannot go anywhere. I can minimize it inside off this man window, I can maximize it inside of this mean window I can close it on my way. Main window is still alive on DSO This allows me So when I'm finished doing this clothes on , then go ahead and view the archive or view the listing off vehicles, Whatever it is, I can do that afterwards. All right, so that is how the main window on MD I rather hold the MBI I concept works. I created me and window so it could be the MDR container on going forward. Every other form will be launched unmanaged inside off this mean window. Before I go, though, I want to make a few cosmetic changes. So I'm going to change the texting mean window from just me and window to say something like, um, you know, Cruz car rentals, Um on then, inside off our car rental form went to say after intercourse, instead of saying Kyra until system on, also with the change that takes inside off the title bar for that form. So me just exit and let me just do all of them in plain view. So right clicking my form go into properties on in Maine. Window protocol. This Krul's car rentals that's name off our business are, you know, car in tow management system. Sorry, current management system. Let's make a genetic causes a product that we intend to sell, so we don't want to brand it before we sell it. Right? So that is the name in our meant in our me and window. I'm going over to Adam anti record, and I'm going to change the property for the farm one text. So I'm going to directly go to properties that show me the events I just click over to properties. James. A text on this one is going to be ad for UNWTO record on going to change the text in the label. Finally, on, this one is going to see add rental record. All right, so there's some cosmetic changes. Just still make our application look and feel a bit more official on. Then you can think start on. We can just take one more look at everything. Just make sure that everything is the way that we wanted. So when I go to after entire record is going to launch the new window inside off the application on it looks a bit better in no easier on the eyes. Know that everything looks like it makes more sense. And we can do a curse artist just to make sure that we didn't break any existing core. So I'm just going to put in a test rental record on submit on There we go on. If you want to go to the database and verify that it went in, you can do that, but I know it went in because we didn't modify the form. So each form. Once again, it's autonomous. It's its own document. It's going to behave off its own intelligence based on what you are have accorded into the called fire for that form.
26. View Data In A Grid: in this video, we're going to start designing a screen toe, help us manage or vehicle listing. So let's just start our application and refresh ourselves with what we've done so far. And we have many listing here that allows us the add remove, edit and view listing. So I'm actually just going to do all off that from, well, one window on. Do you know, case by case will probably designed something else? So I'm going to modify this entire listing or listens. Are many strip and just have managed vehicle listing someone to remove all of the child options off that menu on? I can just click on delete. So when I click that one and it selected, I could express delete on it will actually just remove that many items. So that's where you can go boat removing them right? So I want one that says manage vehicle listing, and when I click it, I'm going to launch a new form that's a lot going toe. See all the vehicles in the database on, then perform the crowd activities from there. So I'm going to do everything from one window. So firstly, I'm went toe no click Onda do the trick events on that button. So there number of activities I need to be done and just a great X. And they don't need to be done in any particular order. So I'm creating the click event. But I'm not going to put anything in the cricket bent because, well, the ultimate reason for the quick event would be to launch the window. And I haven't yet created that form, right? So, yes, I have the tick event, but I need to create the form. So, you know, particular order. I have stuff to do. I'm just saying that to say that you don't think that we have a strict order off our series off activities that have to be performed in a particular order. Right? So have a quick event already. We can come back to that, but I'm going to right click my project. Goto add Say new item on once again. I'm creating a blank form on. Then this time I'm going to call it manage vehicle listing. All right, so my form is managed vehicle listing. I can spend some time doing some cosmetic changes where I'm going to meet the text in the title bar, Not look. So you know, programming look like All right, so manage vehicle listing on. Then I'm going to start looking at some new controls on the first new control would be the grid view control. So in my toolbox, I went to go into the subcategory data on then I'm with C one. That's his data grid views the data grid view, displays, rows and columns off data and agreed that you can customize. All right, so that is what we're going to drive over. So I struggle with that data grid view, and you notice that is just this big green box. So I'm just going to expand this great box to a certain extent. If I use that current I get by that menu on, I can edit columns at columns, you know, enable it. It's delete and add. I'd be son phone shines to the grid if I want. I can also doctor in the containers if I document continent this entire container and that's not necessarily what I want, because I'm going to put in a label at the top to say, manage vehicle listing on. I'm also going to put in some other things in here. So, you know, maybe you start off with one idea of what you want to do, and then you realize that Oh, but you don't have to do it that we could do it another way. Sometimes that happens. So let's start off by not having it duffed on them were to bring over label. So I have a label in ad our entire record already. So instead of going and trying to design at brand new label, understand To copy this, come back over to my new form, paste it, make sure I position its on going to change the text. I go to properties, change the text, and this one is managed vehicle listing. All right, so this reposition it accordingly. Our it's our new window for managing the week listing No. Most important, the most important part of this is our grid views or grid view will give us like a tabula representation off whatever data we tell it that we want to see right so you can think off excel or even in the database. When we did a select star from a table and we saw we noticed that we get that tabular representation. That's exactly what that data grid view allows us to do. So the first thing that we do with these I call them or essential controls because I label is essential, but not that much. But I want to know the name off my grid view, right? So I go to properties after thinking review, and you can take some time and look at the properties and realize that these properties with every control, their different properties on off course, each control is unique in its own right. But the 11 common property is going to be name on. I don't want my data grid view to be called data grid views. I'm going to call this devi, you know are GV grid view Right on This is car, the store vehicle switch over one. So remember my prefix and then the name off. Whatever it is that this control is for so great view for vehicle list, then I'm going toe set up this form that when it loads, I run a query for the vehicles. Andi, I put that data or the data that's going back from that quarry inside off this grid view, right? So remember we did something similar to that when we were looking at launching our car where owes it or add or in Tyler record form, when we wanted to make sure that our or combo box had all the data needed from the listing off cards. Right. So in this case, we want a grid view toe have that same data when the form loads. So since I wanted to happen when the form lords. So when this happens, that is the events that I want. So I'm going to click on the form, go over to properties the lightning bolt on. I'm saying that when the form lords I want this particular thing toe happens under, sent a double click inside the Lord section. And no, it's saying, Okay, this is the four name on the score, Lord. So when the form lords, what do I want toe happen? So the next thing I want to do off course is established the database connection, and then I can start earning my query. So if you forgot, hold that works. You can go back to the Eloranta Record court file where what we did was declaring objects that represented our connections in the database. We initialize it inside off for constructor on. Then wherever we needed toe access the database. We simply called on that object, right? So we're going to follow that same pattern. So in our new form, the first thing I want to do is in the class. I'm defining a private on I'm making read. Only that's most remember, because in dotnet that always happens. It's no, you know, you can do it without read. Only that's fine. But I wanted to be off type car rental entities, though if you forgot where this name came from than in the e d. M X file that you would have created for this project. That's a car intel db dot cd a mix in my case. But if you have a different name, it must be dot e DMX. You can double click that you see your data model, but when you goto properties, you will see entity container name, and that is the class type that represents the connection to your date of bees. All right, so if you ever forget the name or you are doing one from scratch, you just looking entity, container name and whatever name is there that is your data type. Alright. So back in our new court file for our new form, we say private read only the times are car rental entities, which is our entity container name on that. I'm going to give it the object name. So visual studio is always making suggestions, and that's really cool. I took one of the suggestions in the previous form where I said, Odd car rental. Let me find that court file quickly. So in the previous one where I access the database, I used resource studios suggest a and that's fine. But a lot of the times on the Internet, you probably see something like context being used or on the score. Db. The fact is, the name off the object is not it's not. It's inconsequential to the functionality, all right, so I can mean my object anything. So in this situation, I'm going to name it underscore db That being said, however, I do promote consistency. So if you started on one path continued on that park, if you want to change the path, change the path, right, throat your court so don't have underscored DB in this form on, then car into record Renta entities in another form and then another form. You feel another way and you call it Context are CTX Don't don't do that. Parents have one consistent naming convention. I'm doing it this way just to show you that whatever you call the object is inconsequential . All right, so I'm going to use DB in this situation. However, later on, I will go through a normalized everything toe on the score. Db All right. So private Read on the car until entities db on the inside off my constructor on the night underneath initialized component. I'm going to say on the score, D B is equal to a new instance off higher and entities. So that's the initialization, right? So when our program rooms are when this form is launched, it's knows about the DB, then it initialize is its components and the db Andi Then throughout the rest of the form, any time we need to talk to our database, we have our underscore db object. So the first thing I want to do is run that quarry to get the car somewhere to save ire cars. Andi I use cars and vehicles interchangeably. So whichever one you want to use, that's fine. But Vier cars is equal toe database connection docked on. I don't want the car into a record so I can go scroll until I see the table that looks like what I want, which is types off cars. And then I can say that to list. So I want the list, or at least off the types of cars in my database on. Then I'm storing all of those inside off a variable called Cars aren't on. Then the next step would be to let the data grid No, that it should be displaying whatever is going back inside off the list of cars so I can say GV right? So because I renamed it and when it's a great view cycle, it's GV vehicle list dot data source disease. Well, tow cars, right? So the list off cars going back from the database will serve as the data source for my deeds are great. So let's see how that looks. Click start on. Then when the window comes up, I'm clicking and nothing is happening. And why is nothing happening? And we see that we still have a stream, any item. But nothing is happening. And that's because I did not finish up my click. It meant so I finished wiring up the form. Or at least for no. I've wired up the form someone to go back over to my mean window design or to the court file or other on. Then here is that quick events after both the design? Because I missed a menu items I need to delete this one. All right. Good. Are so know that we've cleaned up the interferes back in our court file? What we need to do is let the button launch the new forms. Some just in to save ire on this one is going to be vehicle listing is equal to new Andi. I call it vehicle my vehicle listing. All right, on, then I have to say that it's MD I appearance. Is this vehicle listing that FBI parent Easy quarter this, which is mean window. Andi, Finally, after when all of that out of the way I want to show. All right, so let's try that again. Someone's a launch our application on. Then when I click manage beak listing Now we're seeing our listing on. Look at this. No. So we're seeing the grid view, Andi. I mean, it looks somewhat anemic away. See the big green box and we see the grid filling out as much off the box as possible. We see that we're getting about the I. D and the name on this seemingly random column from the database call car into records. But the fact is that we are seeing the current our records in that grid view so we can go back to this window on may be clean it up a bit, Someone to get out off this. So I'm going to modify the width off this great view because I really don't need to needed to fill You know, that much space for two columns. So I'm seeing I d name on Dwell car car rental records. I don't explain what that seemingly random column is, but I want to add the buttons. So I did take out the ability to edit add on, remove the cars from the menu, listing in the main window. So what I'm going to do is put buttons inside off this window that will be responsible for helping us do those actions, right? So I went toe have four buttons are three buttons. So I mean one button and I'm just copying it. I'm basting. Have a spear one. All right on then, for my buttons for the properties one I want it takes to see what I wanted to do So But in one let's say we want me this the add new car, right? The button three. I want this and other spin properties so that I don't have to keep on going in and going also button three. I want this one to be at its car on. Then this one would be delete car. All right on then. Another important step toe having my buttons is that I don't want them to be Bt and one button. One button, two buttons. Three. I want them to have sensible names that I can reference them in the court behind. Right. So But in one, I'm going to rename to Bt and that's my prefix for button ad car. All right. Edit car is going to be BT and it it car. All right. And then the final one would be Bt end. Deletes car. All right, so then I can go ahead and create tick events for them from no if I want. So I'm going to double click the add car and notice because I gave it a sensible name. The trick event is very clear. Bt and at car on the score. Click right so I can just go ahead and create all those thick events accordingly. So just go back and double click each until I have those quick events. And And this is why I don't like pinning properties because in when writing code, it's not useful, right? So then I have three button click events for three buttons on my form, so that's just some cosmetic stuff. Let's just start again and see what that looks like. All right, so no, we see that a grid is a bit smaller on. We have the data being displayed shore, but then to the right, we see our buttons. And while they're all dead buttons because they have click events, but the click events aren't doing anything just yet, so no, I want to clean off the grid a bit. I don't want the idea and name columns obese, displaying in common letters like that. So what I would do is inside off my form Lord function. When I said the data source so noticed that one, the data grid is generating the columns relative toe the data, it is being fed. So I'm feeding it the list off cars on the list, off cars, each object has i. D. It has name on some list off car rental records. Right? Which are soon explain exactly what that is. Well, at least later on, we'll see exactly what that is. So what I'm going to do for my data grid is I'm going to say GV vehicle list dot on then I want to specify that I want the first column, which I know is going to be I d. So I can see columns. And if you have ever worked with arias or collection So we're working with the least on That's a collection type. If you ever work with honoree, you know that the square brackets represent um you can specify exactly which subscript off the ari you want, Right? So if you haven't, that's fine. So let me explain. So the columns here, This is a great view control. And then I'm saying, I want the columns. And if it's columns, that means it's our collection. There's more than one columns I want the first column, which I know as a programmer is going to be I d. Anyway, so I can say square bracket zero, because in computers you start counting from zero are not one right. So the first subscript off binary zero. So the first subscript zero on then I want the property off this which is the header text to be equal to the text. I d in our caps. Right? So I'm seeing that after I get this stuff cars on. Then I get on and I said the data source at this point all off the columns would have been generated. I'm saying that the first column that was generated I want the header text to be I d. And then I want the second column. Son was going to duplicate that line and Jesus substrate from 0 to 1 on. I want this one to be name in all caps. All right, so let's see. Hold that. So if I click start, then I'm going to see that I'm getting idee name on them since Ian Kerr Inter Records. But at least the column headers are changing accordingly, so that's encouraging at least, All right, so the next thing I want to do is to eliminate the fact that I'm getting but that seemingly random car Inter Records column. But before I do that, let me just explain a bit what that represents. So I opened up my data model, which is the E DMX file, and I'm seeing this BRD. If we look in the table types, off car or types of cars, we will see that we have the i. D. We have the name and then below we have this navigation property called car rental records . Right. So just humor me for a bit. And let me take it on this journey. If you have a rental system on, then you have a car that is rented multiple times. Then that means that this car is associated with multiple records, right? So if I wanted to know how many times my I believe our car called Lada or Honda, how many times my Honda was rented, then I would have literally a list off the times this car was rented, right? So that is exactly what this navigation property represents. It says that for any car that you're going to look at, I can show you the list off our entire records that it is associated with because they are related. All right, on then. In the same way, the current I record knows the exact type of car that was rented in this record. So this is whole link Andi Andi into different work, actually global. It's helping you so form good relationships between your tables and actually coronate across multiple tables with much ease, but without getting too much into, you know, complicating. Exactly. Hold their used right. No, I don't need the reference of the car into records just yet. So back in my court file, I'm going to tweak this quarry a bit to select exactly what I want. So this is doing a select star. We did a select star in our Arent our record once again for or combo box, where I said that doing it this way is just like saying select star. In this case, we don't want star. We literally just want i d and name because when We're doing it this way. We're getting but the random, seemingly random column off car and Tyler Records, right? So I'm going to go back on. I'm going to show you whole weekend treat someone to comment this oath on going to see that this is the way you do Select star from types off cars, right? Just so we're clear. But then I'm going to rewrite this quarry, and I went to walk it through exactly what we're doing. So I'm going to save our cars once again is equal toe the connection to the database dot the table that I want. But then I'm not going to say that to list. This time. I'm going to say that. Select. All right on then. When I do that, I'm going to have to work a bit off. I'm going to call it magic. But as I said, I'm here to guide you through, so I want to select just the two columns that I want. I'm sure if you dio if you're used to SQL than you think I need to say, Oh, I d Kameni. Well, it's unfortunately not that simply in c sharp, we're going to have to do what we call a lambda expression where we create on object. Right. So this is Lambda. So this that's just it's right. There is nothing more to explain. The only thing I can explain here is that this doesn't have to be acute. This could be a w. This could be a word. I mean, you can use any expression you want, but the queue or whatever expression you you use here is going to represent every object that is inside of the collection. Right? So this is just how you start a London expression. You have an expression. What do you want to use? A letter or word or whatever on then you have equal on, then the greater than saying or I just call it the arrow. Right. So you have the expression than the arrow on then This allows you to see what columns you want from the table. So I'm saying select. Then I initialize alumna expression on, then using the lumber expression I can see Q docks. I d right. But then I don't want just idea. I want a new object. So what I want to see is new on, then I'm open up. I opened up my curly braces on then inside off this I'm going to specify that I want on i d being equal tokyu dot i d Then I can use my comma and say I want a new, um a new property called name on Did it Should have the value Que that name. All right, So if you're usedto SQL and a Lessing, all right, where in SQL you would have said something like, Select this and you give it an alias. Should have selected a column, I would say Select I d as another name and select name as something else or whatever. This is how you go about doing a Lessing inside off link, right? So once again, let me just walk you through what we're doing. We're saying I want to connect to the database, give me the table on, then I want to select, and then we do alarmed expression on. Then, after the London expression, we're selecting a new object. So we're literally just building on object on the fly. This is just a new object that we're creating for own purpose on what I wanted. My object is a property called I D, which is going to get the value that's coming from the database with an I D comma, another property called name, and it is going to get the value coming from the database with the word name on. Once again, these are aliases, right? So I could have used anything here. I didn't have to use idea name again. I could've used car I d right on. I could have used car name. It doesn't really matter. So it's up to me to know what I am naming these. Once again, these are aliases. If you are usedto SQL on, then for as many properties as they're coming back and I want and I want to create an areas I could just see comma on do the same Alias equals lambda expression dot Whichever column I want for Lambda Vera brother on. Then, after doing all of that, I noticed all of that happened inside of the curly braces. Then I would end with a semi colon, but then I would probably want to do a to list also, so I like list so and trust me, it makes it much easier to just when you dual inquiry. Just make everything, at least afterwards. If you're selecting, expecting a collection off results right on, then some people would actually bring this into multiple lines, so they would actually do something like the quarry here on. Then they say that select in another line and then something that would even break that coach a bit more. I won't do that. Wonder So that is how you would go a boat building upon Alias sing systems. So this situation will be more like Select on Then I would say something like i d as car I d. Comma name as car name from types off cars. All right, so I'm showing you that link is very powerful and it does create some wonderful abstraction from the typical SQL while maintaining c sharp chord. It does take practice. It don't stay getting usedto. So if you have toe kind of do it toe, be able to read the benefits off what it allows you to do, right? So if I do start again on then we look at over greed. We see that we're getting back. I d We're getting back. Name on. We're only getting about. Those two were not getting about that third column again, and that's once again because I specified what I wanted to select. And even though I changed the names, I still told a great view that the I D ideas should be the column name for the 1st 1 I name the column name for the 2nd 1 so I'm just going to put our break point. Um, at the point where we're sitting the data source on I want to close the window and I'm going to load it again on. Then I'm going to look inside off my cars variable where you see it's coming back with five . And if you look at the object listing, you're seeing car idea and car name. So we created custom objects that we wanted to create a list on then that that list of custom objects is being used as a data source for or grid on. Then we set the column. Names on then or grit is displayed, so I'm removing the break points clicking. Continue on that is the all come. So in the next video, we'll start looking at how we wire of these buttons for adding to the database editing Ah, car record on removing ah car record
27. Managing Database Changes : in this video will be exploring making some changes, store database and hope making those changes in the database will reflect in our e d m X diagram. So in today's activity, we're going to be adding some more fields toe our vehicle stable. Right now, we only have an idea, and we have a name, so we know that I d is a primary key on that's all. The incriminating is just keeping track of all the records on Do have a name, but in reality for storing vehicle information, we need more than just a name. All the things that we would probably be storing would be like the year the make the model . So that would actually split name into into make and model make being the brand on, then the model being, you know, the the type of vehicle off that brand. So, for instance, would have Honda Civic right Honda being the Mick on civic being the model. Maybe you have avian ally simply it's number etcetera. So maybe you have more or fewer things that you would want to store in your database. But today will be modifying our database to accommodate those additional fields. So going over to our SQL management studio, I just went to the database, phoned Appropriate table recollect and said, Design on then we are here in the design view where I can add or remove columns as I see fit. So I did say that I'm going to be adding some I'm went to see Well, I'm not going to replace name with year. What I'm going to do is replace year certain name with Mick right now that I'm going to add a model. And it's also going to be environ are 57% to copy this because it will repeat itself. We'll have the VIN. That's the vehicle identification number. That's one of those government things. We have the license plate number, So license plate number, which is also a bar chart 57. Though it's his number, we're not doing any math with it on my film. My general philosophy when it comes the database on numbers would be that if I'm not doing math with it, I don't need to store it as a numeric leader type right on. Then that would actually facilitate it if maybe I start off stirring just numbers and then it becomes alphanumeric. After a while, then you would have already not bean a numeric data type on. Then the final one would be the year, which I can just set that one to be. I'll just sit that want to be integer. Alright. So indeed, you're because you're 2020 or 2018 etcetera. That would be kind of fun numbers. I'll use that one to be an integer. So after making those adjustments to the database and you know, to South something consistency. So while they're not a big deal, I like to keep my, um you know, my naming conventions kind of consistence if I'm using caps. Amusing caps if I'm not using caps and I'm not using caps, so just to normalize everything, I just want to make everything start with a capital letter on. Then I'm going to save on. Then this is going to trigger an update to the table. So in the database, my eight a table no has new fields. And if I go and select the top 1000 then you're going to see all off the rose appearing with all of the vehicles that we had to date all off them, appearing with the make the model and the additional fields that I added. So here in the make what was name is no the brand, right? So there are a few that maybe we'll need some refinement because there is no brand name Bogey, really and truly body is just something that we call the Volkswagen Beetle back here in Jamaica. But then, that's his boys. So we cannot be that later. That's part off the task at hand, but ultimately you see that our database structure has been updated on successfully. So, however, when I go back over to my application, you're going to notice that the table itself, even if I refresh, I think there's a re fresh here while there is not. So even if I was the refresh, this would not update. So I actually have to go through some process to get this data model updated. So we're going to start off by right clicking in the empty space we have the media makes documents or diagram open once again were I click in the empty space and then I'm going to say update model from database. When I click that we get back. A similar menu toe would have won. When we're creating the data on, then I can go to refresh, click on tables and click finish. Once I do that, then you're going to see some updates. But then sometimes this doesn't quite work out exactly how we envisioned it. Because if you notice it retained the original idea and name on took on the additional fields on. Then it took on on additional property, which we don't necessarily want. So sometimes is actually easier. Having updated the database to actually just select all the tables are all the entities in the model on remove them side has highlighted them on I press delete on. Then it will ask me, Are you sure you want to delete? I can say yes because at the end of the day, that is off very little consequence that the database itself off course if I build right, No, I'm going to get some errors because anything that was referencing those models is no Winchester on air because they no longer exist. All right, so here I just did a bill that I'm seeing a bunch of errors because I'm making reference toe classes that no longer exists someone to right click again on. Then I can see update model from database once more. On this time, I would use the other. So because I just deleted all the tables know the engine is realizing that. OK, there are tables in the database that I don't in the model, and that would be my two tables anyway, so I can just click tables just like what you doing The work rate in the diagram Click finish on it will import the new tables with their new columns and there that looks a bit better. So we're getting back or two tables, and we're getting back types off car with all off the feels from the original sit for rather from the modifications that we meet. No. Having done that, we actually have to go back and refractor summer for cold because would be making reference the columns that don't exist. So if I go over to my manage vehicle listing that CS form on guy, look, I'm seeing that here when we did our special select we are friends in q dot idea with a common I and cute name. These two no longer exist. Actually, no, we are actually selecting que dot i d with a capital I So there ago, Curator. I used to capitalize on the case. Those matter, however, you set up a columns in SQL or in the database itself, Right? So remember that when we were designing it, I went and changed the common I i d toe capital I I d had changed the name from when I changed the column name to make. So those little things do matter. So because I wrote court of facility the common I I d on then fascinated a column called name Have to go back and make those changes so I don't want name. But this time I want Mick right In this situation, though, there are more columns that I want to display because this cord was populating my grid view . So I don't want to just see I d on being as a matter of fact, I really probably don't want to see idea tall. So this time I can actually modify this. Some just want to comment Oats. This existing cord on I'm winter rewrite someone to save ire cars is equal to my db object dots the TV that I'm interested in types off cars. And they are just brake line right here and see dot Select on. Then I'm selecting. Well, I've need my lambda expression first. I'm using Q again on then my arrow on. Then the columns that I'm interested in this time are cute dot Make comma two dots model. Well, sorry. Actually need toe specify the new object from our but So I started listening on the columns that was around. Prior fault I need to say is I want a new object open curly breeze on. Then I start listing out each one with its names. I would say Mick is equal to on day I se que dots, Mick. Comma model is equal to Q dots model. The Vienna is equal toe. Chew that Vin. Right? So remember that when we're making our new model, I can determine what I want to be the name off the property. But then I'm mapping it. This is my name that I'm giving it on. I'm just making it be equal toe are mapping its toe value coming in from what is being selected from our table. Right? So I'm just feeling all this model. So once again make model Vin coma on. What does? Do we have? We have the year is equal toe chew duct on. That would be Judah year. Andi. Finally, we have the license pleads. Number is equal to huge dot license plate number. All right on, then I was just going to break line at license plate number because it's going off screen. That's the only reason I'm doing that. So if you don't want to do that, that's fine on then I'm going toe end with my semi colon. So they receive Is our studio actually formatted it for you? Because they're allies that okay, you're going off screen, You have a lot some does, putting it back the way visual studio wants it on. Then at the end off that select, I'm going to see that to list because I like working with the list data type hard. So let's review this one more time. We're selecting from our table types off cars on. I'm selecting a new object so ever lambda expression on. I'm selecting into a new object that has feels make model Vin year on less simply number on for each off these fields. I'm just making sure that I'm back the appropriate value coming in from our database, right? And then after selecting all of them, I'm just converting that into a list. So when I actually run this and I'm just going to comment on these two lines, remember that we did these two lines because you wanted the column names to kind of like human readable. But look at what's going to happen. So I'm going to come in these two out, all right, so I'm not renaming any columns. So after selecting cars into this variable, I'm then sitting that variable as the details source on my grid view. And then I click start, go to my manage vehicle listing, and then you're going to notice that the Great is actually generating with all off the columns coming from my model. So Make has its own column. Model has its own column. Vin has its own column off course. They were all empty in the database of there are empty in the grid view, but the point is, I used a custom. Model two generates the columns for the grid view. So at this point, we can actually go back and augment or design off or vehicle listing. Know that we see that we have a great view that probably needs a bit more space. You probably don't want your users to be scrolling from left direct to see all of the details on the car so we can rearrange the design so that the grid view has a bit more space. So let me just exit this. Go back toe our design view on. Then what I'm going to do is expand this table will expand this this window a bit more, move the buttons so underneath the grid view on, then I'm going to expand the grid view and make it a bit wider. Right? So, no, that its wider Let's see how that looks. All right, So that's what my own looks like. It no feels all that inter grid on off course, the more rules that appear more cars you have is the more that this will expand horizontally well vertically. Sorry. Now, in just to revisit or add her entire record window, another modification that you may want to make is to the drop down list for type off car so you can see that it has cannot reverted to that. Um, well, weird looking Siri's off off items that we saw when we're just trying to populate it so we can just go back and revisit that court where we were actually binding. So I just want to add a rent a record flick correctly can go to view court on. Then I find a form lord function where we were populating it. So we were actually populating all off the types of cars were binding. The display member, to be name on binding the value member to be idee. So one I want to change this the capital I I d on. Then the value member can be name. But then what I'll do is modify the quarry that was being used to fill the the combo box initially. So what I'm going to do is comment all to this square that is being used another street type. It's I keep on commending them all because I want us to see where we're coming from and where we're going. All right. Someone to save ire cars is equal to on my entity object inside off utter entire card which is formally form one is current entities. So that's another reason I said earlier Keep it consistent because you don't want after guess which name used in which form If you use one name across all forms and you just know that this is the object name that you're using, right? So in this one, we use car into entities as our object name. So our object that times off cars on, then I'm going to do a similar thing where I'm going to say that Select on. Then I'm going to do my lamb, the expression on. Then I'm going to create a new objects and you abstract object on this new object I really need I d to be equal to Q dots, I d. Right. So I do need i d Still for my value member on. I do need name someone to create a mother field called name that is going to move back to name, which I'm already expecting. But then for name, I'm going to expand its orbital with just a cute dot may. And then I'm going to concatenation an empty string onto Teoh onto cute out make and then add on queued up model. So in other words, when we retrieve the records from the database after this quarry were getting back, the i d. And they were getting back some field call name which is going toe have a value off the Mick on the Indy string on the model. So if it's a Honda Civic, then we expect to see Honda Civic appear in our drop down list. Right? So after doing that on guy execute on, then I load the ad your entire record window. Then I went to get an error on do this arrow. I'm going to spirit the details of the era, and I'm just going to let you know that it's because we missed the thought fullest. So I always say that I like working with lists this star very flexible on day, really cause problems. I marchei going to just put in the dot policed that I missed earlier, and then I went to start again and then you're going to see that it runs this time and there we go. So it lords properly this time and you can see in our drop down we do have our what we had as name is showing again, but we don't have anything as model in our database. So just to prove that it's going to show Honda Space, whatever it is, airport as model, I'm going to go back on. I'm went toe. Modify the records, understand? To go to our table. Right click Say any top 200 just going to do it quick and dirty for no someone to say. Honda Civic. So we have a Honda Civic for parental. Alright on. I'm just going to make sure it gets committed on. Then I'm going to execute again on we see what happens. All right? So knowing are dropped on this. We see the name appearing, but it's really the make space. The model. So the others on top of modern soul you're seeing is the Mick. All right, So that's what you can go about customizing the user experience. Because if I use the looks at this, they really don't know what went into getting Honda and Civic to appear in one land they're just seeing on the Civic. As far as they're concerned. Right on. Then, in our vehicle listing, when I revisit that page, I'm seeing that we have Honda Civic off blank, blank blank because we didn't put in any data for the rest off those, All right, So in our next video, we're going to look at implementing some addition on anything or adding and editing functionality for our records in our car in tow. Records vehicle listing feature.
28. Create and Edit Vehicles: in this video, we're going to start building some functional to behind our buttons toe add, edit on delete or cars from or database before I continue, though I want to show you some change that I made here to the grid view where we were filling ordered view with the custom model or a collection or a list off the custom model coming from our database for the make model Vin year and less simply number on the column for a license plate number would have displayed this in Camel case, which you know is undesirable to me in terms of presentation. So what I did was to go underneath on actually set a custom header tix for the fourth or the fifth column that would have bean in our grid views. Remember that this would be 01234 so the 4th 1 would have won the custom header text art. So in orderto put in some functionality first we need to understand the behavior that we're aiming for early start that I'm aiming for. The behavior I'm expecting is that a user would select a column on, then click a car or they would just click. Add new car on our you know, a new window comes up, allowing them to fill out the form click. Submit on. We're going to make at least the meat on the model on the let's see the license plate number mandatory, so you know you can customize your own validations. But I'm going to put in some amount of validation in that form if they select arrow and then click edit than the expectation is that a window similar to the add new car window will pop up, allowing them to modify the details off whatever record the selected on, then when they click, submit our to see if changes. It will update that in the database in a similar fashion, when the select a role and then click delete, we want toe one asked a Mardi sure what they want to delete, and if they proceed to click yes, then we proceed to delete the car. So in this video we're going to be looking at a few things where we're going to be creating a new form, and then we'll be looking at constructor overloading as the whole weekend past values into our forms that this form will load with a set of values. So in this situation, we're actually going to use the same form for the add new car on the edit. Because it's the same set off years we're building one form on. Then we look at passing values in, as at the time off loading so that we get different. Oh, comes from the same form on different functionality. I want to make some modifications to my greedy view. The first thing I'm going to do is include the i. D. So we did build the custom model. We said make model. All the things that we want to display to the user are over here, and you notice that they're kind of greed out. So if you hover over it, the land that bulb is really telling you that since the custom name that we're giving it is the same as the actual column name that we really don't need, toe same E equals the same column name. So if you just let the lining boat and use its suggestion the new C that it just replaces it, then see that you could just reference it as me. All right, so you could go through and clean it up if you want, but I'm going to leave it as it is. No. So that it's clear what exactly is happening. The modification I want to make, however, is at the I d. Because when I select our role on, then I want to click edit. I need to know what Ron selecting and the best way to know what room selecting or what car I'm selecting is to know the I d off that vehicle. Right? So the same way that we needed i d when we were saving it in at current a report, remember that were to bind the value member off i d. I need a way to find that value member in my far from my grid so that when I click at it, I can do a look up for that particular car based on its unique identifier, which is I d on. Then return that data to the form so it can be up for editing. So what I'm going to do is just continue underneath license plate number on, Then I'm just going to say cute that I d. So I could say I D is equal to cute i d. But then the Lightning ball once again appear telling me that this is redundant. So I don't need to say the idea is equal to the same column name. So just driving my point home or to appease visual studio? I'm just going to see a cute that i d. Just to show that I didn't have to specify a column name all the time, right? So I'm including cute i d into the data set that is being returned, which means that my data grid is going to definitely generate a column for the I D, which I don't necessarily want cause I don't need to see the I D column. So what I'm going to do is underneath where I said the header text for the license plate number. I went to say GV vehicle list dot columns on then I can specify that I want the Fifth Column Cardwell The sixth column or column with the subscript five, which would represent my I d. I want it. I want its visibility to be set Toe Falls friend. So even though it's there, it won't show up. But I know it's there on. I am sitting in there because I know that when I need the value from it, I can get that value. So I'm putting it there, but I'm making it hidden. All right, so let's go ahead and prepare over add edit forms. So I'm just going to record my project at a new form. I'm going to call this one ad that it vehicles or added vehicle, and then that's my new form. So I'm going to explore some new editing, um, techniques in this one. So the first thing I want to do is change the title, So I want Well, I'm actually going to set the title on the fly. So I'm going to show you guys that a lot of these things can be controlled from the court behind, um, at at the time, as at run time, right. So I am using for two purposes adding and editing. But when it is going to be timeto add something, I wanted to say a vehicle, but I want to read it. I wanted to say it it vehicle. What? I don't want it for it to say edit vehicle, right? So I'm going to show you how you can sit that during a run time. Another thing that we want to look at in this is the power off Leo. So if I go to my toolbox on, I go to the containers section and I see that I have a few layers. You have the floor panel, which can It takes things and stocks them side by side automatically of the group box, which is usually used like when you have radio buttons and check boxes that you want associate with each other. You have a panel. I have split container. You have top controls and you have table Leo someone to borrow the table, a vote here, drag it over on, you know, dried over the section that I wanted and you notice that it's just one big box, right? One big grid box. So what I intend to do here it's put the labels to the left on the Contras to the right, so I can actually resize this. And you noticed that the grid that the cells actually resize accordingly. I have a boat that Meachum a database have 12345 feels that need to be filled so I need at least five rolls. So using the black current at the top rights on the corner, I can add arose. I can add columns as I need to go on. I can also edit the rows and columns. I want to add a few rules on. Like I said, I need fire. So that's 12345 So I have one extra. I can remove the last for the side. That was the column. Let me just undo that quickly. All right, Someone toe remove the last roll, actually. And then you notice that, you know, sizing is not so keen. So you can always just drug on resize as you see fit. And this may still not work or to be the best, so you can actually just go to edit rows and columns on. Then you can see the type off sizing one for your columns on. You can use the drop down at the top and see Rose, and you can choose the sizing you want for your rules so you can see they have percentage and you have absolute someone to select all. And then I'm going to say I want all of them to be 50% that and I click. Ok, you see that? Everything kind of normalizes. And if I expand, they're all kind off expand with that. Right? So that's my table e haute on. Then I'm going to drop in the add button, so I just growing over my button click. OK, on, then I want my button to see save changes, right? So I have one button that's they save changes. Andi, I'm going to put that one to the left and then I'm going to have another button that is going to see Chancellor, that one I'm putting to the right. So, you know, we're just building this so you may have different design ideas from me, and that's fine. That's actually excellent if you're thinking differently from me. But if you're not, then that's fine, too. We're in this together, so I'm just collapsing this a bit on. Then I'm going to put in over or label so we know that every form we tried to put a label at the top on. Then I'm going to me this label see ad or edit new car so odd you car right for actually I'm going to leave this label blank on. Better yet, I'm going to make it. See, what I wanted to see is on the more so I'm going to just change its name so that I know the control from the Corbyn somewhere to call its lbl title. All right, So even your labels, Yes. We weren't really naming labels because we didn't necessarily need to call them from the court behind. But I want to call this one, and I want to feel the content relative to the mood that this form is in front. So once again, I'm creating a form I'm calling it at edit vehicle. This form is going to have the dual purpose off providing fields for Adina car versus editing a car. Right on adding and editing are pretty much the same operations. Well, at least the same fields are involved in both operations, so in the toolbox, I'm just going to take over a few labels, drug them, drop them inside off these rules. So you may have You may get annoying trying to put them in, so I'm just pasting pasting so that all of them go into the left hand side And then I just delete the extra ones from the right answer because I don't really want them there on. Then I'll drug over the text boxes. So everything here is really a text box anyway. All right, so text box inside of that one. And I'm just pasting, So I'm filling out all of these with text boxes. Then I modify my labels so the first label would be for the me off the car. The second level would be for the biggest right click properties and the European properties. I can move a bit faster, so the other one would be for the model. And then I mean, in a real world situation, you probably want to give them drop down lists for me can model, cause it on them to be typing in Honda without on each are Toyota and spelling it with an extra e stuff like that. So you probably want to give them drop downs. But for the simplicity off this off these examples and these lessons I'm not going to go into all of that. I just want us to have some key concepts under our grasp. So the next one would be the vin. The next naval would be over. Nice is or are year brother on. Then the final one would be the license plate number. All right, so that it was quick Each label change the property for the text. If you want, you can meet them bigger and more pronounce. That's fine. I'm just going to leave those that we for No. All right, on then. We have the respective text boxes to get the values for each. Now let's go over to the court file and start adding some cool that will make over or form a bit more dynamic. So as is typical, we have our class. We have the name off the form inheriting from a beast last call form. And then we have our base constructor which is initializing the components, right? No, in this base constructor, I can actually start setting other things. So I want maybe set the title. So if this constructor or the form is loaded using this constructor, then I want a title text to see add new vehicle. All right on, then maybe I want the well. I could change the button text also so, as as I said before. We want to make sure that we put name store controls, so I actually skipped that step. So let's go back and put names to our controls properly so that we don't have any problems moving forwards or text box one is really TB. Mick, right? So it's a fixed. Walks for the MEK. Off the vehicle. We have the 2nd 1 being TV model. We have the 3rd 1 being T B V i n Stevin. We have the 4th 1 being TB year and then this one is TV license plate number. So TV licence and others certainly sensitive license and no right. And then for our buttons on went toe have Bt and save right on. Then for cancel, I'm went of Bt and cancel. And then while I'm here, I'm just going to set up those quick events. So bgn save quick event and Bt and cancel click event once again to create those, you just double click or control and it will generate the click event. Now, back in our court file, I'm going to create a second controller. So this is what we call overriding right or overloading. So we have this concept in program called function overloading and in Opal or pecan actually, Overlord Constructors, Right? So ever constructor, that's taking zero perimeters on went toe overload. This constructor on I'm goingto wanted to take a Parmenter off type off car, and then I'm going to call it a car. Right? Are you know what? I'm going to be very specific. And I would just say car tow edit so that we know exactly the purpose off this property, right, so cart edit. All right, So when we load the form with this construct are expected to pass in on object off type types off car on, then we will then use it to fill out the data points in the text boxes as they are. They're already right. So off course, if I'm if I'm loading the form, then I need initialize components. So that has to be there. I'm also going to take the part where I'm setting the title. So if it's loaded with this one, which is for the edit mode, then I'm going to say edit vehicle, right? So if we lord the farm using this constructor, it's the other new vehicle. If I load the form using the second constructor, which is overlord than it is. The edit said vehicle further to setting the title for the editing. What I'm going to do is create a function called Populate Fields. All right, So the cool thing about visas students that I can write a function called I Don't Have a Function called Populate feels I just wrote it. And instead, aligning Bull is coming up with a Red X and it's asking me, Do you want to generate the method for this? So if I click that, then it automatic allergist generates that my thought for me, right? So it makes according much more efficient than it could be otherwise, right? So inside off populated fields, I'm going to be setting the field values toe. Whatever the matching field values coming over from this objects. Actually, I'm going to pass this object in tow populated fields, so I'm passing type of types off car Into that on day, I'm going to make sure that this function also laws off the incoming beat. All right, so I could just call this car just to show you that once again, I'm getting a property arm getting on object off type types off car. Right. So this represents the data from the database on this is the objects that has the data that we're going to be editing in the former it on. Then I'm getting it in this constructor. Um, initializing my components. I'm setting the title off the whole form to be edit vehicle. And then I'm seeing I want to start populating the fields in the form. Here is the data. So inside off the actual function, popularly, it feels I'm getting that object. So I'm just calling it card or socially, that it doesn't necessarily have to see have the same name. Most, however, have the same data type. Because if I said interior, then that would cause an arrow in the function call cause any seeing? Well, um, passing some invalid data types. So the data type has too much. The name doesn't necessarily have too much on. Then what I'm going to be doing inside populate feels is calling each text box. So if I say TB, let's start from the top, make not text. All right? Is going to be equal toe object coming in dot on then the corresponding data that that text box should have, so I can do that for everyone off them. TV model dot text is equal to a car dot model. All right, so I'm going to you can pause here and try and do them all on your own. I'm just going to finish them up. All right, So if you pause and tried it on your own, then you would have something looking like this. If you have an air Otranto put the year into the text box, then remember that you need to make it a string in order for it to go into the text off the text box. Right? So once again, we're seeing that we can load this form normally, right? Or we can decide to call the constructor with some data. Push some data to this. Construct a version off the form on. Then that construct will proceed to populate the fields with some data. I'm also going to introduce ah, private property, and I'm going to call this one. He's Edyta more so I'm just using this flug, which is a Boolean is, and it moored all right. And I'm not even going to the initialized just yet. I'm just declaring it on. Then if it is the constructor without initial data, then I know that I'm not in edit. More someone to say is at its moored is equal toe falls, right? So it gets initialized right there on if it's the constructor with data than I know is that it's moored is equal to true. So for too much, I'm going to be using this flag inside off my Bt and see so I can determine the behavior because it's a different kind of quarry. If I'm inserting the ITER versus if I'm updating data, so I'm just going to track the value off is edit more. By the time the button save is clicked, I would know if it's any more or not. If it's any more than I do an edit quarry, if it's not any more than anyone, adequately, that's pretty much all that I need to do now in my BT and save Button click event. What we're going to do is do like a quick if statements under seeing if is edit more than remember that when we in C sharp, we can write this in shorten on this really translation if is edit more is equivalent to true. That's really what that is asking, right? So if is any more than we want to do something else, we want to do something else. So if it is it more than we want to write it cold here on if it is not any more than we were at add chord here, all right, so I'm just going to leave those commented for no on. Then the chancel click is pretty much worse than to close the window. So if I click cancel, the expectation is that this window will close so I can actually just say in the Bt and cancel click. I can just see this dot close. All right, so remember that this keyword is special and specific to the actual class. It's just an object off the class that it is being used in, so this represents the add edit vehicle window on. Then the dark clothes is the opposite of the dot shows. I'm not sure shows the window that close closes the window, so if I kick, click, cancel, then just close the window, that's pretty much our saying on. Then you notice that visual studio is actually green. All this So it's not every time that you actually need to use the word this so you could actually just see close and it will know that he so close itself. Now we're making good progress. I'm not ready toe. Quite not quite ready toe called the edit and add functionality. Yet I'm instead going to go back over to my manage vehicle listing window Onda. We can just take a quick look at that design once again and we have the add new carbon. And we have any carbon expectations that when they click at new car, we're going to show a new form. So I'm just going to do that. So when they click that, I'm going to see this. Sorry, I'm going to see ad edit vehicle on day. I'm just going to call if I m so or I can just use the suggestion from visual studio. Sorry. And then ample interest is equal to a new instance off at a vehicle. Once again, we could always simplify this by saying vier, if anything on, then I'm just going to say add it, it vehicle dot short. So just going to show that new window and off course you want the MD I appearance because they don't want to just show this. Remember that when we just show the window, it's a free window to do anything. So I wanted to know that it's peer int is the mean window. So I went after at this quarter bit differently where I'm going to see that the ad it object that M D I. Ah, parent is equal toe on. Then I'm going to have to see this dot MD I appearance. So let me explain exactly what is happening here. Remember that we're launching manage vehicle listing from inside off army and window. Mean windows are MD appearance. So when when the manage vehicle listing window would have launched, it would have launched inside off me and window. You would know that mean window is it's MD appearance, right? No, I want to launch another window on the command off the manage vehicle listing, which is a child off me and window. So I needed to know that it is also a child off me and window in the previous ones. We would have said that when you launch and let me just get back to my me and window courts A can, you know, be re acquainted with it. When we were launching money vehicle listing, we said that this is the M the appearance. This being me and window in this situation mean window is not the one being doing the launching. So I can see this is the main window because then that would make manage vehicle listing the parent for my added it, um window, which would be wrong. So instead I'm seeing this, which represents the manage vehicle listing that MD I appearance, which represents me in window. I'm seeing that whatever the MD appearance is off this form that is the parent for the add edit vehicle. All right, so that's all I'm doing in that line. So, you know, situational you just after no hotel Manu fall right in some situations. It's this because the MD appearances make under call. Sometimes a child off the MD I appearances making that direct call. So you just need to see whoever my appearances. That's also your appearance. So that's all we're doing in this line. I'm saying that my appearance is your parents on. Then we're assuring that window. All right, so this is loading our add edit vehicle using the normal constructor in other botton click events. Especially like in the edit. No, we'll need toe one. Retrieve the data off the car to be edited. Onda then passed that data over to the form, right? So once again, the expected behavior is that the person is going to click the grid, click the ruin the grid on, then click edit. Right? So, firstly, we need to know what role was clicked or at least the value or the I d. Value off the raw that is clicked on selected. So here I just outlined some off the steps that we need to take when we click the edit button. So one we need to get the i d off the selected roll. Right? So when the person clicks the rule and then clicks edit, we need to know what is the I d off that role, right after getting the idea off the road, then we need to Corey our database, which means that we need toe initiate. Okay, we have db already. So we have a connection to debate. A. B is already active because, well, that's what we feel. The grid. So I already have that object. So we need to query the database for the record associated with that I d on. Then we need toe launch our window similar to what we did here. Except we're going to be launching it with the data. So remember once again that we have two constructors. We have a normal constructor that doesn't expect too much. You just need knows. It's supposed to add on that we have one that is expecting some data to be passed in on. That is the one that we're going to be using when we're going to be launching for editing purposes are. So let's start by getting the idea off the selected rule someone to save our i d lower kisses equal toe on. Then I'm going to call my grade, which I call the vehicle list on then. I have a property that says selected roles, but they notice it selected rules because that's actually a collection cedar. It's a selected row collection because maybe the person can select more than one rules, right? But we're expecting that it's only one road that they're going to select. And it's the even. If I select more than one, we only want the 1st 1 right? So this is a wild assumption we can call defensively in the future. But right now I'm just keeping it simple. So out of the selected Rose, I want the 1st 1 So it's a collection I can use. My square brackets specified that I want the 1st 1 Then I say dot Then I want off that rule . We have cells. Cells represent the columns, right? So I want the cells, but then off the cells, which is also a collection I'm really only interested in the one called I d Let me have the property So we have more room to write 1/4 so I can use unnamed index. So in some situations, you want the number some situations. You just want to specify the name off the column because if I add more columns here, I can't go by numbers, right. I would have to change my court every time. So what I'm going to do here is say, I want a cell with the name I d Right on. Then, after getting that cell, I want the value in that cell. So all is going to do. We see give me the selected the first selected roar on, then give me the cell called idee on whatever the value is, even though it's not visible, because we should it. But whatever that value is, I want to store it inside off this variable called idee. Know that we know the I d off the rule that was selected. I can save our car, doesn't know only want one is equal toe underscore db dot the table that I intend to get the car from, which is the types off cars dot and they know we're going to look at another function in Link called first or default. All right, for a start default were first on were first our default. So the difference is that first will feel if it doesn't bring back anything, defaults will feel gracefully and give you a nice, sensible error message or just returned, knows so on the first or default. And then I'm going to do what we'll have come to love. No, a lambda expressions. I'm going to say Q. Then my arrow, then all I'm saying is give me the first or default pretty much. This is like a were clause where Q docked on. Then you see that the Q is giving me access to all of the columns. I'm interested in The idea column where the value in the I D column is equivalent to the I D that we just retrieved from or selected greed. All right, so all this is going to do is run it. Oh, and it's giving me an error here because I d is actually an object. All right, so what I would want to do here is probably just doing implicit parsing toe on int for it. So no, this is returning an intent I d on. Then I'm comparing the integer column to the integer value coming back from this operation . Know that we've got in the car. So is either we get that car or we get no, that's what first our default means right on. The assumption is that if they're clicking the grid than that idea is in the database, and then we go on to launch or form with the data. So I'm going to also see Vier on going to use the same object name. And because they're in different functions, they won't interfere with each other on. But then this one is going to be equal toe a new instance off add edit vehicle. But then I'm passing in the data that is coming back from the database in the form off car . All right, so I'm went toe launch a new instance off this window. Are this form passing in the data coming from the database on. Then I can to Skopje these two lines where I'm sitting, the MD apparent and I'm sitting it too short so you can see that they're doing similar things, except that it is a bit more work because toe edit you after do a look up for the i d. For the record to be edited on, then you can pass it on in tow. The new window, right? So when it passes into the new window, I'll just go back over there is going to launch with this construct er which is what? Sorry, This construct er, which is initializing a component, setting a different text, setting the edit mode to be true on, then populating a few so that when the farm Lords. You will load with all of those values already in the text boxes. No, back in our manage vehicle listing. I'm just going to top it all off and do the delete freak event, which is going to do a similar thing. They select the grid, so we need the i d off the selected value from the grid on when toe reuse this score on. Then I need to do a look up for that vehicle. Right? So I do the same thing there, But then this time I'm going toe delete vehicle from table threat. So after I get the vehicle that is being referenced by that selected role that I'm going to say underscore d d dot types off cars dot Remove this time. So when we're inserting data, we do add, when we're moving, we see Dr Moules on. Then I went to pass in whatever it is I want to remove, which is the car that we just did I look up for on then after all of that, I'm just going to say db dot Save changes hurt. So when we select something from the grid and then we click delete car are the delete button, it will then remove on. Then I'm going to do one more thing where I say that the grid view should refresh. Right. So I'm just going toe Refresh the grid view after this has occurred. No, Back in our add edit form, the first thing I want is my connection to the database. So I'm just going toe copy the existing chord because once again, I want to keep it consistent this time our own. So I'm just going to copy this on, bring it over on, then I'm going to do the respective initialization. So this might be like a double step at this point, An ear saying, Well, I don't really want to do it this way on. That's fine. If you can find a more efficient way, you feel free to let me know, Um I mean, there are more efficient with I'm not saying there isn't a more efficient way. But if you feel free to let me know if you do it a different way right, you can actually just do it in one line just like this. Andi got out the double work, right? So you could do that. We and have it initialize all in one line. That's fine. What? I'm just going to leave it like this, since that's a pattern that we've been using upon to know. So after initializing it, what I'm going to do is go ahead on right in the court for the editing, right? So after we click save what we need to do is tow build, a model that represents what was edited on. An important thing to note is that when we're editing, we need to keep track off the I D off the record that's being edited. So we have skips that step because here I said, all off the text boxes to have the values. What? I did not say it was a value for the object being edited. So, no, Sometimes you do something, you make a decision, and then you realize later on that you missed the steps. So that's part of the development process. So I'm going back to my design. I went to my toolbox on bringing over a label on dime, just going to call this label. I'm going to blank coat the text. I'm also going to make it not visible, so I'm going to change the property Visible toe falls I'm also went to change its name Toe l b ah, I d. All right, so that I know exactly what to call it in a court behind. So one of the first things I'm going to sit is actually the text for lbl i d. So I'll be I d. That takes is equal to car docks. I d right. So we're getting the car object on idee is coming over all So it's seeing accounts that the strings have to do it to string because I'm storing it inside off a text verbal red instead of a string variable. So that's all I'm doing. I'm just sitting at label toe Have the i d being tracked so that I know exactly what I d belonged to the record that was being updated. Now that I have all the data that I need, what I'm going to do firstly is look up the record that was being edited. So, like I said, you always have to do these Look up. So that is why it was important for us to track the i d. Because at the time off we are speaking Save I can get the I d from my form so I can see var i d is equal to on Then I can get it from label the text which we know is a string. So that's a string. I want a V. I want a indigent variable Our interview value other Someone's out to do an in thought parse I can't do the implicit conversion because that doesn't work when it's a string being convert right Second was in dark Pires what is in my i d label on Store it inside off that on then I can do Ah, look up for the car where save our car is equal toe And I'm sure you're just completing my sentence are typing it'll before I do where I say db the types off cars not first or defaults Anytime you want one record, you can use first or default Get over Lambda expressions open running on then sake you dot i d is equivalent to the i d. We just got back from our form No, we have or car object on. We're not entirely sure what was edited because we are five feels five text boxes we don't know if there were populated. If the visor deleted, we don't know as yet. So what I'm going to do is just a car not on. Then see, for each one off them, Cardell model is equal to the text that is inside off TV model that the TV model text box right on. Pretty much that's all I'm going to do for all of their SOCAR not make is equal toe whatever values in TV, Mick dot text. Right. And I'm going to continue doing that for all of them. They can pause here and finish them. You find that as you go along, you may have to do quite a few data convergence to and from so up top where to convert from integer to string to get it into the text box and then below we have to convert from string into integer to get it back into the database after filling out All off the properties are probable value. So the only one I'm not sitting his i d. I don't need to set the i d because the whole operation is based on the fact that idea is the same on the idea would not have changed right after doing all of that, I would just say, db dot save changes. And then this would submit all off the potentially new values, even if they're old values. So even if there was no real change made, it would submit all off those toe the database for our new car. No, a similar operation would be done for adding some just going to do, adding quickly where I'm going to build a car. Models, I would say something like Vier new car is equal toe a new instance off type off our types so types off car. So remember that this represents our new table, right? So it did something similar to this With our car entire record, we will build the current Eric or no, we're just building a new type off car record. Are types of cars records. So we say new types off car. And then I'm going to show you a cool feature that C Sharp does word that lows us to just use our favorite embraces on Philo. The column names in one good sweep in one full secret, so I'm initializing my object to a new instance off table to be populated. And then I can fill out each column so I can see the license plate number is equal to whatever values coming back from the license plate. Textbooks understand to copy and peace so I can move more quickly on then make is equal toe . What are our values going from the text box for the MC and notice that there are comma separated model. Sorry. Model is equal to the text box TV that model that text. All right. And then we just go along and fill each one vote. And I'm just shark alternate years. I can move a bit more quickly. TB Vin, not text on Dyear will year would no have the in the par stretch. Second this Copy that Gaza coat It already exists. So know that we filled out a new record for types off car in order to add it to the data BCE You know that we need to see on the score db dot Find our table dots odd. And then I went to pass in my new model which is new car right on then at the end off any operation like this, we always need to save changes on that's it. So if they click on Save and we're in any more than, we will change the maps of the changes made toe on existing record, So we do a look up based on the I d on. Then we've me, you know so but the changes to that record that we looked up and then we see the changes on then if we're adding a new car, meaning we're not in any more than we create a new model off the table type or the class type to be added to the tables, right, we fill it with the data on. Then we added to the collection off records on, then save changes. No, we can test our changes test all off the core that we've written Onda I click start. So I go over to my manage week listing agency that I already took the liberty off, editing one off them before. So let's just look at this experience. Let's start with adding a new car. So when I click at new car, I can no add a new rules. Rice Onda. Let's see that the meek would be phantom. The vineyards wanted before. For six. It is Ah, 2018 on. Then the license plate number would be each G fast. 64 seven, right that we click save changes. So I mean, I click save changes. The window is still open on. But then, if you notice that the label is seeing at new vehicle because we clicked, add so it's setting the title. Well, um, we didn't see that anything else should happen after seeing changes. So those are some refinements that we need to make as we go along. All right, so the window is still up. That's one onto the grid is not sho
29. Vehicle Data Validations and Exception Handling: Hey, guys, welcome back in this video, we're just going to review some off the changes that would have made since the last time one word creating over add edit form for our cars. I'm going to start off with the manage vehicle listing window where you realize that I added what I labeled as a refresh button. So this refresh button will just refresh the great each time a change is made. So let us take it for a spin. Firstly, let's start off the add new car. So when adding a new car, I added validation to the form to make sure that at least a make on the model our presence . When I click save changes, then I'm going to get this. Please ensure that you provide a make on the model. So if I'm going to add a new one and let's say I had a Suzuki Onda Swift on, then I click save changes. I also put in a try catch because then year is expecting our values. We can parse into interes when I click save changes. Then my my arrow comes up from my exception, right? So then if I provide a change Ah, year. Sorry. And I see 2013 on Dwell. Those are the only three fields that are absolutely necessary for it to work and click Save changes It put in another message box. That's an insert operation completed. Refresh. Great to see changes, right? So we did discuss that we wanted to manage the users experience between adding or modifying data in the car on getting back to the grid. So after displaying this message box and click OK, the window will close. But then I can click, refresh, and then it will display the new field right on. Then, if I tried to edit without selecting. So even though you may see it highlighted Blue, it's not really selected. So if I click edit car, I get a nasty era. But then, once again, this is just a try. Catch catching that arrow that would have been an exception showed me that you know, the index was not offering just basically saying that I didn't select anything from the grid, um, control right? But then my program does not stop executing, so I can always rectify that by just clicking on. Let's start with the record that we just added, when I click Suzuki Swift Click Edit Car, then it brings up edit vehicle. So I made sure to modify the title that comes up. So let me go back to add car when I click a new car to see inform. But one the label changes and to the title in the window changes. So to any typical use, I could have been two different windows. That's not really our business toe worry about, so I'm just changing into the user. Feels like it's the same window at all times, while it is the same with about the feel like it's a different window. Sorry at on. Then what happens that a functionalities like the difference also. So we're getting the load, the data preloaded. I can change the vin on the license plate number, all right. It's just put in some random number click save changes, and then it says update operation completed. So it's a similar message. I click OK, similar behaviour and in a refresh, and then I get the brand new data. No, deleting is kind of tricky because you can't delete related data, meaning If I select, say Subaru, which I do believe I have a rental record Two Represents were already. If I click delete that, I'm going to get some arrow saying that, You know, I have to see the exception for details. When I checked all that arrow, it was really because I have a rental record on Suero, so I would have to delete all our entire records for the Subaru in order to delete the super. And that's just referential integrity coming over from the database. So that's not necessarily a bad thing. So if you get that error, that's really what's happening. But I just created Suzuki Swift someone to delete it, because I know I have no, um, no records associated with it. So when I click the leads, it will. Oh, well, it's actually giving me an error. I did not expect that. So let us see exactly what that era is because I really didn't expect that one someone to go over to my cord West Lindy boat moored Go to my click event on, then I'm just going to select at the point where it's supposed to actually remove the car, and then we're going to try that operation again, so I click. He jumps that break point where it retrieved the car successfully. So it knows that it's getting the Suzuki Swift all right on. Then I can use F 11 to step through. So goes to save changes, at which point it throws the exception on. Then, if I look in the exception object, it is. Look inside off the inner exception for more details. Write C inner exception for more details so I can look at that on. Then it's seeing that I have ah const re, ain't that conflicts with my date of beast? So I definitely have to drink that old to see if there's any rental record with that, um, property with the car, sir, with that record in the detail beast. So there is no related record. I just checked. But what is happening is that it is enforcing some design constraints. So in the design off my our car into a record or well, in the design off this table, four types off cars on the I d or on the relationship. Actually, there would be this uncertain update deletion on update rule. Right? So basically I can sit no action or caskets. I would have had to set Cascade for it to just say OK, I will delete. The danger to this, however, is that if it deletes the record, then it will delete all associated are all related records. If it leads car with I d. One than ever, rent a record that is associated with car with 81 would also get deleted, which you may not necessarily want, so you would have to gauge that experience with your application. However, we do see that you know, we can probably go by the delete for no, and we fix it at a later date. But I just wanted to get across the fact that we could, you know, modify our application to do some crowd against the vehicle listing. We could create a single form that has multiple purposes on. We can write some cool chord in our court file toe handle the experience that our users have, So after finishing up or manage vehicle listing, will probably want to turn our attention to finishing up the entire record where we create a view similar to this, where we can view all of the rental records coming in from the database on be able to modify them when needed.
30. Manage Car Rental Records: Hey, guys, welcome back in this video will continue putting in functionality into our win forms application or most recent set off tasks. Had us do our manage vehicle listings where we could add a new car and I'll just add one for sample sick on. We know that when we click save changes, it would get added to the database, and they would have to refresh the grid to see or a new car off course. When we select the role and click edit car, we could change this. So if I if many was supposed to have two ends, then I correct it. Click save changes on Once again refresh the good so we can look into automating that Gregory fresh in a bit hand. I'll be doing that in the the current record management. But just to finish up or recap, the final thing that would want to do with our vehicles would be to remove them. So if I select a role on remember we discussed that if there's our entire record already associated with a vehicle, that's the database would make it a bit more difficult to actually delete the vehicle itself. But I can always select this vehicle lead car on. Then it would okay, and I refresh. And then I see so would want to automate that refresh because just know the car was removed , but their greed didn't show me. It was removed. And then I tried again. And then I got an arrow. All right, So what we want to do is have a grid, like sitting in the view archive on. Then this would otherwise to kind of do those crude operations. And then I'll show you other things, like automating that greedy refresh. So to get this party started, the first thing I want to do is create a new form that will serve as my archive window for my entire record. So I go to my solution Explorer, right? Click in the solution. Goto Odd new item working. Just go down to form. I went to call this form, manage your entire records and then click, add. And once our form is, I did we do our regular changes where we go toe properties on, we change the text in the header, Toby more, you know, more human readable. We make it a little bigger and then I'm going to borrow the same elements that I had over on the manage vehicle listing on winter bar each and every one of them. Some just been Tobar the label on I'm Holding on Control while I click. So I'm holding on control while I click the refresh button. Agreed the Three Buttons. Then I'm going to control Sea to copy. Go back over to my design on, then control V to Paste after peace thing. I'm just going to change this label text. So I quit the label Water Properties on. I'm just going to change just the manage our entire records record. It's all right. This speaks May spelling errors on. Then we're good to go. So manage their entire records. That's our new window on then off course from the main window. I need toe set it up on the click events or persons of close all the unnecessary windows. Right now go back to Miami in window, gets the button on. I'll save you archive. I'll set up a click event on. All I'm going to do is launch this new windows. I'm just going to copy score. I just reacted throat, So var a manage rent. Uh, records is equal to new manage Trento Records as my class. Then I will do that. MD I appearance is equal to this Onda. The window should so all right, after copying over these things what you want to do off course after renaming or label, we want to change out our buttons so right? No, they still have the same names from the previous form on. That's because, well, the same name can exist on two entirely different forms. So in two entirely different classes, sure, you can have the same names. What's contextually? I don't want this to be Bijan at the car. Would wanted to be beaten. Odd, maybe record. All right, Andi, just the same way. Anything that's his car for these buttons should see record, so the text should be add new or entire record or record. How? Just the record on. Then I'll do the same for the edit, so I'll just change those. All you can do those on your own. All right, so by then of that exercise, anything that's that car should know. See, a record also might want to change the name on this greed just to make sure everything is consistent cause this is GV vehicle list. So I wanted to say GV record list. All right, so know that we've, you know Oh, I think the refresh button may also need to be changed what it's beat entry for, so that can stay. All right, now we know that we need to click events for each of these buttons. Someone to just set up those click events from No, by just double clicking each button on generating the quick events. So have the click event ad click event for the record and cricket of and for the delete already have a window that adds, So I can actually just write that quote, right? No, on. We kind of followed the same pattern as we did when we were doing the manage vehicle listings. So if I go back to that click event for a new car, you see that what we did was us launch the window, tell it's that the parent off the launching window is its parent also on, then shows I'm goingto father that same pattern for the odd on Add beaten at a record freak . All right, there we go. So when we click the beaten at record, it will launch the same window as we would have launched if we clicked from the menu itself . Aren't so. We're giving the user to options. Of course it's your application. You can style it whole you would like. But that's so I'm doing it now. If you pay attention here, I don't know if I would have gone through this before, but you see those three dots underneath the new If I hover over it and click on that light bulb that appears, they showed me that I can actually simplify the initialization or declaration of this object, right? So if I click on that, then you're going to see a different way to initialize object, which would have seen when you were using the link. Um, court. Right? So it's universal once ever class, and they are setting properties off the class after initializing the object. You can actually do it like this are you can do the way you're comfortable doing it. And that visual studio are rare injured for you. No, for the rental records were going toe have to write a lot of similar court what we're doing when we're doing manage vehicle listing some actually. Just going to be copying and pasting a lot off the court because whatever in a lot of authority. So I'm pasting my connection to the database or my entities object on the object initialization which went inside off my constructor. Also, I would have set up the Lord event for the form. So I need a load event on this form, so I just go and double click the form itself on that gives me a load event on then inside off this load event for the magical listings out of tried. And I'm just going to copy all of this because I mean, I'll be retyping very similar court anyway, So I'm I copied overpopulate grid. But of course, I don't have a method inside off this new form called Populated grid, so I can hover over it on click that light bulb. And then this is generate the my thought for me, which Okay, that's good. Can remove that generated line on then for the populate. Agreed. I'll be doing a similar quarry. So let's just right all the quarry for the populate greed for our manage rental records. So Of course, the first thing I want to do is declare some objects are some variable. I just save our records is equal to on then a column, a database object dot car into records, which is my table on. Then I would say that select And I want to be very specific with what I'm selecting someone to say select. Open up my lambda expression using Q as my object or my talkin on. But I'm bloating into a new blank object. All right, let me just put a cynical on here so I don't forget later on on. Then I went to say what it is I want to select. And what that column name is our So the columns that we intend tohave would be like customer. So I'm just going to say, customer is equal toe cute dot on our own customer name. Clay. There on, then date in. So here I can actually use my own names for property rights. So date in, he's going to have the value off q dot data into Sorry date. Oat. I bade is going to have the value off the veteran Ted. All right, on then. I'm went to see comma date in. Since I have the value off q dot Beat returned off course I need the i d. So I d is going to be equal to q dot i d All right? Aan den The last one that I would want is the cost so cost his equal tokyu dot costs. But then you notice No, that off course I need to say the type of car What car was barred. So is he cost this kind of grade out? Everything else is, you know, strong and white. What? This is greater or less because the property name. I'm giving it to the same as a property coming from the database, so I can actually just remove that. And that's a cute out costs. Give me the costs and I need the car. No. If you look back in our database and others go over to the database quickly, you see that we have the type off car I d. I have a table for the types off cars. Sure, but then I have the name and the i d over in that one. So then you just select top one. Those under our let me just look at the design rather so that we can see the column names. So I have I d make model Vin license plate number and year. No. In showing the details of the rental record, I probably wouldn't want to just show the idea because I d car i d. One means nothing to me. But that is the property that I have access to inside off Meyer entire record. No, in a typical situation in like in SQL syntax out after use an inner joint statement to actually get the details off the car being referenced details meaning the meek on the model . Right? So if I wanted to say all of this information and then print the car details karmic on model inside off a quarry, then I would have to use an inner join link allows me to actually access the property. So let's say I said car as my property on then I wanted the meek on the model off the related car or the car related to this record. All we really have is the idea, because it s a que not they're going to see type off car I d. Once again that is not useful. So I can kind of simulate a situation where need an inner join and any details from the related entity or the related record by seeing I know we see the use off this types off car property. So remember that we had the types off car I D on. I was saying that we have inside off the relationship. What link does is give us access to all related objects off the but so I can actually say q dot types off car. And then they tried to make it single or sometimes awkward, cause it's types off cars. That's the name off the table. And in this it's types off car because saying that I know only one car is associated with this rent I record. So it's a car, right? So they try to, you know, modify the the 10 not attends the singularity and plurality off each property just to indicate whether it's a singular or multiple. Right? So if we're looking at the types off car table, types of cars stable than Lincoln shows that we have our entire records because one car could be in multiple entire records. But then Since we're looking at the rent I record, then we no wonder, entire required, is associated with with one car types off car in this situation. Oh, that's not too confusing. But the more you use it, there is more. You realize how exactly it works. So against a que dot types off car, which is a class representing or the entity or the related record once again off the car details so that I can see types off car docked on, then it gives me access to every single property that would have bean in the types of cars table anyway. So I did say I wanted the MC on the model so I could say car is equal to cute types off, card out Mick on, then concatenation that onto que not types off car dot model. All right, so when this runs, it will give me all of the's properties basically from the database table. They're all right there in the database table on that. It will kind of to an inner join to say OK, give me the details off the related record from the Types Off cars table and give me the Mick space model on story that inside my custom property called Khar All right, So the expectation is that when this grid lords were going to be seeing all of these details in addition, toe the details for the related car and then when I was premature with a semi colon off course, After all of this, we always do a to list just to make our lives easier. All right, So the next line, which I'm just going to go back over to manage vehicle listing, would have me set up the data source the header text for anyone off them that I think is too cumbersome to display Aan den. They hide the ideas. So I'm just going to copy these lines on. Then I'm just going to meet them relative to this situation. So actually, I don't think, OK, dating and date. Also, I definitely want to change those. But before I do that, I need to change all the references. So I need GV on that its record list. So it's no longer vehicle this, but no, it's record list. Some assumes the change, the result, the data sources, not cars, but its records. All right, on then the header text. So no, I can actually say which column header text. I want to cheat and jobs, so I can probably use the named columns here because I'm not so confident about the ordering rather ordering my change at any time. I mean, introduce or remove. So if I don't say four like I didn't in money vehicle is so it worked here, you know? But then if I modified the arrangement of these columns than 4 may not be life simply number anymore, right? So instead, off referencing it by its subscript or its placement. Instead, I went to reference it by its name, someone to see that the column called Date Oats are did in It doesn't really much are just don't get that under one for in our own for out. All right on, then, the date in one. I wanted to say d it space in on in the data out when I wanted to say the it space boat right on. Then I want toe once again hide the column for the i d. All right, So with all those changes made, let's at least see if our great lord So I'm just going to start off this application there went to manage rental records on Click View, Archive and Fallen. So our Grady's loading and notice that we're seeing the car record Subaru on Ah Honda Space Civic, right? So just a verify I can actually all have multiple windows open. So I just expand the applications. They can see both the vehicles listed to the left on their entire courses. Right? So we see that Subaru is really just the meek. So that's what we're only seen. The word Subaru, whereas Honda Civic make Andi model are displaying the details accordingly. Right further to identify, click add new record. Then we see a new record coming up, allowing us to put in a new record. So I'm going to put in one test. Driver cost is $1500 on DA. The date returned would be, Let's see the first off march. The type off car is the rules Royce Phantom I submit, and then everything goes through. I can modify this window. No, that it closes afterwards, All right, so that we don't have somebody clicking submit twice, but once it close and we click, refresh where there's no refresh here so we need to wire that refresh button up, but and we did say we're going to try for an automatic refresh. But by closing the window and reopening it, it rear end that query that populate grade worry on. Then we see that our greatest populated. So I'm just going to quickly wire up the edit on the delete record, um, functionality. So I'm going to go back to my core that we wrote for the edit car listing, and I'm going to distinct all of this school because, literally, it's going to be very similar on I'm going to place it inside off the function on. Then I'm just going to change all the relevant references, so it's not GV vehicle is anymore. It's GV record list, and I'm getting the same rule with the same cell I d. Looking inside off Corrente records This time, though not types off Karkare intercourse is our table, so db car into records first or default. Give me where to common, I de so you may have some little cosmetic things that change owt on the variable here is not car, but it is no ah record. All right. And then we're here launching at edit vehicles. Remember that we did add edit vehicle as our window toe, close the edit or add a vehicle. We started off with our current I record. So we're going to have to make some major modifications toe. That's to get it to work. So I'm just going toe comment. Oh, these lions for no. All right on. We'll get back to them. We're going to have to do that in part two. I'm just going to go back and get that delete cord on. Get the try, Catch everything, bring it over me. Those cosmetic changes once again. So it's GV record list are not GV vehicle list. So I'm getting the same column. Different table once again. Car into records. Come on, I I d on. Then I'm seeing remove the object and this is record, not car. So remove the object record from the table, save changes on then I could just recall popularly it grid here instead, off doing that reefer. Shrek, it's after delete. I just recall populate grid, which should rear end up Quartey. I did allude to us doing it in prior to what I'm just going to push ahead and finish this up in this video. So we need to make some modifications to our window that we were using toe add our entire record. So we call it at our entire record. I'm going to rename it Toe Ad and it our entire record on by renaming it and saving it is going to ask me if I want to rename all references. Click yes. So that everywhere that it was making reference to add her entire record, it will no say at any trends are record off course. I need to modify the cord, some just going toe right click, save you court. And that brings me directly to the view file. But I'm also going to bring up our added vehicle court files so that we can see them side by side once a day. Again, I'm going to make some very similar modifications. I'm going to take most of the properties that I had an added vehicle on place inside off at any rental records, starting with the is edit mode because then we need to track if it's edit mode or at moored . You see that we had toe constructors here, so I'm going to take all of the's properties. Are all of these lines off cored that were inside off the just remove what's not necessary . So I had car rental entities as the name here. So to keep everything consistent of as I've been saying before, I'm just going to rename it toe underscore db All right, so it's better to just keep everything consistent. Use one variable name across the board so that everybody knows who they are. Right, So have lbl title. I didn't give the label inside off this window a title, but I don't say add new her intel, Onda, the text off the window should also see at new rental. On is anymore This falls. All right, on then I will declare another constructor second astride cto are stopped twice and then it generates another constructor on. Then I will put in the object off on your entire record. Second to see car Trento record another see record. All right, so remember that when we're doing the added vehicle, what we did was create a second, um, constructor that I loaned us the past in the record that needed to be edited in this case it was a car to edit. So to keep it consistent, I can say record toe edit. All right on, then. Once again, I'm just going to copy most off this with all of this cord since is very similar once again on, then I'll just change old. So this is it? It's a rental record. All right, let me put the record so that my sentences are complete. Throwing through so her entire record it it Parenteau her card on. Then this is record to it. It's not hard to edit. All right on then, Popularly, it feels that is Ah, that is something that we need to load up. So I'm just going to generate a method for that on then go back to add vehicle on. They will see all off the fields that were populated. So we're probably going to have to do that part manually, since they're two entirely different forms. I'm not going to copy the score, but I'm going to still follow that pattern. All right, so then what I'm going to do is scroll down to the beach, one click saysi even these things when we're starting off. I was starting off simple. So I didn't want toe complicate everything but know that we're more advanced, that we're going back to our old Cold War allies that we have some gaps. So, like, but no one on the score. Quick, I don't like this. Born one on the score. Click. We didn't name the button at a time, but, you know, know that we're naming buttons. You can probably go back and give it a better name on change of the name off the button click events that is associated with it so that your court is more readable. And you know, what I'm really here for is to get back these fields. So what I'm going to do is just copy all of this so that I have all of the fields that need to be populated on. Then I'm going to remove. So I just wanted the field names. That's all I really wanted. But I'm trying to work, you know, a bit smarter than I'm trying to work harder. So I'm just getting the relevant parts because I have it all over. So instead of sitting down and retyping and trying to remember, all of the's feels, I know I had them somewhere else, so I just went and got them right on. You see here I have a few errors because this is calling on the current entities which we changed the DB. Sami. Just fix that same time. So I mean, if you you may not have some other problems, I'm having what? I'm going through them just so that when you see similar problems, you know exactly what you need to do to fix them. Right? So here I see that I have my fields and I need to give them values. So TB customer name that text is going toe, have the value off. Well, this is Carter, and it's I don't to say carted it. So it's a scar to edit because I generated the property based on the function called the function based on the function costs are so its record to edit such is that to record to edit Andi, I also changes to require to edit and change the type. So, you know, this is a generic object type. So to make sure that it's his car entire record so strongly types along, which makes sure that all of your you're types are proper. So its record to edit dot on Then I'll see costumer name. All right on, then, just to see him for the day Trenta DT or in ted dot value is into the record to edit dots date parented on, then the same hero, but and I'm getting at air worried, seeing that he cannot convert, he cannot implicitly convert knowledgeable. So once you see that question mark alongside a data type, it means it's a knowledgeable, which means that I may or may not have a daytime or of value inside of it. So it saying that to the right, this is knowledgeable. But to the left, it's not knowable. The value is not notable. So they're saying they can't just say that, Okay, I have a notable data types I'm was going to see date time toe convert it. All right, so, no, we see that the date time is not know little. All right, so I converted it from a notable date. Time toe are actual date time. So once again, if you look, if you just over over data until you see the question mark. So the reason the question mark is there and it's notable is that this is coming from the database. Remember that our database design, or at least my database design, says that the date time I lost the nose. So if I load knows from the database design when link was converting it into C sharp equivalent data types, then it would have retained that alone Oh, property So that the data type would have had this question mark to say that the date time is our load to be no in C sharp, just like oh, it is allowed to be no in the database. In reality, however, that that knowledgeable date time data type is actually different from a regular daytime, just the same with string Andi int and almost every other data type. You have a notable version and you have a version that requires a value. All right, so I just do the same thing for the next line where I'll just go ahead and write the date time at the front on. Then I will call the object that date returned on. Then for the cost. I'll see object not cost. All right, I don't believe I need any off this, But then I see here where they're saying that they I have What is the problem? So this is a decimal. So of course I need toe do this to string. So the database come from the database? It's a decimal. I need to convert its string so that he can go inside off the text box. All right, so I still have a few errors. The fix going back to the top. We see where there is. No, there is no, um object on my form called lbl title. So I'm just going toe, make sure I go over to my design, So solution exper Add Eddie Trenton record on this will be the said title, right? So I'm this or said Leibler the sun was going to go to the properties on give it a name. So right, Nobody for this label one. I just rename it a bill title on. Once I do that, that should solve that error on there. It goes on that. See if there any other errors so you can always see if their errors, you know, court might is looking over here. You see that little red? Any line that as an aerial show your Red Square on that line has a visual cue on the next era. Here is that it's still looking for the old entities. Reference are the stream that Soto my new object name, which is on the score, e b. All right. So the next thing I would probably want to do is to enforce that edit mode. So I have any more, um, declared. I have it being set to True are falls based on which constructors being called. Oh, so our I'll do that later on. Then what I'll do is modify the quick event toe, get all the data on, then do the validations on. Then after checking if it is valid, we check if we're in edit mode or not. So me just do that quickly. So if if is it, it's moored Onda else. All right, so I put the else put all of this in the else because this is what happens if it's not in edit mode. It adds threat. So if it's not in any more than we're adding, I already have the other courts up, put it in the else, but then, if it is edit moored, they don't need to get the record by I d and then re add it. So then I'm sorry if I have seen back on for what? We're just going through this together from scratch anyway. So to get the idea, remember that what we did in the added vehicles that we created property or we created ah, control, which was hidden a label which was sitting toe, which we had assigned the I d off the record that came over in the constructor car. So we failed to do that this time. Let's just do that to make sure that we have all the bases covered. Once again, it's good that we already road. That's good, because when we do a side by side comparison, we can it can help to inform what we need to do. Right. So I'll go back to my design for the add edit, your entire record. I will bring over from the toolbox a label. All right. So common controls get a label, place it somewhere. It doesn't really matter. At this point, I can just change the properties to see that. Okay. I want this one to be recordar LBO record. I d Sorry. The sticks. Taiwan takes to be blank. So I don't want to time any text, but I do want it toe. Have the name lbl record I d on. Then I want its visibility to be falls so it will be hidden. All right, so we have that hidden label somewhere. It doesn't really matter where if you can see turnout, it doesn't really matter unless you need to bleed it. Then I guess you can paint over that era to help select it moving along. What we want to do is to make sure that we're also populating that lbl record i d dot text with the i d. Coming from record. So edit arts a record to edit that I d. All right, Onda off course after see it to string. Sorry. Good. So we have our i d somewhere on the form. Right? So when I move along on, I'm about to do editing know the first thing I do is retrieve that so far, these equality, um and then I can just implicitly convert this will no account. I can do it in that Paris Rather in that parse on. Then I'm parsing the record I d that text. All right, so whatever gets stored in that, um, in the text value for that label, embarrassing it on int and I'm retrieving that I d on then I can save IRA card is equal to gets it from the database db dot And I want car entire records. Not first our default. That and then queuing up my lambda expression you dot i dy property is equivalent to whatever I devalue. I just got on stored inside off my variable called I d. All right. Not that I have the record. I need to go about changing. Oh, it's the actual our values, so I can actually just Well, so what I already did was I scraped the values from the form. That's what we were doing from the one right. When we submit the form, we have local variables that get all of the's values from the form. All right, So what I'm going to do is not that I have the record. I'm just going to borrow this cord. Don't. Here, actually, can I just borrow all of this court here? Base it up here on, then what I will say is instead off seeing your entire record because their entire record is relative to the object. Are we created from the start? When we said our entire card is blank on, then start assigning all the values accordingly. This time I retrieved a record that existed on by variably record aren't well. I could actually just call the variable or entire record just to seem. Because within the scope, this rent a record exists within the if scope on, then this rent a record object exists inside off the else scope. All right, so then, based on whatever on I mean, this school could probably be refracted. I'm seeing how it can be reflected right now to be, you know, more, you know, much cleaner. But I'm not going to complicate it. I'm just going to leave it like that. So I am declaring on objects, whether I'm retrieving a record here on then using this record, I'm going to reset all off the values based on what we just stored in our local variables coming from the form. All right, on then, in this situation in the l situation, I'm creating a brand new record on then I am still populating this brand new record with what is coming from the form. Say, see, even populating with values coming from the form all of that that is repeated court. This court could have been done one time really hard. So any time you see repeated court like this, you can look for an opportunity to refractor it. So I'm going to leave you to do that, our factory it later and explain it to you. But then you can try it on your own and see how basic contrarians record that's your You know, you don't repeat all of these lines off court, but you could one time on, get away. All right, so moving on the next thing that we want to do having re populated all of the records with the potentially new values, even if they're the old buddies with a potential new vice from the database, is to see if the changes I can just once again copy that save changes on. Then once we're done, we want to close some after just going to write flows after the message box comes up. If you're adding, we disclose or rather I can just do one close, I don't have to close if you idiot and close if you delete. But if is valid on you do all of these things, then you just close at the end off that right? So I'm going to set a message box after saving changes after an edit operation. Andi, just duplicate this. So that's another duplication here again, right? So the problem with duplication is that if I have to make a change to something that I have to do it here on, have to remember to do it here on, then, in this case with the mystery box after do it here and then have to remember to do it here . So one way to refractory accord would be to just so you know, like column method that does something one time. All right. We have multiple ways to do it. A zay said you can find a way. I'll do it on my own and then explain it later on. But I want to China and you guys to start thinking about or factor in court and reducing repetition. All right, So before I moved on, our fractured the cord. So I hope you pause the video and tried it yourself, but either way I did it and I'll explain to you what I did. So I said after checking if it is valid, then I declared on object off car into a record. All right, this is what we would have done if it was create. But we need an object off that type. Either way, whether it is create more are nuts. So within the context, then it's OK to just do this step. Next I take If it is anymore, then I want to retrieve the I. D on d. I want to then retrieve the record from the database on, then said that as the value offering to record so if it is not if it is not any more than this is still just a blank object off current I record it has no value yet accept the fact that it is an initialized object if it is a bit more than we give it the value based on the record that we're treating from the database. Either way, whether it is any more or not, we want to be populating all off the properties with stuff from the form. So if if it is any more than we know. The form is goingto have values. Andi. If the person is just creating a record, the former still went off values. So we're just getting all off those values on placing inside off the object, whether or not it was retrieved or it is still empty. After doing all off that, I'm just saying, if it is not anymore, which means that I'm about to other record, then I want to do the Steptoe. Add the record to the later bees right to the database table on then. If it does skip over that, then well, either way, it's going to see the changes. After saving the changes, I call the message box, show the same message box on. Then I close the window and that's it. That's why refractor that whole section, so have you know one instance off everything. Instead of having two instances off the same operations, you know, you can look for things that have to happen regardless off the situation on how you can rare injure cord into fewer lines just to make it easier, so you can actually go ahead and pause at the different parts just to see um, add comments. Alright. So I've added some some comments and the fix of spending error here on so you can actually just go ahead on and pause the video, you know? Look at the the comments. Look at the court that's written rewrite a record that we are. You know, whatever it is you need to do toe get acquainted us what was done here. All right. Um so noticed that the statement here? I don't know if appointed, he told if you have one lying often operation, you don't need your calibrations. So here you have to have curly braces because I have two lines. But then here I just said, If it's not any more than there just indented winter next letter indented, he will only operate on that one line so nothing else will be seen as part of that statement. Now that I'm finished wiring up the added it your entire record. I need to go back to my management, manage rental records on that any quick event on then I will be able tow launch the window like I need to write. So I would say add and it are in tow record. That's any off. My object on that is equal toe a new incense off at edit your entire record and then the object were passing in his record, not car on. Then it's the same lines off cored, just changing on object references there on. Then we should be able tow properly, test the functionality off what we've just done. All right, so I clicked on view archive on. Then let's see what happens if I click a new record. So we see add newer, entire record comes up, we get our blank form. Let me just make sure, uh, that it works properly. So he's renting our Volkswagon Beetle for that many days. Submit. All right.
31. Enhancing Application Flow and User Experience: It's welcome back in this video. We're going to be doing some essential fixes, our application. So we've made good progress so far, but there are certain things are both the application that can be changed. I can be, you know, refined on. Just list a few off them. What we want to do is make sure that only one window at the time comes up, meaning when I in my applications say that I want to add a car. Let me just log in quickly so we can see exactly what I'm talking about. When I say I want toe launched a manage vehicle listing, I don't want to be able to click that and have so many instances off the same window open. So we're going to be modifying or applications so that only one window comes up at a time. That's one on another thing that we will want to do is to do our grid auto refresh. So we didn't get to do that. But we want to do that where if I had a new car, right? No, I don't have to come back and click Refresh, but the grade should automatically refresh and show me the new car. Outside of that, anything else would just be refining the messages and the visual cues that we give to our users. With each operation that they complete on down, we will upload our application to get hope. So let's get started. I'm going to exit the application, and the first thing that we're going to tackle is opening on window at the time. No, they're pretty much two ways to do this one. While one will help us maintain the MD I child pattern that we have been using, where when a window comes up as an M. D I child, they cannot be moved outside off the mean window on the other one is assured us that dialogue where that window will appear, nothing else underneath that new window will be active until that new window is exited. But then that's at the expense of it being an MD I child, where it will sit on top and I'll explain exactly what I mean. So let's start off with the manage vehicle this time, or rather, let us go to the main window and then I will look in one off these. I have this option here. This button click event where I said at rental record on I don't know No one for the manage vehicle listing fired. So I'm going to modify the ad rental record button, click event toe show a dialogue instead. Know the problem with show dialogue is that I can't sit the MD I appearance on, then show dialogue. You get an error, all right, so you can show an M d A child. So after sitting MD, apparently because the name their child and then I can show the window as a named Chad, however, I would have to or Mitt, or at least rearrange this line off court. So I'd have to put show above the MD I appear in line, and I would have to say Shore dialogue. So wanted shows that allowed pretty much that show, MJ, that MD I child setting him their appearance setting gets nullified. It's irrelevant at that point, but let's see what happens when I showed dialogue. So let me start. All right, so we modified the money either entire record on DSO. When I click, manage your entire records. That's a click event, right when I Oh, sorry, it's this given other entire record. When I select that notice that it is not an M d A child, this window launched well outside off the main window. However, I can't do anything outside of it while it is on the screen, and you can see blinking that each time it blinks. That's me clicking something inside of me and windows I can over and window at that interacted anything on. As a result, only this one no will remain active. Until I exited on, then I can proceed with anything else in application. So that's one very simple way off. Following are ensuring that only one instance off the window is open at any given time. Like I said, though, as you can see, the window operates independently off the main window, which may not necessarily be a bad thing, depending on your objectives. So I mean, in this situation, it's not a bad thing. I can move it our own on. I can't do anything else until I close. It's right, so when I close it, then it's business as usual. So that's not necessarily the worst thing that can happen, really. So that is how you can go about ensuring one instance off the window is up. So if you want, you could change all off these shores to show dialogue. Once again, though, it has to happen before the MD appearance line. Or else you get an error so you could actually just remove this line where you set the M. D. I. A. Parent. If you're going to be showing the dialogue, however, I'm going to show you another method using the manage vehicle listing, um menu click event. So this one. No, I'm actually it's going to be far more lines off cord Onda. Little more goes into the soft process, but the fact is that I will retain the fact that this window has an MD I appearance. All right, so for the other window, which is managing publishing Zarei, remove some court. I wrote the code charity, so I'm just walking through it. So what's going to happen is that we're going to be checking all off open forms in the application, so un button click right when manage week listing. When that button is clicked and we're about to show we're going to initialize, we're going to set them their parents But then I'm going toe. Also check to see if there is any open form inside the application as amount of a fact. It's probably, but I do all of that first. And then if there is not dinner, initialize a new object on, then show the windows. So let's do it that way. Right? So the first thing I want to do on then you'll see if you research that you see a number of ways off doing it Some people use for each loop. Some people use different methods, but the concept remains the same. I'm actually going to use the link because link allows us to iterate through any list or collection, just like if we're using a for loop or for each loop. So link is not necessarily unique. Toe only eight obey squares. But if you have a list that you want to interrogate, just as if it was ah, list off records going from the databases Sandy, stop objects. He can do the same thing using Ling anyway, All right, so the first thing I want to do is declare some very but I'm going to save our open forms right on. Then what I want the story here is the number off are all the open forms in the applications? I say application dot on Then I can use open forms, right? So this is giving me a little collection? No, off the forms owned by the applications. That application is what comes up or what? Stance to run on the quick start right on. Then all of the forms or the open forms would be any window that is open at the time off. It's running right, so I say application, not open forms. But then I wanted in a kind off lists off form. So while it's open forms, as you can see, it's in a different data types. So I'm going to say that and then cast, and I really casting it to the data type off. Ah, form. So I'm basically saying, give me a list off the open forms in the data type form, which is what I know. All right, remember that everything is off the base class form, so I want all of them at the beast class form. So it will hand that typecasting for me and then give me a list off the open forms off all off data type form. Now, after I get that list off the open forms, I'm going to declare another variable. Where I want to say is open Survive is open. All right, so this is really going to be a bullion writes. I'm saying vier, but it's really going to be a bullion on guy. I'm telling you this because off how I named it is open. So usually with a bowline. If you have noticed before, you can you don't think I know what I mean? Know that whenever I declare a bully and I say is this or is that right on? That's a common pattern in programming. So I'm going to say a virus is open on. Then I'm going to say, give me the list. Off forms, open forms. Andi, say that. No. When I say that, if you just looked through this list you see that all off? The options that we had in link are here and available. Er first our default. You have the weary of the selector of all off them. The one I'm interested in this time is any right? So any I listen to stipulate. Ah, Condi son. Is there anything that has this condition on? It will give you true or falls all right, so I can say any and then do on my land Expressions of Q arrow on then q dot So I'm checking if there is any form. So if I say q dot you notice that all of the's just like the options we see when we are creating a new instance off a farm writes, it's the same thing, someone to see. If there is any form on, then I can do this, too, is I can say by text or by names are just victor name. So if there's any form with a name, all right, that much is R is equal in tow. The name off, whatever amable to bring ups. Remember that the name off the form is pretty much the class name, right? So I can see Is there any form with the name manage vehicle listening cause I'm about to launch manage vehicle listing, So I want to know if there's any, um, form that is open that has the name off the former mobile to launch right on. Then, if it is not our if not is open. I remember that this really translates if his open is equivalent of falls. So if it is not open already, then I will do all off this. All right, so this one has a bit more lines off court. But the ones again, the point is that we're just checking to see if there is any open form, um, currently with the same name as the one I want to launch. And if there isn't, then I can go ahead a large. It I don't need an else cause is it's either true or false if it's falls and we launch it. If it's true, then well, we skip over everything and nothing happens, Right? So let's try that one. All right, so I'm about to launch manage vehicle listing. That's the quick event that we just modified so in and click manage vehicle listing, it will launch the window notice. It's the launching of them on MD I. Child. I didn't change anything about the way launched, however, If I click the button again and I'm clicking right no, I see the most is moving and I'm clicking. You only see one instance coming up that's because it is checking all the open farms and seeing that there is an open form with that name already, so nothing should happen. If I close it and then click it again, then it will work on. Then every subsequent click does nothing. So we see that that is one way that we can retain that MD I child behavior, but ensure that only one window comes up at the time so you can use either method. Um, like I said, the show dialogue doesn't allow for the India child, but it does give the form that is launching kind off the autonomy off. Not being a child. Um, but then, based on your application flow under your expectations, then you can use either one that you wish No moving on. We want toe start looking at our greed. Refresh. No. What we want to do is ensure that after we do let that add edit operation are at delete operation that our greed refreshes automatically. So what happens right now is that when you add or edit our record, it will, you know, say the success message. But then you still have to go back to your the manage um, list me Just launch application so we can see. All right. So, like I was saying, we have to when we launch manage vehicle listing. If I add a new car Alright, then I will have to come back here and then refresh manually for me to see the changes. So I want to modify that after at the car and I click submit and it has been saved the database that the grid refreshes on. I don't I will actually be able to delete and remove or remove. The need for this refresh button are so no, we'll have toe actually get creative. Just like aware to kind of get creative with the long inform. Where when we were launching our me and window, remember that we had to modify it toe put in on object off the log in form into our constructor so that we could manipulate the properties off the log inform when we wanted to , which in this case was to close it. So what I'm going to have to do is whatever window is launching or that manage windows. So money vehicle listing. I'm going to have to pass an object off this into the add edit window. All right, are even if it's the edit, which of a one? But once I'm called in that window, I need to pass on object off this in tow. That phone shown called into that construct a call right on then from this window, when it launches, I'll be ableto you know, execute a populate agreed form shown card. So I have ah, function called Populate Grid, which runs acquire runs the query. Sorry. Toe actually populate hungered on. Right now, it's private, so we'll just do the build out and it will see all of the changes. I'm going to do it with a manage vehicle listing on. Then I'm going to challenge you to get it done with the current I records window also. All right, so let's get started. So the first thing that I want to do is modify my constructors in add edit vehicle. So at edit vehicle had playing constructor Andi, the construct that I took on object off the car to it, You know, what I'm going to have to do is modify this so that I have one my privates property off type manage vehicle listing so I'm making this global because I don't know what I'm going to need it. So I have to make it global, just like I have to do with the log in and everything else that's up here on. Then I'm going to card and manage equal listing on. Then I'm going to tell this constructor. So this copy this quickly that it should take this construct that I No, no parameters. Initially, it should take a parameter off type magical listing on this one. I want to sit us, underscore, manage vehicle listing. But then the prop the parameter won't have the underscore. All right, so either constructor is supposed to take on object off managed vehicle listing. No, I can modify this parameter and say something like equals. No. Which would then say that you may or may not need to pass a value in. All right, So to make it not a requirement I can say equals no. So if you have to call this constructor, but you don't want to pass in that window, um are this object? Then this would kind of help to ease the frustration that would happen at that point. So I can do that for both of these parameters and two se equals no, just in case the person wants a called add or edit constructor without passing in the monitor vehicle. Sorry I said user, but it's really you. The programmer I'm catering toe at this point are so after we've gotten our parameters. We want to initialize our local O. R private object that we just declared Want to initialize it toe whatever value comes in to manage vehicle listing. So if it's no, it's no. If it's not know, then we get on object rent. So money Guica listing is the underscore. Magical listing is equal to that on. I just did this line and repeat, it's below All right on DSO. Whenever our window launches, it will have some knowledge off the manage vehicle listing window that called it so it's easy enough to declare an object off one inside off their own time. But then that each time we declare an object, it's a brand new object. It's not the object that exists, are so we want to make sure that we're tracking the one that exists are the one that called it because you know that the one that called it is the one that is opening about grown currently showing the grid. All right, so I'm going toe. No, go back. Sorry. Go back to manage recon listing on. Then I'm going to make sure that I just hold on control and click. And then I can just double click save more than one instances It will financially and DoubleClick which one you want to jump toe? So then my function cause toe add edit vehicle or a mind edit vehicle? Yes, I am notice that there's no Arab because I did set the parameter to be knowledgeable, right? So because I said it would be notable and let me just change it our own and just show you exactly what I mean. If I did not say that he can be equal to know, then manage vehicle listing. This constructor would then give me a red line because then it's saying that it doesn't have a construct, that it takes no arguments it needs. And it needs to pass in an argument because I said it can be equal to know with me find it back because I said it can be equal to know. Then I can proceed to me. The regular function called if I want O or the regular construct a call without passing a parameter. All right, so that is one of those. It's the things that I'm just showing you, just in case that you want to be more versatile. Either way, I'm going to pass in this. So these, sir, presents an object off the manage vehicle listing window that is making the call toe add edit vehicle learn. So I put this in for the ad click on guy. Also do the same for the editor. So they see the Eddie click is the same thing. It's posse in car, which is ah, required parameter. But it's not crying out about the money vehicle listing. However, I will say comma this All right? No, that that is done. What will happen is that when we add our edit the vehicle, once we complete our operation in this function call. All right. The expectation is that our great is going to refresh. So what I'm going to do is before showing the message box and seeing everything is completed, I'm going to put in ah call toe are objects. Underscore manage vehicle listing. And then I would call Populate grid. No notice that I can't call it. It's not coming up in the interest since and even if attacked, we thought, it's not calling it All right, So what happens is that it is private, so it's private over, inside off or manage vehicle listing. Let me see if I can find it quickly. It's private here, so I can't call it from outside. So if I make it public, then that problem gets solved so it's a public void populate grid on. Then we'll be able to call it. So let's review the expectation here when I click Save on whether it's an ad for operation or an edit operation. The expectation is that our you know, we do everything that we know we do. We check if it said it more than we populate our, um, object accordingly. So you see that we kind of repeated court and we kind of refracted at the last time. So I have a DB save changes here have a DBC of changes here on day. I really wouldn't want to repeat those things all the time, right, so What I'll do is remove these lines off cord. So I'm just refractory right now on. Then I'm going to take the same lines off court that were repeated and then put them outside off the check. If it is any more or not right, so we check if it is valid else. If it is a bit more than we want to do this. If it is not that, it more than one to do that. Either way, at the end off the day, we want to see if the change is popularly grid on. Then sure, I would just make a generic message Operation completed, refreshed to see changes on. Then we close. All right, So then what should happen is that when we click save and it does either operation, it's actually just going to populate the grid. Call a popular grid function in the manage in the manage vehicle listings window on. Remember that populate Grady's? What runs the query since the data source and all of the operations in between on then that should trigger the refresh off the grid. So let's step that up. All right. So we launched our manage vehicle listing window I'd like a new car on then I'm going to see the make is high on the Sorry. Hi, only I 20. This is 2019 model. Uh, sorry. The Vinnie's just putting something random. The years 2018 on the license plate is that we click save changes. It's his operation is completed refreshed to see changes. Okay, fine. But then look at that. It already refreshed for me so I can change that message. No, I don't need to tell them toe decree fresh second either refresh button there. But the point is that the operation would refresh at the grid automatically. Alright, The same for it. If I say I wanted to edit this and I wanted to clean up this vin a bit because I put in the wrong didn all right, and then save changes. Operation completed. Now you see the changed vin going in on everything is happening real time, right? So that is all you can do that, Gregory Fresh Operations. I did it with the manage vehicle listing. Well, I didn't do it with the lead car. Let me make sure I did it. With the lead car sold, I no longer have this car in my fleet effect delete. You see that nothing happened. So and click delete. Nothing happens. No visual. Cue off anything. But if I refresh you see that it disappears. All right. If I tried to delete again, I would have gotten an error. So we want to do then is make sure that our delete function, um, tells us that the operation was successful and it refreshes at the same time. You mean even want toe? Give them. Ah, yes or no? Are you sure you want to delete kind off prompt. So I'm back in the marriage vehicle listing. So initially I tried are re fresh using this function. But that kind of does something different from what we want to do. Someone to remove this on. Then what we're going to do is say one. If the person clicks the delete button, we get the details, you know, we get the object and everything on. Then what I'm going to do is stratum cord and I wrote it quickly, so I'll just explain it to you. So what we're doing is doing a message box that allows us toe from the user. Are you sure you want to delete. So when you click the delete button, it's going to retrieve The record is going to retrieve the idea and the record associating with the I d. Sorry on then. We're launching a dialog box. Remember that the dialogue boxes are box that comes up that prevents you from interacting with any other open window. Right? So this dialog box our dialogue result is the data type on the object is d are That's what we're calling it on we're seeing I want a dialogue results that has the value off a message box. So on then this the message we're showing. Are you sure you want to delete this record? He can put in even more details in this if you want because you already retrieved the car objects they can say. Are you sure you want to delete the car name, so to speak or the car model karmic If you want on, then this is the title off the dialog box, which is delete. Then we say we want to show message box buttons. Yes, no, are chancel. So when it comes up, it will come up with a Yes, I know and I cancel button on. Then we're showing on icon. That's a warning color warning that we're letting them know that they just entered the danger zone right on. Then, after we've prompted them, we're seeing if the result is yes. Meaning they took the yes button. Then we want to proceed to remove the car. All right, on then, having removed the car, we can call populate grid. All right. On a spelling error there so we can call populate. Agreed on, then that should, um you know, refresh organ. So let's try that bleat operation. All right, so we're back on, but I'm about to try to delete Toyota. I don't think Toyota has any rental records associated with it. I can always check second only. Just overview the archive on. I don't see anything with Toyota, so I shouldn't have any difficulty deleting Toyota. I click Delete on. Then it's giving me that ballot box. Are you sure you want to delete this record? Yes. No. Maybe so. And click. Yes. And then boom, It removes it. And then it refreshes the grid automatically for me for it that strikes were also if I click No. Then nothing happens. If I click chancel then nothing happens. But then once I click, yes, then it is removed heart. So we just cleaned up our application a bit. We made sure that only one window opens on. We also put in some good refresh capabilities into our windows. So I did it with manage vehicle listing. I'm challenging you to try and repeat those steps with the manage rental records. Either way, in the next video, we're going to be checking in our court into source control. I will have all the court ready and available on commented so you can check the repository for the completed chord in case in years.
32. Simple Login Form: in this video will be setting up a log in system for or current up. This will require to make a few changes, want to the database and the two to the overall flow off our application. So I start off with the database singers. I'll just bring up my management studio on what we need is a new table which will store the user information. So I distract Lee called New on Tables Go to new Click Table on, then what we want is an I d off course, which would be off type integer. It would be the primary key on. We will set it to be identity so it will auto increment on once again if you're not so familiar with database development, the new canticle, my course, my curse soft SQL server for everyone or my SQL database design and mastery. So after setting off our I D column, we then have the most common user related fields, which would be using him, which, as it has environ Char 50 and password, which I will set as Env Arch are, Onda said. This want to be 100 and I'll explain why I also not alot these two to be know as well. It doesn't make sense. You have a user with neither use anymore password or either or they both must be present for user to be complete. Right? So I just have those three columns for No, Save this and I'll just call this one. Users click. OK, and then we have our new table. Now, some schools of thought would say that you know, you want to use that truck. Exactly which users doing what in your application. So if that is your desire than what you'd want to do is add foreign keys to your other tables, being types off cars on the car, entire record so that you can actually see who is interacting with these, um, tables at what are these records at whatever points? I won't go that fire, though I just want a simple log in system so that we can try and restrict access to our application. So after adding this to the database, I have to go back to my court and make a few modifications so they just come out off the boat moored on. Then the first modification I'm going to make is toe our E t m X. So whenever you make a change to the database, you want your e DMX Aurier into the diagram to be aware off those changes. So we added a new table second US right click Water Update model from database. Then when this comes up, you see the ad tub on under the Aftab. You noticed that I have on arrow beside tables and show me that I have a new table that is in the database, but not in the diagram so I can just click users click finish on, then it will go ahead on the water process. It needs to do with the end result being the addition off the user table toe my toe, my CD. A mix diagram, right? So no, my application knows and can access the users table or user properties, right? If I had set up relationships like I said, we set up the foreign keys between the tables and user tip. Then you would see lions going all around the place because these store related and then user would related toe types off car as well as current our record. But that's that wasn't done, so here diagram should look like this off course you built on. As you can see, my computer still loading cause it's building on. We give it a moment on. No, that's done. We can go ahead and design our log. Informs I'm just going to go back to Solution Explorer, right? Click or projects goto Add on. Then I want a new form on this one is going to be log in. So I'm just going to call it to log in and then I click add give it some time to process all of that. All right, So I went ahead and designed it, and I had used the same techniques have been using up until this point, right? I used a label for the label at the top to depict that it's a log inform. I used a table layout, and on the left, I put the labels for using him and passer. So the right I put the text boxes on Then after me, sure that my text boxes have the cards names, so I have TB user name on die have TB password, right? So he can this pause the video and then off course, have the log in button at the end off the day so you can pause the video here and do your design, you know, similar to mine if you want. If you have a different reason, that's fine. But that then off the day we've been learning this, so I'm not going to go over the same things. Are these basic things over and over and over again? Because I hope you've caught up and you're familiar with it. If you're not, then you can review the previous videos and get up to speed with some off these controls and hold a work. The next step, however, would be to make sure that log in Button works, because once the person provides user name and the password, the next step is for them to actually log in our click log in so off course on Click. We want to do something. Now that's something that we're going to want to do involves us interacting with the database, Onda looking and retrieving the user name and password to make sure that whatever was entered much is what is in the database. So by way of testing, firstly, I'm goingto go back to the database on I'm going to add our record, so I'm just refresh my tables, go to buy users tables, say at its top 200 rules on. Then I'm going to put in a manual user name admin on the passer. Here's what to be the word password, all lower case on, then that's our first user in our database, so add mean postures. So that means when I get to the log, inform if I provide credentials. Use the name admin and the password is the word password. Then when I click log and it should be able to look in the data base, verify that the user name I provided much is one of the database, the password. Likewise on. Then say yes or no. I can go through All right, soul back in our cord. Like I said, we need to interact with the data base. So that means that we need something for that. So we can the store private read only, and then our object off car Trento entities on. I'll call the object on the score D B on. Then I will initialize this on the score. D B is equal to a new instance, off car rental. And it is There we go now in the click event. I'm going to wrap it in a track it from? No. So I'll just say try. Sorry. Let me try that again. Try double top CIA, double top tub on it. Generates that snippet right on. Then I'll just write out my exception. So I'll give a generic, um era. So it's never a good idea to actually show the exception message to the user. Like I've said before, even though I have done it before, I'm just showing that you can. But generally speaking, you probably don't want to, because that means nothing to use their. So you just want to say something went wrong? Piece draggin. All right, so then that's what you want to do on, then, for to try. What we're going to try is to retrieve the values from our form so I can save our user. Name is equal toe TB user name dot text on. Then the same way of our password is equal to TB password text. So we've retrieved the user name, retrieve the posture that user has entered. No. The thing with the user name is that you may want to trim it a true means that you're taking off white spaces on either side so I can actually see something like string dot trim . Oh, sorry. No, it wouldn't be. String that trim string does not have that trim function. You would actually be that text. Not true, right? So when we say dark trim, whatever string value it is our string variable that we see that trim, then what it will do is take off any leading white space or training with white space from the using him. Because if I wrote my using a merit, if I wrote space or inadvertently person space bar while writing or before writing or after writing the user name, then it would not be a direct much. So you just want to trim it to make sure you don't have any trailing whitespace or leading white space so that the text is more absolute during the comparison. You don't want to do that with a passer, though, because you want to make sure that the person puts in the exact text that is in the database that they put in the the actual text box. All right. So after retrieving the user name and password, the next thing I want to do is actually compere. This are trying to find something in the database that much. Is this combination right? Someone to save our user, which is my object that I'm going to use to retrieve from my database on my users table. Any record. So I want one record. All right, so I need bad, terrible user because I'm expecting one record. There shouldn't be too users with the same user name and password combination, even if they have the same password additions have the same using him at any given point. So I'm expecting only one user to come back really on. I'm going to say first or default, and then I'm going to throw up a lambda expression on that. I want to say I want the first our default user who has a user name that much is the user name I just got from my text box. Andi. So I I can use the and under or just like if I was doing an if statement Andi, the password in the database directly matches the password in the text box. All right. So once I do that, I am no going to check if I got back. I user are not. So I say, if user is equivalents toe no then. And if the user is equal internal, that means after its searched the database for the first our default user Well, default means no. So for the first user, that has the same using him that was entered into the textbooks on the CME passer that was intended into the textbooks if that user construct has no All right, So if after that operation this is no, then we want to say something like, please provide valid credentials. All right, so as far as we're concerned, nobody in the database much is that No, You don't want to tell them exactly what's wrong. As in you don't want to say yes. I phoned that using a but your password might be wrong or vice versa. Because if it was somebody who was Trento maliciously get into your system, you would have solved half off the equation for them, letting them know why they couldn't get into your application. All right, So you want to say if it is No. If no user exists like that. Then you know. Please provide valid credentials. We can leave the else blank for. No. Um, of course, that's what would happen if it is. If everything is OK, then that's what what happened. So I'm just going to say flows are so just for experiments sick just for no. We're going to say if it is not current than the screen should just give us a message box. If it is OK, then the screen. It'll just close, all right? No. If we want this window to come up when we press start, then we need to modify our program that CS file. So right now it's going to launch the main window first. What I want for the floor from my application is that when you double click the application to run it, it's actually going to launch the log in screen. First on, then, if he launches a log in screen, then you either get in or you don't get you either get further, which means you get to the man we know, which is rmd, I apparent, which we have designed toeholds every other window and functionality of our application or you're stuck at the log in screen. All right, so I wanted to say launch the log in window first. So when the application runs launched the log in. So when I click start, then we see our long and window come up. So if I type in admin one on by tapping the word password and click log in, then nothing comes up. Art. So you see that piece provide valid, valid credentials and I'll show you exactly how I got these square. So you're probably typing inside of it and you're seeing the word posture. It come up on your seeing squares come up in mind, which just represents the Generi Carter showing that you know it's obscured its mast. So I'm going to show you exactly how I am asked mine. I didn't send a text box, and I did. I skipped the step of showing you So that's fine. I'll go back. But the point is that when I type in invalid credentials, it won't let me go further. If I type in admin and password on a click, log in, then it closes. So we see that our application or are logging Logic is working because it went to the database and it checked. And it's our admin as using him password as a password on dso It did whatever it was supposed to do in the else statement. All right, so let me just go back to the design quickly and show you what I did to the text box for the text box. Toe mosque, Your dear text. Then what you need to do is provide the password Char. So we're going to pass were char and providing a notices is char. So that means give it a symbol if you put slashes and slashes will appear if you put an asterisk than the Astor School appear. The reason I'm getting squares, however, is that I changed the funt. So by changing the funds Toe Maar let I was just experimenting a change minute to Marlet. So what happens is that there is apparently no asterisk in that fund, right? So then it just give those generic squares to show that well, it's masking. It would love to use the asterisk, but I don't have an asterisk in this fund, so I'm just want even the generic square. So you know the symbol aside. The point is that you can mosque your text by using that password char, because of course, you don't want the word password or the person sponsor to be in plain text while being entered into the database is no to fix the floor. All we did was say Next are, say, closer in the else. What I want to do is actually launch the main windows. I'm going to say Vier mean window is equal to a new instance off mean window aan den. I'm going to see well, of course, my semi colon and I say me and window dot Sure, all right on, then I want this window to hide, So the thing is that an images show you exactly what's going to happen. So when I launch my application on, then I go ahead and provide valid credentials. So add mean and us word and click log in. Then it's going to bring up the main window, but logging is going to be there, lingering in the background, which I don't really want. It's not that big of a deal, but what happens that if I wanted to close the application out after closer me and window on. Then come back and close logging because they're two entirely different windows. I didn't say that logging should be an India child off the mean window. All right, so they're two separate windows and they're both apart off the application. So as long as they're both running, the application would not close. So what I would want to do is hide on the reason I don't hide. I'm not close. Is that if I say close here, the log in window might actually close before the main window shores. Alright, so I've actually seen that happen before, so I'm going to just say hide. So that means when I provide valid credentials Alright on, then I go ahead and click log in. Then it will check the database, verify me, launched a man window, and then hide the log in window. So that looks a bit cleaner. It's a log in a norm in my application. But then when I close this mean window notice, I'm still in debug mode. So debug more represents that application is still running all right, which I don't want. So that means the lugging window is still it's hidden on. I can't even get to it because it's really hidden. It's not even showing up in my task bar if you're if you're experiencing this right, no, it won't be showing up in yours either. So I actually have to click stop. And that's still not desirable, because then that means a processed red would still be running until that persons computer is shut down. So we don't want that someone to just modify this scored a bit more so that we can see our application flow a bit better. So what I want to do is that when I launch my main window, I wanted to launch with the notion are with the knowledge off the log in window being somewhere. So you realize that these forms are kind off autonomous. So any launch one or you see not show the only time one cares about the other is if it's an M d. I appearance off that one. All the Children don't care about each other. They only know about their MD I appearance. In this situation, the main window is its own em, their parents, with its own Children on the log inform, is a sibling, meaning it's on the same level as the mean window. It's not a child on it is a mean window in and off itself, even though it's not an M D appearance. So the point is that you can't if you have toe let one. No, off the other deliberately, right. But the cool thing is that once you let one know about the other meaning, once I let me and Window know that there is a log in window somewhere, then I can actually manipulate the log in window through the main window. Um, at any point. So what I'm going to do is pass is in this declaration off on object. I'm going to be clear that are passed in an instance off this sermon, but that this keyword means that whatever class I'm operating in, this represents an object off that class, something I'm passing an object off the logging class or the log in form into me and window . Now you're seeing that this red line appears because there's no constructor for me and window that takes an argument off Le Guin, someone to modify my me and window inside this hold on control on Click Me and window and it jumps over to that form. So if you didn't know that or that quarter other So if you didn't know that shark or no, you do on, then what I want to do is let here no lets me know in the north that there is, ah property or give it a property off type logging and I'm going to call it underscore Log in. All right, But then I'm also going to create another constructor. Soc! Tour me. Use up my shortcuts here on then, this main window, this other constructor other is going to take on object called Le Guin Onda. So what this one is going to do after initializing components, remember, you always have to initialize component, but then you're it's also going to initialize my local and private log in property that I just created. It's going to initialize it to the value that is coming over in the constructor being initialized here. All right, so once again, what this allows me to do know is that throw the rest of the court wants this constructor is used. Then I have access to the property, underscore logging on. Then I can do what I want to the logging. All right, so all I'm going to do know it. Say that me and window. So I just bring up my designed for me and window, and then I'm going to tell them, and we know that I wanted to have a closing event rights. However, we didn't put a load Events. No, that's fine. But then I want to say that when you are closing on, I'm just going to look very carefully for the closing event. Right? A screw too far down. There it is. Form closings. You have form closed on your form. Closing right. Someone to say when you are closing, I want you to do something. So I went to double click in that space and get the form closing event on. Then all I'm going to say is that when you are closing, I want you to tell the log in object or the log inform that, you know, called you initially and passed over itself into your score so that you know about it. Tell it to close. All right. So what's going to happen is that when the lugging window is going to launch me and window . It's going to pass over an object off itself into the mean window when the main window launches main Window will have launched with knowledge off this log in window, and it would be doing whatever he needs to do with all the Children are operating, whatever it's doing it's doing. But then that Then off the day when it's closing, it will say, Well, when I'm closing, I want you to close so you log in window, you close while I'm closing on. Then everybody's happy, so let's test and see if that works. So when I click start on, then I log in. All right, so we have mean window and we know that we can do whatever we want to do in me and window. But then when we click, close me and window, then expectations that he closes the log in screen. Also on visual studio exiting de boat moored is proof that our cord worked are so when that that's one way of setting up a log informed. I'm not saying that is the way I've done this in quite a few ways before, but that is certainly one way that you could use and I think this was the simplest one to show to. Somebody is just getting used to win forms. But, you know, some people actually launch it as the MD I child on. Then you know, you get into the application, but after log into proto proceed to get access to the other Windows and other many options . You have a number off ways to implement this kind off flow between your forms. No coming back to our log informed. There's another major modification that we have to meet our application to make it actually secure, because the fact is that if somebody gets access to this database and they look and they see admin on the word password, it's all in play antics. You never want to store user names. What? Sorry, You never want to store passwords in plain text. You always want to do what you call an encryption. So we need to encrypt our, um, you know our password to make sure that if somebody gain access to the database by whatever means even they were walking over your shoulder in the office when you were doing this work that they wouldn't be able to decipher watch that password is. So in order to do encryption, we need to get the help of some third party libraries. Their number off encryption algorithms out there, you have shot 256 You have shall one. You have MD five. You could go on research them. Um, but I'm going to help you get some tools, get some help into this project on. Then we can do some encryption together. So what we want to do is go to the solution. Explorer, right? Click or project on. Then we'll be using new get Monnet Package Manager are so this will follow us toe bro's on open source kind off marketplace to see what we can get to help us along the way. So I'll goto, bro. Sis bro's installed and updates. You saw that you had the entity framework install. Which is what allows us to do link and interact with our database. What? What I'm really interested in is an curb shun so I can look for encryption. All right. Encryption didn't really show me anything. Let's try security. So sometimes you'll come here and you don't necessarily know exactly what you're looking for. Because there so many libraries to choose from. But what I'm really looking for is something regarding security on what I would want. So you see that you have quite a few ones here you have, sister. I thought I just cite Microsoft that nets that security know our itself on the one that I'm interested in, which is system that security dot cryptography, not algorithm. So if you look to the right, you see a nice description about all the tests offer. I'm sorry. Feet appear small on my screen, but hopefully your viewpoint on your screen and it is more clear. But they you see that they have a SRS, atia each each mark MD five on a bunch of others are so you can actually just click on this one on, then the awful off course you want to make sure that you're using their ah safe publisher so it doesn't get much safer than Microsoft to be in. The publishers say you see by Microsoft to the right off the actual title. And if you see Microsoft and you can trust it, you see other big names that you can trust. But then the thing is that certain people sit down and meet these libraries on DNA, not discrediting them or anything. But not everyone has. Your best interests are tired. Some off, the more malicious. As much as you get me, try to weed out the malicious ones. They do tell you that they are not responsible for anything that may happen to you or your product. What if you don't load anything from them? Right. So you want to use this one so you can click this Click this little black arrow to the right, or you can just click install on. Then you can give it some time, know where it's going to go ahead to gather all. Dependence is it may use more than one libraries on day. It's going to don't little called D L L's, which are like packages that have a bunch off library. So, you know, every time we write a class, I would say using this are using that Those are actual dealers. Are libraries full of phone shots, right? So it's just downloading a bunch off libraries. They can just say I accept and allow it to don't Lord as many as it me Neto. So we give it some time. And then when it's done, if you go over to your solution Explorer on drop down references, then you're going to see a bunch off new references. You probably didn't do this before, so you can the state note of the fact I have system that security system that security dot cryptography dot algorithms any of a bunch off others. You'll also see the entity framework, ones that got added when we added our e DMX diagram on so on. So that's what you get that brings to the table Tello's you to just install a package widow going and fetching these from all over the Internet and trying to inject them into your project manually. Alright, so know that I've added these algorithms. It's timeto actually put one to use. All right, so I'm going to use the Shah 256 I think that's a good, nice secure one on. Then I'm going to walk you through exactly what needs to be done in order to get it to encrypt. So firstly, you always want to encrypt your password. One weight, which means that your encrypting your password for a storage you're never decrypting your posture for comparison. So that means if I am creating a user right, if I have a form, you know, like when you sign up for anything on the Internet and put in a using him on you would have type your password in plain text, cause you type it the way you know to spell it when you click Submit. What happens is that that system will take that password and encrypted and save it. Now, after that, each time you try to log in, it will take whatever you put in that password field on, then encrypted on, then compared this encryption with the stored encryption, it will never take the stored encryption and try to decrypt it too much it against your plain text. All right, I hold that made sense. So you always encrypt one way. So what we need to do here is encrypt the password that is being provided on. Then compare it against well, the pastor that is being stored. So there are two things we need to fix one day encryption for the comparison and to actual password being stored. We need to modify that also. So, firstly, I'm going to declare a variable or an object rather on. This is going to be off type Shah 256 Now shot 256 on the red line is going to come up because then it doesn't recognize that and I'm just going to call it Shot is equal toe sha 256 dot creates Redlands Galore Let's see what the red lines have to see. So if I hover over the red line or the line with a red line the world with the red line, it will tell Give me the light bulb. But I will tell me that I need to include the use off this library. See, I need that statement. So if I click that follow the guidelines, then everything is blue. I know it looks more acceptable. So I need to include that library toe access the encryption algorithm amable to use. So having made reference to the shot to 56 on, then gotten the user name and password from our text box, the next thing I want to do is compute Ah hush. So I'm going to converts the input string toe abaya Atari on compute the hush. So I'm getting the value password. I'm passing it into the shot dot compute hush, which doesnt in cording over it on. Then it converts it into a bite, Ari. So in C sharp, that's what unique urinary on. Then. That's the data type on the variable is called data. All right. Having done that, we need to construct a string. So first, there need to declare a string builder object, right, so that I can collect the bites and create a string with it on. Then, after I do that string builder, I am going to use a for loop aan den construct that string using the different data points from the bite. Hurry. All right, so let me go through this again. I'm converting everything, converting the string into a bite, Ari, Right? Called data on. Then I'm just declaring a string builder object so that I can further use it inside off a four loop where I'm saying, give me I equals zero. Eyes should run to the length off the data bite area. Whatever lens that came upto on, increment accordingly on. Then I'm seeing a string builder just append each subscript in my bite, Ari, while converting it to string all right. After doing all of that, I'm then going to assigned the value toe a password or toe a variable called hashed password Heart. So string builder, Doctor String. I'm stirring it as hashed pastors and end off this operation. Whatever it is, you changed you type Sorry in, as the password would have been completely changed by the end off this operation into something that you may not recognize, I do say me, and I recognize I really should say should not recognise. So I'm just want to set a break point right here on the line where I tried to fetch the user on going to show you exactly what the word gets transformed into. All right, so I typed in admin password on were at the break point. So first, let me show you the text that I typed in would have bean password right on, then the text. That is no hushed looks like this. No, this is not the word password. Well, it really is the word password. And that's how it should look in our database such that if somebody comes along and sees in the database, there is no way they should be able to tell me that this is the word password, all right? And if I continue with the court where I'm no one to look into the database, well, this will continue because it's still looking for Just stop the execution on. Then what? I really should be comparing what's in the database with is the value that is in hushed password and not password. No, if I run this again, right, and I'm just removing the break point. So if I run this again and try to log in and I'm trying to compare the posturing in the database, which is skewed out pastured with the hushed password, then it will be a miss much because our databases storing the word password on hash password does not look anything like the word password. So then that tells me that the hashed password is what should really be in the database. So me just click start again and drive that point home real quickly. All right, so attempt in admin and password, and I click log in on. Then what you will do is try and compare the hushed password with password, and you see these provide valid credentials are. Okay, so if I just put ah, break point here again on then I attempt this log in and I'm doing that just so I can get a hold off the value that is in hushed passer. So I want all off this volume, excluding the quotation marks off course. I'm going to copy this and then I'm just going to cheat a little. I want to go into my database and I'm going to put that hash password as the value. All right, so that's one of the reasons I said the password lends to be 100 varta are 100 not Varta are 50. Because that's what a pass or it is supposed to look like in your database. Nobody should be able to look in the data base NC that the admin spots. Word is the word password. Let them C five e 888 Yeah, blah, blah Andi. If they've taken the time to memorize all of this, they need a life really and truly alright on decrypting It won't be the easiest thing in the world either. So let's go straight that again. So know that I've changed it in the database right before the comparison was done. When it does the comparison on, likely continue. Then it should proceed. All right, so let's try that one more time just to make sure that everything is OK. So I'm going toe once again. Start on. Then this is going to launch my log in screen, which is going to prompt me toe enter my user name and password. So my user name is the word I'd mean on my password is the word password. See, that's what they're typed. Click log in it, then hushes that pastored compares it against what's in the database realizes that the two words hash to be the same on. Then it proceeds. So if I make a spelling error inside off my password, then the hash is going to be different, and it's not valid, all right, so off course, when creating users, if you wake, want to extend this function that you know and put in some user management capabilities in this application. When you're creating the user, if you are the one sitting the passer, just make sure that you're going to follow these steps. This algorithm where you declare the algorithm you're using one. Andi, try and use one algorithm. Throat application. You don't use MD five here and show over there because the hashes will look different. And you, me run into problems, right? So always use one hashing algorithm on. Then you create your bite, Ari. And then you rebuild that string based on the different parts that went into the bite area on. Then you use the hushed password. After that, for every other operation on, that is a quick way to add some log in functionality to your application.
33. Control Features Based on User Roles: Hey, guys, in this video, we're going to start looking at building functionality that will help us to manage our users now up until no, we would have hard quoted one user. We called it admin, and we hard coded the password. But in reality, we're not going to expect that every time we have a user or when we have multiple users for or application that the user has been tough to bring up the database and hard coded on, find the hashing version off the password. Like what we did on manually put those in. That's not the reality. What we want to do is create an interface where the user can click on. Preferably, the admin user, of course, can click money users get a similar management window that we've done for vehicle listing and for inter records one that list sort off the users in the database who would want to display their pastors off course on gallows them to maybe the activator user versus adding another user on maybe resetting a password or something like that. So those are the functionalities that we would love to put in based on what is on the screen However, I already modified my main window toe. Have the button manage users. Andi, I already created the new form Money Juicer. So you can pause the video right here on go ahead on at this new menu option on the new form and write the code that would launch the window. You can pause a video. Try it yourself. What I'm still going to go through and show you exactly what I did. So in the main window design, I just added a new option. Manage users. I didn't put anything underneath it because I want this option to just come up with our when clicked, rather come up with the new window to manage the user. So I added that new menu item and that I double clicked it to give us the the click event on. Then, in the trick event, I did the usual thing. You could even just look at previous code that we've written. I didn't put in the check to see if the window is open already, so I can put in that check just to make sure that it won't launch twice. All right, so I'm checking if it is open and I'm checking. If manage users is open, then if it is not open Sorry, Then you can launch so that we will get one instance off. Manage users. I already went through and I did the new form. And remember to add a form you go to solution expiry Right click you say add on. Then you select form on. Then you go ahead on at this new form to your project. I called it Manage users. If you want to change them, that's fine as long as you know what name you used and that the name is relevant. I'm just going to make some cosmetic changes because you can see that the window title is did well. I didn't change it. Some with the change, the text to manage space user. So it's more readable, more user friendly in its appearance aan den. On this form, we're going to have pretty much the same thing that we had another form. So I just go back to manage, maybe manage vehicle listing that design our entire records, whichever one because we want to have the same kind of nails. We have that grade. I can actually remove this refresh button, but just for no, we'll have the label, the refresh button, the grade on all of the buttons. I'm just going to copy all of these controls on, then over to my new window. Manage users on peace them on, then expand the window as much as I need toe, facilitate it. These new controls. You just do that quickly. All right, I rearrange them and then let me change the text here from management. Our records toe manage users. And then because I'm reusing the scored, of course the names, what is presented on each button won the text andan to the names need to be changed. So this one needs to say, Add new user instead, off record right on. Then I'm going to change the button name from beating at record to beat in. Add user someone to go through and do that for each off the buttons. So let me just do them quickly while on screen. So that's edit record on, then that's going to be beaten at its user or, you know what? For attitudes there, I'm actually just going to say reset password. So we're changing this for insanity from anything the user to reset because, um, for the purpose of this function or this application rather well, you just want the admin user to be able to reset. Ah, users password. That's the only anything quote unquote, that should be ableto happen right on. Then I'm going to change the lead toe, the active it user. So instead of delete user, we're going to do the active it on actually exactly how we can facilitate the activation versus deletion. So Bt in the activate user, all right on, they know that we change those old I can just do my trick. Events are just double click each button to get those quick events and the clothes that we know for. No, I don't need it. The click reset password on the active it user. All right, so now that we've done those cosmetic things to the form, I also want to make some changes to the database table. So I brought up my management studio School management studio, logged into my instance, went into my database, which is currently on. Then right clicked my table that said on and chose design. Sorry. So now I'm in the design off the user stable and I'm going to do Ah, few modification. Well, one modification really on. That is to certain active flag. So what I'm going to be doing here is I'll be seeing is active, all right? And then, based on the naming convention I've been using upon to this point, anytime you see is something that's me saying I want a bullion in SQL bullion will be beat . So the data time for bullion here is bit. And then So this is going to say, Is the person active yes or no? So if it's true, then that person's activists, if it's far as a person isn't active on, I will not alone. Oh, here either. But then there might be I'm going to get an error if I try to save while not allowing knows all alone. Oh, and save And then I'll go back and make a modification. The reason for the era would be that I already have a user that does not have any data for this. So if I chose to not alone oh, then it would have been a problem, because then I already have a record that has no So then it would be a data type or constraint mismatch. Right. So moving on, I have the user on. I'm going to add another table somewhere directly. Tables go to new go to table on then. This one I'm going to call rules someone to have I d on. Then this is going to be our primary key, which is identity. So it auto increments on. Then I'm going tohave name a name is just going to be via tar 50. So name here represents the name off the rules. So when we talk about rules and receive the table and I'll see rules so we talk about rules , it means that what role does this user plea in this application? Is this person Adminis? This person may be a data entry clerk is this person may be just a supervisor, you know? And then, based on the rule that they play, we can determine what functionality they have access toe or what actions they can complete . All right, so I have users I have rules on. Then I have to have another table that links the tools someone to create another table on. Then I'm going to give this on an I D on, then this is going to be the auto incriminating I d. On this is really going to be what we call a linker table because we're going to do a many to many meaning many users may may have many rules someone to have a user, i d. And then this is going to be int. And then I went to have a role i d, which is also in All right, So this means that any user right now have admin and that admin users an i d off one on. Then let's send it off to rules. I'm goingto have admitted I'm goingto have users. So you have an ad mineral amp a regular user role are being taken of 31 for supervisors, whatever the case may be. But the point is that when um I user, with whatever i d gets assigned, are at record, goes into this stay brother with ah article user idea in a particular role, I d. That is association between this particular user on that particular role. All right, so I'm going to save this stable, called its user rules. Next, I'm going to have to create a relationship between the user role stable and the other two tables. So because I have user I d. That's a foreign key to the user stables after go right click. Sorry, did that kind of quota limited again? After right click in the design view, then go to relationships and then, well, it would have been blank. So I click, Add on, then I'm going to choose the tables and columns specifications. Click this ellipsis bottom over to the right inside on. Then I'm going to say my primary key table is the user stable on. I want the I d. Call him from this primary table on. Then I want to link it to the foreign key called User I D. So that creates that constraint there on. Then I'll do the same thing. Just click add, create another one using the ellipses on. Then this one will before the roles stable with the I D is connected to the role I D column in the user or stable. Then click OK click floors and then I can save and then it will take a while. Then it will make sure that I know that I'm about to modify these three tables I can click . Yes, all right. And once those changes are saved and we can go both putting in some money while changes. So the fact is, that application is yours. You can always create your own management screens. For what? About data. It is like we've been doing so far. We've done it for the managing off the car, entire records. We've done it for the management of the cars. We can do it for the users. It's up to you to know how granular you want to get to that. Because then some people allow you to create your own roles in the back and well in the application itself on. Then they allow you to assign users to the different rules for yourself. Right? So I will show you how to do the assignment. But then, using the same principles if you want, you can allow them to create the rules in the application itself. But for this course, I'm going to just do it manually. So I would love for you having done it in the application. That's my challenge to you that you share your quarters. May I mean, I would love to see how it is that you accomplished it on. We can, you know, learn together. But for no, I'll just do it manually. So all right, click on roles. Goto Edit Top 200. Now I know that I only want to rose in my application. You might be building their application for, ah, bigger context on mine. But then, within this context, I only want an administrator. Or so I can have an administrator user or many administrator users which you want to off course, you know, be careful with that on. I'm goingto have ah, data entry. Clark. All right. As a matter of a fact, I'm going to extend this table a bit and I'm going to put in another column, so I only have name on the I. D. Let me go back to the design and I'm going to put in short name. Alright, Someone to put in short name on this one will also be environ. Char 50 can be var char 50 very, very similar data types, but but it's invite our of our chided up very similarly so it's OK if you had either one. All right, so four rules. Let me get back to this. So let me just close this design view, go back to roles at it. Top 200 on, then for the short names I'm going to put in like a shark cut name. So literally have the name administrate. Or but then the short name could be add mean, Um, the shirt name for data entry clerk. Could be just Clark on. Then Maybe you just have you only so this person wouldn't have any access to actually change data to actually click, add or delete or edit or anything like that. So view only will only be able to click on see the data that is integrated, right? So I just save you. All right, so we have the role names, and then we have the short names and you'll see the relevance off the short names in a few . So those are my rules. All right, Onda, I'm going to do one more manual addition here, So I'm doing these manual additions because I already have the data, and I want to show you how it will work before we start building. You told right So the user i d here would be the user i d for the existing user, which let me just confirm what that is. So if I select upto sorry, toe top 1000 rows then I see my one admin user and the i d here is one I see is activism Nall All right, so the user i d here is one on then the role i d for administrator just still topped 1000 again would be one. So that means in user rules user i d one has role i d one on I just changed lying toe Commit that on. So we know that our user all our admin is in the user or administrator with a short name admin Now that we've made those modifications 01 more modification. My body. I skipped that part. I need to let the administrator user know that he he is active. Right? So I'm just going to Right click users go to any top 200 rose on then change is active toe one right. So one or zero in database. If you say one, it's true. If you say zero, it's falls, right? So it will always the truth. You can write the word Suri can just assigned the value one on the beach will automatically know what is through our falls. All right, So the user admin user is active, so let's minimize part. And then in our project, we need toe update our data context diagrams. So I need to go over to my E DMX diagram, and then we see all off the three tables that we left here with, but we need to make some modifications. So I didn't make any modifications of these two. I'm not going to delete those. What? Once I made a modification toe Well, the one I made a modification toe would be user. So I'm just removing users that has clicked it and and press delete on my keyboard is confirming. I say, Yes, I do want to delete it on. Then I'm going to right click on, then update model from database, which is then going to prompt me. Do you want to add new tables, understand to click tables and added a few tables just know and then click Finish on, give it some time on once that operation is done, then you'll see your new tables appearing. So I created three. Well, two new tables and meat and a I just meant to to use the table. And you see the relationships that exists so both user and rolled I related to the user role table. I know that I have the ability to determine what role the user is in at the time off lugging. I need to be able to see which ruled this person isn't so at the time when I confirmed the person. So we know that we have our logging form that handles that logic on as soon as I find it. Here's my log in form, right? So we know that on the button click event where we said lugging we did that hushing computer the harsh and then went ahead and said, Get us the user. Is there the user on? Then the user would have said yes are no right. So when they said yes, right then we would have called a 1,000,000 window passing in an instance, off the Italian window, etcetera. No, we need to extend this functionality because no, I need to know what role this user is in. All right, So I already have the user object right? Already know the user object. But remember that I had said that once you get the user object or once you get an object from the database, you can actually access the related data directly from this object, so I can actually declare a variable here and save our role. All right, role is equal to. And then I can say user, because I just got user here, So And if it's in the else that means the user is not know. So that means that user presumably will have our role. I recording the user role table. So if I say user dot then you're going to notice that I have access toe Oh, my buds, I I broke my own rule. Every time you make on on update to your diagram, you should build this. I've skipped that step so you can see the side effects of this. I'm glad this happened. So when I just type that chord, let me just undo and go right back. So when you fail toe, build our field to update the diagram and your editor at court, you're going to see that the court may not reflect the changes. So remember that we had added at least one you column toe over user Table said. I mean, their user object should have access to their new column, at least so we're still seeing the same three columns that we had before we changed the database before we updated our diagram all because you didn't build. So what I'm going to do is us comment off this line on then do control shift and be on below The project to rebuild all rights or build is completed. And let me just uncommitted this line and then control and shift after the full stop just to see on. There we go. So we're seeing here I d. Addie's active sorry is active password user name, and they were seeing user rules. So that is our relationship, right? So remember, I said that once something is related to our ah foreign key for tables in another table, then we can automatically access the list. All right, so for car intel, the rental record is associated with one car, but a car is associated with many records, so if you have a car objects, then you can see all of the records that are associated with it. So in this case, we have the user, and we can see off the rules that this user might have. So it could be a situation where you have multiple rules. You have multiple users. One user can have many rules. That is the situation that we facilitated in our database design. A simpler way to do that could have been to just add role. Um, I roll a name to the user, but then that's kind of limiting, because then if you modify the rule or you want a new role, then you're going to have to go through every single user with that rule and make a modification right? So if we have that linker table, then it's easier to manage the different rules, the different users on which rule that user might have. But within the context, we only have one Roper users. Even though we're facilitating that I use, it can have multiple rules in our context. In this application were only facilitating one rule. Her user. All right, so I can say user doctor, and then I'm getting user rules. But I only want the one rule that this person has second a safe docked on, Then use link to get the first or default rule that is associate ID with this user. So first or default No, I don't need a Lambda. Expressions appear I use alumni expression because at a condition upon which I want to the first or I wanted the first record that met this condition institution. I don't have any condition. I just want the 1st 1 So I'm saying, get me the user on the list. Quote uncle, list off Rose and get me the 1st 1 because we know that we only have one rule. Anyway, when we get the 1st 1 then we're getting the one role. All right, so that is a nice quickly to just get the one role that this user has. And this is embodied in the object called role. All right? No. When we look at role, it is going to be off type user rules. So then I can use roll on, say dot and I can get the details on the role Are the details on the user that that this record is associated with second see, roll that roll and then I can get the name or the shark name and the reason I created short names that it was much easier to check the shark name than the names. The name is the nice official title. Improving readability about the shark name is what we the programmers will be using in the background. All right, so know that I have gotten the role that this user who is about to log in or who has just loved being now that I know the role that they have Alright, what I'll be doing is passing the well. Let me say that I only want the shark name so I don't want the whole rule object. I want the shortening, so I'm just going to save our Roland name is equal toe role dot on. Then get the table rule on, then see it? Not again. And then get then sharp name. All right on then. I'm passing this short name in tow, so start name. Oh, sorry. I'm passing Rolling in my body. Not sure Name some passing, so I'm all right. Let me you know just because I just confused myself suddenly just be clear. Role short name right is equal to roll our role object which is users rule dots rule, which is our table dot The short name column on then this variable is going to be passed into the main window. Um, constructors And I need to modify this construct of some toilet on control and then click it jumps over to that constructor. We know it's this one that we need to modify some saying that you can also accept a string called roll Short Name. All right, so when we log in on, we're launching our mean when they were passing in the logging object, our also passing in the rule Schardt name. So because I'll be using this role all over the place and when to do it similar to how I did the log in. But this time I want to make it public, someone to say public on a string on. Then I'm going to give it underscore role name. All right, so my main window is going to have a publicly accessible property called role name, which will allow any window that needs to know what rule is the currently loving person in right. So I can just get that on, then I'm going to initialize it, so I'm going to call this row Underscore. Old name is goingto have the value off the rule. Short name. All right, so and then I mean, this principle can be used anywhere. So it could be that you're going to be passing and maybe user data because you wanted a welcome screen or, you know, you want some little task buyer the bottom to say who is loved in. You know, you could have passed that information just to see him way so I could have actually just possible the entire user object into the main window and then use that data. How I needto there so many ways you could have done this, right? But right now, I'm just interested in knowing the roll off the person. All right, on, then we can move along with that. So the person's role? Um, no. Let's do a simple operation. So let me just show you exactly why we need to roll a name and what we can restrict. What if anybody who is not admin should not be able to click manage users and you can just think about it? We have three users. We have Where? Three year old. Sorry, we have admin. We have our administrator with data entry clerk and we have you. Only nobody who is not in the administrator role should be ableto come and click Manage users. It should either not be visible or visible and inactive. Right? So the best way to make sure that when the men window lords we know one world this person is in on then two we determine what they can and cannot do would be to say, unload. We want to me these decisions, right? So I'm going to just create ah form lording event on the mean windows side escort the properties go over and here it is, Lord, Some just going to double click in there. I'll have the men win the Lord event in the main window. Lord event. I'm going to say if roll short name is equivalent. And this is why I said we can use the shark name Andi. Sorry. Actually have underscore role name. So Rochard name is unique to just the constructor. But then the rest of the application was underscore. Ruling name right. Someone to say if on the squirrel name has the r is not equal toe, admit let me say, not equal to admin. So it is not equal toe the short name admin. All right, so obviously this wouldn't be so keen if somebody went and updated the shark name that would break it all together. Right? But let's just work with it for no. So if the rule name is not equal to add mean, then we want to make a decision. The decision want to make is that the manage users button? Let me just find its car its name. So at the school with the properties for that button on, I see that the name for this button is manage users to strip many items. I can copy that. Just copy the name. So if the rule is not equal to add mean, then I want to say this, controlled by its name dot enable is equal toe falls. All right, so you have properties that way to disable its Remember, we have that visible also. So if you wanted to hide it, you could say that visible is false Or it could say that enables faras meaning they will see the button. But it's not. Anybody can click it. I can't interacted it, but let me use a visible cause. I think that's cleaner instead of sure and then something that doesn't work. Let's just hide it all together so that they would never guess that they could have or that that functionality will existed. Right? So I'm saying that if the rule is not equal to add mean, then this is false. All right, so let me click. Start on this. Test that out. All right, so we see that we still see our manage users and expect card. So when the user we have is the admin user, So when it got to this statement when it was loading, you would have said Is the role admin? If it is, or if it is not admin. Sorry, then doing show it. So I'm going to creates on mother User. That is not an admin user. So this one I'm just going to say user so and then I'm going toe use the same password on then this is going to give manner so that we could copy of the passport first, then see user password on is active is true. All right, on, then. I'm going to try that again. So let me try and log in with this new user. All right? So I'm logging in with user password. Click again on. Then it said something went wrong. So let us see exactly what our error Islamic over to my lug in court. Let's see if it's something So it's something on the click event. Let me put that very point here. Let's try that again. And then it gets over to the hushing off the past word That's fine at me. Step through using F 11 and then it runs the query where it's seeing where if the user name is equal to user on the pass or it is equal to the hashed password. Good, Let me step through again. Then it says, if the user is a corpulent in law else OK, so it moved along. Now we're getting the rules. I suspect that my era might be here. So rule came because no. All right, that's what's wrong. So I did. The user and I did not set the recording set off the user rules, right, and you can see even if you hover over it. In debug mode, that account is zero. So that was a problem. So let me just click. Continue, and it will. So exception handling is why didn't break right? You just said something went wrong. Please try again on then I'm going to go back to my database on di need to add the new user . So my new user is i d three. Let me go toe user rules table at it topped 200. And I want to say that user with the 83 has a role idea off to because, you know, that role I D one is admin. So we wanted to be anything but admin. So I just added that the database and I'm with the trainer and the application again, frankly clogging on going to remove the break point on press F I for it to continue, that's something still went wrong. So there we still have an arrow that's we need to fix. So let's try that again. So I'm moving the break point. Don't today if statement check. All right. So let us try the logging operation again. So it's good that we're going through this experience together because I think I may have Mr Step, but I don't want to just tell you the answer. Let's see together. So ah f 11 and then we get the role. Also, the role is still no. So it's still tell me that the user rules. So let me just make sure it's getting the right user. So when I over over the user object, I see that the I. D is three on everything else is good, but then it's still seeing zero for the user rules. All right, so let me stop. Come out of the boat moored on. Let's just recalibrate on dsi on, make sure everything is okay. So I have user roles, I added, And the user i d you three. So just make sure that it was the right user. So, user I d three on, then. Sorry of some tops. Open. All right. So, user, I d three on two. All right, So let me just do some debugging on, let you know what's up. All right? So I already it will stop it and started and try it again and try and debug on. And there was literally no era. So I'll explain what happened then. So user password, we clicked again. We're going to hit the brake points. I didn't move the break point, but as you can see, a user roses, no counting toe one if I hope for over it. It is no seeing one. I literally made no changes. I think what happened is that when it during a run time, it did not go back to the database every single time. It kind of like took a staged, like a snapshot off the data at the time off the quarry or at the time off the bogging. And it was using that set off data, saving one with me and the changes. It didn't reflect in the application at a runtime. So what we've done so far should work. If it doesn't, we can always let me know. But I'm just going toe remove this break point and I'm just going to continue because everything should work. I So we envisioned it. And then when I pressed F five to continue notice that the menu is not showing manage users . So that's because the role that the person who has loved in the rule associate ID with them is not admitting. So The role is Clark, right? If I had left the debug the very point. And you said that a rule short name is still seeing Clark aren't so Then that is whole. So what we did was we got the rule for the person lugging in. We transfer that rule data over to the mean window on then in the main window. What? During Lord, we Well, we set up our lord time event and we said, When you Lord, check if it is in ad mineral. So whatever it is, you don't want the known admen persons to be able to do if you had 10 many options and they should only be able to access to you can follow the same pattern and just make it not visible. Just you know what they deactivated to make it not visible. The point is that they can't do it.
34. User Password and Active Status Reset: Hey, guys, In this video, we're actually going to start building old or manage users where we'll be retrieving all of the active users. One allowing the user toe add, or at least an admin user to be ableto add the activate or reset the password off another user. Andi. So the first thing that we want to look at, however our coaches the time made between the previous listen and this one so one. I modified my log in logic. The quarry that is checking for our user added, the constraint that the two dot is active must be true. So firstly, we were looking for in the database. Get all the users with the argued the first user from the user stable that has are marching user name password in the database. That much is our hushed version off whatever was entered on. Then I added 1/3 constraint where I said on the active flood most bankable into True. So you would remember that we added on is active Flug Now, we said to bullion in the previous video on DSO. That must be true. So the same logic applies Author of these most much on If it doesn't. Then we say please provide valid credentials, right? You can be more specific than that. I mean, you can probably get into the details as to why I was, But like I said, the more generi the better for malicious attempts on your system. Um and then I modified our chord in the l say can take a look at this court where instead off doing all the processing to find the role name like we did initially on, then passing just the rule name over. I'm actually just passing the entire user object over to the main windows. So I said, environment windows, equal toe man window on a passing, this comma user. So when the user is retrieved, I want to pass that entire object over to the mean window. No, this will give me more flexibility because and I'm just going to bring over me and window. No, I am. I can use this user object to access any data about the user that I wanted, including the rule so initially would have used it to access the role information and then passed only the road information this time just passing all off the user information on, then honor, you know, case by case basis, weekend access the user information if we want. So I introduced our new property inside off the main window and I said Public to user on the score user, this one is underscore or name on. You can see some of them are replicate someone of my luckiest. I need to be more consistent. So I'm a changed role name, toe Common case are everything looks a bit more consistent on then inside off the constructor. I'm initializing I have my log in object. I have my user object on. I'm still just going toe set the role name separately because I don't want to repeat this bit off court every time I want to rule name right now This bit off court that I just highlighted, I condensed it even further. So the last time what we did was we got the user roles and we got the first our default. And then we had another variable where we got the role, that shark name from the first, our default in this case. I did it all out once, so I'm just going your whole There's so many ways to write to your quarters in link that as long as it works, its good on link is kind of designed to be as efficient as it can. So even here, write it this way are another way. It's almost going to be the same runtime. So, you know, efficiency aside there so many ways you can write your queries. So in this situation, I'm seeing that role. Name is equal to buy user object, which still has the list off user rules like we did determined on. Then I'm saying, Give me the first our default one. No, Once you do our first our default, it is going to bring back only one instance often object. One result. Once I have one result I can say dot on, Then call the associated table. So this would be like my inner join on, Then call the property. I want solid teams get me the first our default user roles record for the user. Then look at the role and get me the details off that rule. Same bit off court that we wrote, except we split in two lines last time. I'm doing it in one line here, so you know if one were one. We work, so you better you can read the other one better. That's fine. You can know is this. Copy it over and reproduce it. That's no problem. As honest, it's works. It's good. Everything else remains the same. And like I said in this video, we're going to focus on our manage users screen so already kind of put in some grown work on this form. We have manage users who have the refresh of the grid on. I don't think I renamed the grid, so let me just go over and rename it quickly. This should be GV user list. Alright GV user list and then we have our different buttons. Want to add a new user? Want to reset a users password on one to deactivate the user? So let me just go over to the court file for manage users on die need to initialize something that will allow us to connect to the database. I know we should all be familiar with the father. We need our private object or private property. Sorry, off type current or entities on the score db on. Then we initialize that inside off our constructor, and then we have or three object three functions. Sorry. So I'm going to deal with the easiest ones. Are the easy ones first and for me, the easy ones of the reset posturing and the deactivates? Because I don't have to do much design work as opposed to go in on computer, toe the beaten and add user click because then, after designed a whole form, these are simple queries. So the simple query would be that when the person selects a user from the list, then they would be able to creak, reset password on. Then I'm just going to generate generic or have a generic password that is going to get Hush replace their existing password in the database on. Then that is what they're expected to type in when it is that they're logging in. All right, so I'm going to put in the reset pastor functionality on like I did last time. That's going to borrow some cord from one off the existing manage screens, right? So in the manage vehicle listing, what I had done was I said that for beaten at its car. Whatever it is that was selected, the selected rule that is the one I want to retrieve and then doing operations. I'm just going toe literally copy all of this because it's there already. We wrote it. It's ours so we can reuse it at will. So I'm going to say Beauty and reset Pastor. Click I want to retrieve from user list Devi user lists, Right? So I haven't done anything to popular GV user. Let's just yet, but GV use or let's get a selected raw on the cell should be I d. And let me just go back to my database and make sure that it is capital I D. Which it is not. It is common. I i d. So there again I'm being very inconsistent and that's not good. But, you know, it's good that we see these things on common mystics. They want to be consistent in their cases in a database and any record. All right, so we're getting the value associated with the selected rule on. Then I'm looking inside off the users table, not they type of car stable. So user stable, que dot I dy being couple into the idea that was selected on Ben. I'm not about to launch any screen or new windows. I can remove all of this on. Then what I will say is that var password ARV are new on a score. Password is equal to on Ben. I can put in a generic one, so you can probably ought to generate something based on the details. I know some people they would do is auto generate. A pastor based on the user details what user details would usually be like. First name, last name, date of birth and such. I didn't collect those details. I'm not storing those details about my users. If you are, though, you can use that to your advantage. Right. But in for my simple example, all have is user name and password. So I'm just going to put in something that says what? I'm just going to put in a generic password so your new password would be passed Word at sign. 123 All right. So I could communicate with the user that you reset the password. Their new pastor is that on? Then you could build in from Senator that wants to put in that generic password. They have to change their password. All right, so their new password is password. 123 Then we want toe hash it. So we're using the shot to 56 algorithm. If you look back in the logging that CS file you see shot 256 and you see all off the court that we need toe hash. So I'm actually just going to copy MPs a lot off this. And then you see that the snow once it's repeating itself and is repeating itself across multiple forms, we could probably create one function that is called that does this job. So I'm actually was going to do that right now. So inside off my solution. Explorer, I have a file called you tills Right on then in this form, Well, in this class, what I had done was I created a utility for The form is open. So I'm going to do another one where I'm going to see public static. Ana, this is going to return. That's a string on. Then it's going to return. Hush. It's going to be called hush password. All right on then, is going to accept a string off. Well, ah parameter off type string on. We're calling it password aren't so inside of this function. No, we're going to start doing all of that stuff with the shot to 56. So I put the court in there on then after include my references. So including the reference. And then I'm going to just take all off the cord that we used in the log in. So all of this, where we're doing the string builder and building it up. All right, so I'm going to just caught all off this cause I don't want it in logging anymore. So this is part of refractor when you realize that court is repeating itself on, then you have to modify one place, have 10 other places in just one place and reuse it. Right. So we have our shot to 56 object. We do our bites, we rebuild our string on. Then the last thing that we did was to say, s builder dot to string. So what I'll do is state this line on, then I see returns are function is going to return the string buildup to string. So this is the final volley that would have been assigned toe the hushed password. So, after getting a password value through the parameter we go through all off that, and then we return the rebuild string. So that means anybody who wants this, um, this, you know, hashed password will have to make a function call where they say, the hushed passers. So this to modify this logging function as a hash password is equal toe you tills dot hash password, and then this is expecting some string on. I'm going to pass in the password that we want hushed. All right, so that looks much clean a c just one line off court to get the hashed password. All right, on, then. Just the same way we did that in the log in. We just did that in a log in who had no business doing that in the logging. I'm just doing it one time because I'm about to do it again. Enemy to save you tools. I'm about to do it again inside off manage users. So my new password is going to be equal to, and I just caught this. So instead, off, let me just say vier generic password is equality that string, right? So I like to give my my values variable names. I don't like toe have random values anywhere because And if somebody stumbled upon Pastor and that sign 123 that would be wondering what in the world that is, Right. So I just put it in a variable and then I'm going to say new password or could just say hushed password right here is going to be equal toe you tills not and then hush password. And then I went to pass in my generic password as a password. I would like to get hushed. There you go. So we don't have to repeat all those lines off court every time we want the hash. A pastor, we just wrote a function inside off a static class that's just going to take a value and then give us back. Ah, hushed poss word. All right, so that is our That is how we hush. So you see, if wanted to introduce what we'll call assault, then we could have just introduced it inside of the u tills. Because then everything is happening here anyway, so you could just say add assaults to the password and then do the hashing on. Then so would no one solid. He wanted to change. The salt will change it. One place. Not that changing. I saw it is recommended anyway. But that's the point of having all of your cord a centralized as possible. All right, so know that we've calculated. So we got the user, right? Presumably on. Let me just change us up this object name. So its user, far user. All right, so we retrieve the user on, then we got the password we hush the password on. Then I'm just going to see user dot password. Right. So remember that we retrieved the user from the database right on. Then we, you know, said legendary password. We hushed the generic password. So I'm seeing that this user from the database, your new pastoral value is going to be equal toe whatever came back from or function on God stored in our variable hashed password. Then I can just say underscore db that save changes because then it's sorry trucking that why I just got this user back from the database. I changed a property in this user from the database. So to save the changes right on, that's our But in recent and then at the end off that you probably want to give a message box to see that Riess Passer. It has been reset, so you could even be a bit more explicitly could let them know that. And I just use some interpretation here user dot user name. All right, so I'm going to see which user name on an apostrophe s recipes. Um, you know, punctuation on grammatically correct. So, user, that user names password has bean reset. All right, and that's it. So we just did a password reset a nice, simple quick under two password reset off course. Like I said, for the password, I use that generic password. But then, in practical terms, you probably want to generate this under fly on, then let the person or the patterns. So that's part of security. You would never want to tell the person that your new pastor is sponsored at 1 to 3 calls and anybody can go in that person's ConEd Once that pastor, it has been said if this is the posture, it has been used for every single person, so security wise, you want to tell them that it's there. The 1st 4 letters of the first name, the last two letters of the last name and the date off birth, something that only they should be able to sit down and figure out easily on the probably be a bit more difficult for somebody else to figure out. All right, so that's a nice, quick and dirty way off doing the reset password function. No, a similar thing would be for the deactivate user, so I'm just going to take this strike catch, all right? And we're just going to modify the quarter little. So once again, we retrieve the person who was selected, Then we retrieved them from the database, but then we don't need any hushing off anything, All right? What I am going to change, however, is there is active state someone to say a user not is active is equal toe falls. All right, as a mother off, this is deactivate. So I should probably build in putting functionality to say that you know, if that same button is clicked, then it would be activate or deactivate, so I would have to change the queuing or the representation off the text on this button. So what I'll do is see change of text to see the activites slash activate user, right? So then the person would be clicking. This button would know that this is a button that will deactivate the person or reactivate them. So if they're active, deactivate and vice versa. Then the core that we're right is that on button click and we retrieve the user. All right, then we're going to see if something like that I'm just going to use a Turner operator here . Southie User dot is active is equal to, And then the Turner operator No would allow me toe state a condition someone to say, user, that is active. Onda. I have toe specify the condition is equivalent to true now if his active was not notable. So if you hover over to see a bull with a question mark, that's because we made were load nose in the database. And so it is knowable in C sharp. If it was not knowable and it was just a straight bully and I could just say is active and then say is true or false here at what happens if it's true what happens of its Farr's well , because it's notable. We introduced our third states where it could be. No, it could be true. Falls are no right. So because of that, after specify is equivalent to true. So we're seeing if the user is active. If we do, you will not abuse in the database, and we find that there they are active. If is activist true, then we want to set it toe Falls otherwise said it's true, right? So it would just be like any statement if is activist. True, then is active. Should equal toe falls else is activated. Equality Troop right? That's that's basically what we did in this one line, All right? So I could add a comment here to say this is what it would look like us and if else All right, So that's what it would look like if the user is if the user is active, not sequel in the truth and we set it off, falls else who set it to true. So that's what this one line does. So when the person are the user clicks the activity, it user, it gets the road I was selected on. Then he gets the user associated with the idea off the road. I was selected on Then we see the user is active is relative to what I was store falls. Then we save the changes on. Then we see user user name our has bean Our activity status as change Active status has changed. So I'm just I'm just leaving a generic here because, um, the thing is I would have to probably put in an if statement to say what they destroy falls at the end off that if it was activated or deactivated, so I'll just leave it. Try and keep it simple on just say, you know, the active status has changed. All right, so right there, we would have done to very major operations Onda We would have done it with, you know, much simplicity, quick and dirty at some points. But I did explain why on how you could enhance it for yourself on DSO That's it for this lesson. Next time we come along, we will be populating our grid on, then focusing on adding users toe our application
35. Add New Users and Password Reset: Hey, guys, welcome back in this video. We're going to get the party started officially on. Actually look at how we can go about adding users on Dwell, managing their experience with our applications. So they're adding users, selecting the rule that they're in on generating a default password. And then we'll have to modify over long in process such that when persons log in using that default password, we we redirect them toe a screen that allows them to change this password. Now, before I move on, I'm going to show you a little adjustment I made to my me and window, where I added a little bar at the bottom here, and I have the Texas is logged in as colon on the user name. So I'm going to show you how to do that. And it's really simple. But these simple things, you know, make your application come to life. So what I did in my design form, I mean, window was toe add. What I call what they cause are, er, context strip. So I went toe status trip, brother. So I went toe the tool box, and then I got inside of menus on the tool bars, one that say's status strip. So I just drugged that over dropped it. Don't bottom part here. And then I added status label someone to click status label and show you what that looks like. So I got to strip status label all right. But obviously you that is what will get printed to the user. That's not what we want printed to the user. So what I did was modify the properties one by changing the name. So I changed the name to something I could identify changes, ts I log in text. That's what I called it. But I also removed the text because I don't want that text to be showing on. Then clearly whatever takes is going to be displayed is going to be context to, All right, so after a renaming it and I'm going to delete this one that I just created because I don't need the tool. All right, so I guess it's going to be difficult for me to remove it because he got condensed, but that's fine in the window. Lord functioned. Oh, I did say that I want of user name to be whatever we got back in our user objects that user name writes. Remember, we have worked user objects that we're sitting here on initializing inside off for constructor. Once person logs in, we're passing about that entire objects will have access to everything about this user on. Then what I'm choosing to display in that text is and I call it ts I log in text. All right, so I called it ts i to strip item. That's just me. That's my prefix that I'm using. But I'm seeing tears. I log in text, that text is going to be equal toe. And then I said, logged in as call on on that I'm passing in that user name that I got from my user object. Right. So when the man when the lords it will know who is logged in on that it was really that simple. All right. So moving on, what we want to do is activate our ad logging. Um, but right. So we have the manage user screen where they have our database context. Objects were already did our password reset on deactivate user functions, so, no, we need some functionality inside off ad user. So, in order to add user need another form which I did not create. So we'll do that together on then. When you right click my solution goto odd take form on. Then I'm going to add the form and I'm going to call it add user. So I'm not allowing you to edit the user. It's just abusers. I military just want abuser as a form trick ad. And then we make our usual cosmetic changes where we change the text in the address bar to add user on. Then I'm just going to widen it a bit. I'm also going to copy, um, some stuff from one of our existing add edit screens of outdated vehicles on the reason copying this is all right. So I'm going to use at her entire record, and then I are. The reason I'm using other entire record is I just noticed that at Edit Vehicle did not have that. You know that label at the top. We also didn't do the cosmetic changes, so I may have, you know, overlooked that part with it. It vehicle. But we can me those adjustments later, so I really just want the label. So I'm taking this label on I'm moving toe add user, putting it at the top, changing the text in there. So that's odd, User. All right. And then we can, of course, re center it. All right, then. I'm going to use my layout. So I have been using the table yield for these forms, so I'm going to use the container on It's going to be the table layout panel. All right, on then. What does the user have a user for? User would probably want to sit only the user name. Right. So you don't want to a low Your users are your admin user to be setting the password. Remember, we're already hard court imaginary password. So Well, leave that Jerry passer that when they click submit, that's all that gets created. So I'm actually just going to resize this since it's not going to be that big off a window . So I'd user move our table panel or a table container a little bit. We bring over over labels. So under common controls, I bring over my label and then this label property. I'm goingto set the text to be user name. All right, on, then. I'm going to bring over a text box. So then the text box for the user name? Sorry. Um, something that forget word things are text box. So we can at the user name. And then the next thing that would be sitting for this user is the role. No, the remember that the rules are all stored in a table called rules, which means that I would probably need a drop down list. So let me just copy MPs this using him label on. Then I'm going to change this one to see a rule. But then for the role, I'll be taken this from a predetermined list in a combo box. Similar struggle with this combo box. And then we did a combo box before. So what I'm going to do is just me this stable Liotta beat bigger so that we can straighten . Um, you know, maximize under space a bit more. We can expand or controls a bit more on. Then for the comma box, we have to modify the properties where one wanted to actually be a drop down list. So I don't want anybody to be able to type in there. Also, the functions are the items. Sorry. Would not be hard core did it. So we'll have to set that from the court behind when this form lords. All right, so we need a lord functions already. Those double the form. I've got the Lord function. All right, so, in orderto load in the items, we did something like this already when you were adding the car into our records. If you want, we can go back to the added it our entire record on Then we can look at the Lord function that we did for this one where we said we want to acquire the table for all off the cars in the table on. Then we want toe populate our combo box. So we need to do something very similar. Where? Let me just get back to my design. I'm going to see that first of my textbooks for the use any is going to be TB user names. Let me go to properties. Onda, change that TB user name on. Then the combo box is going to be CB roles at CB Rules or role The star something letter. But once you have the prefix and the appropriate title afterwards are so then what we want to do is populate this combo box list with the rules and the appropriate value and data Members. So I need to initialize my database object. All right, so we have our database object. The next thing that I want to do is to actually put the function call or that quarry in here somewhere to save. Virals is equal toe database objects. That table, which is rules dot on then. Well, I just want to list. All right. So I just want the entirely stuff rules phoned in the database, which we know is Really, um the i d the admin it that started i d The name on the short name. Those are the columns are in. So then I'm going to say CB dot Sorry, CB roles. All right. That's data source is equal to or oh, are variable rules that we just declared on. Then CB rules, But Value Member is equal to and then we'll see that it is I d on their we say CB roles. Thought ISP, remember? Sorry. Display member is equal to, and then we're displaying the name, not the short name, but the name this time. All right, so set the data source. We said the value member and we set the display member on after all off that we want this add user window to be launched on the click off the Eid user button. So let me just go back to my manage user. So this is a button click event. So off course, you know, we need to declare on object at user is equal to new and then went to see add user as may initializing Sean. Then I'm going to see add user So the objects MD I parent is equal to on. Then it's going to be this that in the eye appearance remember that We just want to be clear them they appear and to be that on, then I'm going to see odd user dot show. But then remember, Of course, we want to make sure that only one instance of these shows. So we need to put all of this inside off that if statements I need to check if a number is going to say you tells that for me is open and then I pass in the name of the form that I want to check if it's open, which is add user. So if not meaning, if the farm is not open then I want to do all off that Sony click add user. Only one instance should ever appear because off this if statement No, we're not finished with this. The next thing that we want to make sure we have is the button. So we have. Ah, I would submit button on. I'm also going to put the cancel button right, So you could submit that. Okay, Yes. This is the user you want to create. And you could also canceled operation. Right? So we just do those two buttons. So but no one I'm going to go to properties on, I'm going to call it what I'm going to say submit firstly on. Then it's going to have the name BT and submit. And then for a button tool, I'm going to make similar changes. Where button toe. The name is going to be Bt and cancel on that The Tex is going to see cancel All right. And then for the cancel button, Ahlam went to see his clothes. So if it gets clicked, I decided to create electric inventor. I'm saying If you click, Cancel. Just close the window. That's easy enough. Right on. Then, if you click submit, then we want to do something else. So when you click submit is when we actually want to add the user that you're entering to the database, Right. So then I need toe declare our new object off type users. I'm going to save Ire. User is equal to new user. All right, as then, I need to collect data from the form. So let me do that first. Actually, someone to save our user name is equal toe and then I know TB user name dot text. So that is, use the name that was just entered, right and then went to save our role. I d is equal to the selected value off this comma box or CB roles that selected on that's index. That's item. But what I want is selected value, right, But then off course, this is going to be off type object. I need it as an in Sunday's going toe implicitly typecast it to an integer. So no role idee is into jury selected value. What does those usar object have? So we have the user name. We have the role, but we don't have. Are the password and the active status? No. If I just created the user, I'm going to assume that the activity is true, right? I don't know why would create an inactive user someone to leave that as true by default. If they want to change it later on, they can go ahead and do that from the management screen. But I have the role I d on d. I have the user name and then the next thing would be the password. So if you recall, we have a default password that we use when we recently did the reset password functionality and I can review that and show you in the manage user, we did a reset password button where we set that to the default posture. No, if I want to 13 this as a default password, then that means that I have it hard quoted here and of it heart about the hard coded in this next function, which is not good because then if I need to change it, I'm I change it one place and not remember to change that the other place so It's easier if we have it. One police. Right. So I'm actually just going to create another function in the U tills where I'm going to see default hash password. So I'm just going to take all of this, right? Andi, I'm just going to copy and paste it on. I'm going to call the function default. Hushed password. So this is just me. Name it. You can name it something else. But this one is not going to take any parameters, because what I'm about to do is hush the default password and return that harsh. Right? So I'm going to return. Were replaced. Sorry. The reference being me to the terrible password room where we had the perimeter here. So the parameters here we have password. I'm replacing this cause I just to call the permit it off. This one on I'm replacing it with ah, hard coded password at 123 So that means if we want to change that default pass really need only change it here on every time we call this function, it will just hash that default password that we have set. Right? So what I'm going to do having created this function is one Go back to manage user on. Then I'm going to change this entire block off Corno to just use my new function on that His default hushed password, which takes no parameters so it is going to do is return. Ah, hushed version off. What about that static text that we sit is so there are many ways you can make it more dynamic. I'm just trying to keep its in before the purpose off learning. So have utilize thought default hushed pastor, that will just hush. Whatever that text is that we did programmers or the business has determined is unacceptable. Default password aren't on. So I had used it inside off money juicer, someone to use it again. Inside off user When I'm adding the user, I want the password to me You tills dot default hashed password. So they're getting a default posture. It also all right? No, I have the minimum requirements for a user. Have the use the name of the password on our boats and activity status role ideas not required in the user's table. So we soon get to that part. But I'm going to say user not on then actually, I'm going to show you how you can do it with the With the initial Isar, someone's going to save our new user. And then I'm just going to open and close the braces on then, inside off these open and close braces. Just a little indentation here. We're going to see the user name that should be. Here is equality user name. All right. And then, comma, the password that this user is going toe have is equal to the password variable comma on. Then the final one is is active is true. All right, so when we're about to some meat, a new user, we call it the using him that was entered. Collect the password, declare a new object off type user and then we feel in the columns, put a similar call and thereby accident. So we have used the name is equal to use an impostor. Dizzy court of password is active is true. We can go about adding someone to say DB dots users some no one to say at this record to the user stable. So user database, user stable Add this user object And then, after all of that, we save changes. All right, Now, let's see if this works. Someone toe klik start a lot being as the admin user so that I have access to the manage users. I click manage users know we still haven't populated this great. And we soon get to that. Let's click add User. And here we see our form coming up. So we put in the user name and then we can select the roll. All right, that looks good. So I'm going to put in another user and what to say Admin one on this person. This news is going to be in the administrator role. Then a click submit, and I want to show you something. So before I click submit, I am going toe put a break point right at Lyon 40 it. All right. So are my line 48 or the last line off this function Because I want to check something with you. So we're going to click submit on, then it got this far with no errors. So my assumption is that it went through it created the user the password on. Then if we look at the user object under strolled on, we will see that we actually have an idea associated with this. Users seek and look at that hush. That's an entirely different from what we're used to. But what I want to point out to the fact that once we got this far and we added the record and we save the changes, it automatically updated over user object toe Have the I D that it got after it was added to the database? All right, so the relevance off that is, remember that issues the object is is playing its dome. It's it's it's only knows what we tell it. So we only told it. Tohave are using him. Were only told it have a password on Devon is active state of true. We didn't give it an I. D. Ideas are being auto generated by the database. So the fact that it did, the save changes. And then he continued it. The court can reflect that it was saved in the database and in no has an i d off four. And that will come in handy on I'm going to show you exactly why. So I'm just going toe. Continue. All right on, then. Well, you know, we need to put in our try catch and our success message and to close the window after everything is done. So we have all off that to do. But I just want to focus on something first. Remember that we just added a user and we selected the rule from the drop down. However, based on hole, we have designed our rules. So if I just refresh this rear on this query real quickly, then you will see. So I hit the wrong button so we'll see our new user being added here. Alright, butts in our user or stable. We don't have any reflection off any new user within the neural. So of course, if we're adding a new user and we're selecting the rule from the drop down that we need toe insert the rule accordingly. Are the User Role Association in this table where the new user I was added with the I D that we just saw in this case was four has the role I d that is associated with what was selected from or drop down list. So we need to me that modification to our court. So after we've saved the change to add the user we can know, say var user i d is equal toe or user object dot the i d column because no, it has a value writes like we just saw No, after getting that user idea and we already have role idea and the middle strange is too common are keeping consistent. So have role idea We have user I d Then I can go ahead and say on the score db dot user rules Art Well, sorry, sorry, sorry. Skipping ahead of myself there so I'll save our user role is equal toe a new object off user rule which is my table, right? Some creating an object off type user role on then just going to initialize it here on then the columns that I have are role i d and user I d. All right, So role I d is going to be equal toe the value that I got from my combo box on then user, I d is going to be equal to the value that I just got from my newly created user. And then after I do this, then we know the next thing would be toe other record to the database of DB objects dot user rules. Not on on. Then I went to see user role on. Then I'm going to say db not save changes. All right, coming on nicely on then. Once this is done, it will create the new user and then fetch the i. D then assigned him to the rule or her. Right That said that user to the rule. So before our continent is going to put in the try catch So just writing. Try pressing it up twice on. Then I put all of that operation inside off the try on day. I'm not one. Just roll. Instead, I'm going to see a message box docked. Sure on. Then the message box that show I'm just going to see on arrow has occurred, right? Something generic. You can always get a specific as you want. But then, you know, we've been through this quite a few times on then. After a successful at operation, I do want to display a message box in. Ah, Sorry. All right. Seeing new user added successfully. So I just a new user added success fully on. Then I will close the window. All right, on then Well, that's pretty much it for the art user. So let's just try it out one more time. Someone to click, start then we log in, go to manage users than a new user on. Then this time I'm going to save you. Clark, that's this person's using him on. I'm sitting them to review only and then I click Summit. And then this is new User at it successfully click OK on, then, while the break point is to there. So I remove the break point and then continue and they were back a Tormey and window. We haven't done anything with a great just yet. But then, if I go to my database and I re execute this query, then I'm going to see ah, third roll right with user I d five. Remember that before? No, the use ideals force and all five on the role ideas. Three. And if I go over to users on re execute this query and I'm going to see my view Clark with user i d. Five on then the password because they both have the same default passers. So these two passwords are the same. These two pastors are the same. The hashes are the same, right? So it's I mean, it's not. But if the hashes are to see And the point is that you know, if you look at it, you can tell if I didn't tell you which one was which you wouldn't know which password or what the English word or equivalent off these passwords would be. So the next thing that I want to do know is to modify my manage users windows so that when a user gets added, or, you know, deactivated or something, that it should reflect here that that data is going to be reflected. So I'm actually going to do it. I want you to pause the video. Onda attempted yourself, and I'm going to pause right here. Do it on, then I will come back and explain it to you. But I would really encourage you to try it on your own. All right. So I'm just going to walk you through the adjustments I made. Andi, I hope you, you know, pause the video yet attempted ITI. And if you're still on feeling comfortable enough on your world, you're waiting on me. Then that's fine. I'll walk you through what I did. So I created a similar function to what we've been doing upon to know called populates Grade on in Popular grid Iran, a quarry where I said on the PCs it of cars. That's because I actually copied and pasted the court from or previous work, right? So in this case, then we just change it quickly on DSO. It shouldn't say cars. It should say users. So I got the users I selected into that new abstract object getting the i. D. The user name the roles. So remember, had to do que dot user. Was that first our default to get the first roll Or, you know, it's a list off Rose based on the data construct, but in reality it's one roll, right? So I'm just saying, Give me the list. I know it's only one in the list, but the way they process it to see the list starts one. So since it's at least give me the 1st 1 in the list on, then tell me the role name. All right, on, then, the active status. No, remember that these column whatever, though whatever the column name is what will be used as a column name for the grid. So I don't want user name looking like that and, you know, name And what is the name? Name doesn't mean anything to an end user. So I renamed the columns. So I said, whatever column has used the name is the name Set it to use the name. Name should be Role name is active. Should be active on. Then we said the I d visibility toe falls. All right, on then what I also did was I modified the constructor for the end user. So when we click, add user and passing in on object off this and this being the manage users form right on. Then I didn't bother to do two constructors, so sometimes I did too. Sometimes I did one something I did too. And I haven't used, you know, one off them. So this time only did one. Because really and truly, I don't think I recall in the abuser window without an object off manage users. So you know you can mix and much. It's up to you to determine what you may or may not need to do is I just modify the existing constructor toe. Have the harem it off. Manage users on. I declare the private object private property. Sorry. Off Money's users initialized it here on so that alone, you know, after clicking, submit toe call on the populate grid function in the manage users window. All right, so once we click submit and it does out off the change it's has to do to the database we show the success message on. Then we refresh the grid, and then we close this window. All right on then. I also call this populate grid for in manage users. So in money juices, remember where two other buttons with a reset password reset on. We had the deactivate, so the activated, more than the password reset, would need toe refresh because it for a change, the active status. Then that's visible data changing because we have the is active column showing through our Farr's. So we need to refresh each time that is done on. Then I need to wire up. This refresh button doesn't make sense. We have it and it doesn't work. So let me just add the click event to the refresh button on this refresh button is just going to see Populate grade. All right, so let's just look at this application. All right? So we opened up, logged in, go to manage users. So that's what our grid will look like, so ever test user notice. I'm not showing the password off course, right. I mean, it doesn't make sense to show the password. This user, remember, this was the first user were created, and we didn't set a role, so that's going to be blank. So I can actually just use this one for the activations. Let me just click admin one and try this. The activites are admin ones. Active status has changed on. Then we see that it changes toe falls there, refreshes the grid and changes toe falls. Right. So, of course, the grid doesn't look all that attractive With all this great space you can resize don't know when to prioritize those things too. I just want us to get the function of stuff under our hands. Right on. So let me just create one more user on. Then I was going to call this user clark, and this person is going to be a data entry clerk. Submit and then click OK on, then. There we go. So the window closes, and then the Clark is there. If I had the activate, then the active status is now falls, all right? And then just to see him, we if I click. So if I let me refresh and then if I try and click one of these were going to get those arrows. But because of the tri catches, it won't crash the program. Right. So those are the wonderful things that we have been able to accomplish with all of this. Now, let me just put in one final function on that is the check if the default password is the pastor being used when the user logs in. So they're number four is that this can be accomplished, right? Pretty much the root of it. What we're checking is if the password that the user used to get authenticated is a covenant or we know is the default password. All right, so what I'm going to do is modify the main window lord screen, right? So I'm going to have to create a new form, and that will be like our password reset for which is really just two text boxes on the submit button so I can just quickly go to Solution Explorer at a new form on. Then this one is just going to be reset. Sward. All right, click odd on. Then you know, we can do our regular cosmetic adjustments or reset password. Andi, what I'm going to let this one do really is it's going to be a very small, um, window. Our girls going to have to labels and two text boxes. All right, so I just designed it off screen. You can pause and design years also, but have to labels want to say enter new password. Want to see? Confirm password on. Then the two text boxes for either, which I renamed two TB password and TB confirmed password on then my button, which I call Bt and reset. So and I don't think this, um well, this form is going to have to talk to the database. I'm goingto have toe No, my initialization, as usual. All right, on then. Know that I have made reference to my db object. What I'm goingto have to do is retrieve the user, change the password part on, then save the changes. If I'm going to need access to the user that should be modified. And I went after ticketing as a parameter someone to say, user user on. Then I'll have my local eso that's private, user and then see underscore user. And then I'll initialize on the score user to have the value off the user being passed in as a parameter. All right on, then. All right, so we've done quite a bit of work on the reset password. I want to pause here, and then I'm going to turn my focus again to the main window. So I'm just going to close all of the's tops that I don't need a right. No. All right. On then, on the main window in the main window in the Lord function. What I'm going to do is say that if they if the value off the users hushed password is the same as the default hash password, then the person needs a reset crossword. All right, so what I'm going to say here is so in the window. Lord, I'm just going to make some space here, and I'm going to see, So what I'm going to see, you know, is if on then say something like User. So I have my user object here user dot passwords. I would already know the hushed password based on what's in the database on. Then I'm going to say if that password that's in the data base is equivalent Oh, you tills that on. Then this would be default hashed password. See that? So I'm seeing if the password in the data base is equivalent toe the hushed password. That is the default Tash pastor government. But this function is returning the hashed version off the default password. So anybody who's postured was just reset or was just created. He's going tohave. Whatever possibly this return on. That is what is going to be stored in their records. I'm saying If the user who is just logged in has the same password as the default hash password, then we need toe launch. Ah, window for reset postures. I want Cevaer reset. Password is equal to a new instance off the reset password window. All right, on. Then, this time I'm going to show dialogue. I noticed that this is saying reset password eso because I didn't create two constructors, so it's expecting us the pass in some user object. So that satisfies that. And then I went to see Reset password And in this time, when to show it as dialogues. Remember that we looked at how we can launch one window at the time. Sure. Dialogue is an excellent way to make sure you can't just close it just like that. Right? So we're seeing reset posture dot show dialogue right on. Then what will happen is that the expectation is that the user will use that reset passer dialogue on actually changed their password. If they don't, then that's a problem. But hey, so when the window lords we do that, check on, then we display the reset password window. So, no, When they get that window, then the expectation is that we one get when we're well, we're going to get the values that they entered. So that's just a fire password is equal to on. Then we have TB password, not text. Right on. Then the next one is going to be TV confirmed postures of are on firm on the score. Password is equal to TB. Confirm. Ah, sward, not text. All right on then. First they want to compare them. So I'm going to see if password is not equal to confirm password. So we know that that's a problem. If use any modern website, you know that the pastoral on the country and pastor bodies must be the same then is going to see message box that so And we're gonna skin to warn the user that passwords will not much. Please try again. All right, And then otherwise we're going to know Say that our on the score user object dot password is equal to you tills dot harsh password, and then we're hushing whatever value they put in as the password. And then once we're done with that, we can see underscore db dot save changes because we have access to the actual object coming from the data bees. So we're went to try that. I'm this has worked for me in the past. I don't want to say it definitely is going to work. No, but, I mean, we're going to test it. Oh, no. So that means start. And then I'm going to log in with one off those new users that would obviously have one of those new, um, passwords. So I went to log in as the Clark. So Clark Andi, it would be capital P S W O R D at 7123 That is the pastor that we set on DWI. Okay, it's stunning or studies are involved. Ah, it's telling us it's involved because this user is said to inactive. All right, so at least something work. So the tire kinda passer, But the user is inactive, so that didn't work. So let's try a test user instead. Someone to test user. And I'm going to type it in lower case because in the in the actual database riveters mixed case. Let me see if it works, OK, it does work. And then they're telling me that I have to enter a new password, right? Someone to change this password to the seam were test user. All right, so we need to fix that because I didn't set the pastoral char on the password and confirm Posser, but lets us try the reset passer non the less All right, so a tested it and it does not work. And I mean, that's the That's what I was fearful off, to be honest, but it's not a surprise to me, and we can still whether that some Because they have a bit off work to do. Still, So what happens is that even though the user object is passed over where not we're technically not connected to the database by the time we hit this point, right, So we can just dvc if changes. Because technically, there were no changes me toe the database. All right, so what I'm going to do, I'm going to exit D boat moored on. Then we'll just make some final touches. So first I'm sitting the pastor char for my text boxes on DSO. My pastor chart here is going to be asterisk for both off them so that, you know, we don't see the plane takes two in the person stepping in their postured. So that's one. Ah. Secondly, what I'm going to have to do is still retrieve the actual user from the database, right? So I'm going to have to save our user notice, not underscore user. The standard issues er is equal to DB dots Onda se users dot first or default and then Lambda expression where say Q r o. And then q dot and I just want where the i d in the database. Much is the I. D. That is, in my current user object art. So where those Too much I want by that user on. Then I will say that this user that I just received to the need to be some still connected to the database here I'm seeing Set That's user that I just retrieved toe, have the new password and then see if the changes right off course we want toe as you this customary for up all of that inside off a try catch. So right, Trey Per stopped twice on. They were trying that operation. And once that operation is successful, we show a message box to see. That's the posture. It was reset successfully off course you don't want to just print or what the password was was So possibles reset success fully. All right. And then after that, we can close the password reset window and then if there's an exception, I don't want to throw. But I was sure our message box, seeing that there was an therapy share again on Arrow,
36. Add Project to GitHub: in this video, we're just going to review some final chord changes and then check in our solution toe Get hub for source control. So I'm going to start off with explaining this class that I have here on screen which is public static class. And I called it you tills. Alright. So first, let s static class I lows you to declare class where you can call on the other static functions and properties without needing to declare an object. So I'm going to explain exactly what that means. So, firstly, in the social Explorer on that it was right. Click on the sign on the project Goto ad again say class on. Then I gave it the name you tills so you would get the same dialog box that were used toe where I say class on. Then I called it you tills on. Then I said add and they got added to the project. So we see you to some people would put that in a folder maybe by itself and in a folder called maybe helpers or something like that. At this point, it was really matter but my utilize class is static, so that means I can just say you tills dot on and call on the static function which I have here, which is called Foreign, is open. All right. Normally, when it's a class, we know that we need to declare an object. So, like in the case, off declaring an object off one of the forms which is which are classes would have had to say Give me an object on initialize it to, on instance off that and then I can start interacting with the properties on the functions when it is a static class. Then in this situation I'm seeing give me you tills dot the functions I didn't need to declare you tills as our object and then use our declare an object off type utilize and then use the object. I could just say you tills dot form is open that I'm passing in the name off the form. All right, so instead, off right in the court here, like I did in the previous video where I wrote it one time and I wrote it in not one situation, but then every time I need to check if the form is open, I'm goingto have to rewrite that court. What I did was create a uterus class. Create a function called form is open, which is going to return a Boolean. And it is expecting a perimeter off type name or will type string called Name on. Then it does that check. So I get all off the open forms in the application check If the one with the name that is being passed in is open, right? So we just did all of those. I just took that chord on all that it was putting a function and a return is open, which would either be true or false based on the outcome off this, um function right. Are this operation on? Then the function is really returned. In the true are falls, which is bullion, right? So that means every time, have a quick event. Where are every time a situation where we're about to launch a window? All I'm doing is seeing, if not on, then whatever the result is, when I call you tills, that form is open. And then I passed in the name on. Remember that the name off the forms to see him as the type, right? I know. I wanted launch manage vehicle listing. That's a form about to launch when that this clicked. All right, so I know that that is the name I'm passing in. So I'm just saying check, call that function on. If it is not meaning it is falls, then I want open it. So if it is not open, that's basically saying if it is not open then shows remember that we did it as an if statement, but once again would have toe rewrite that court every single time so that it was put it in a function where all I'm doing is passing in the relevant name according to the quick event . Right? So that was it for the manage vehicle listing later. Don't have manage or entire records. I use the same function. All I do is change the name because I know it's a different form that I want to launch this time. Is it open? If it's not open, then go ahead and launch it as an MD I child on its proceed. Right. So that's one change that have made otherwise I have normalized the manager. Entire records are putting the same delete kind of functionality with the dialogue box. And yes, I know, as you would have done in the manage vehicle listing on dive normalized. Everything are most things up until this point. So, no, I'm about to check this cord into get hope, right? I'm going to create the repository on. Then, by the time you would have watched this video, you would be able to go on, get open access the repository with all of the's court files. And, you know, if you don't understand something or moved too quickly in the video and weren't able to get it clear, and you just want to see the court itself, then you can go to the post story that we're about to create. So I'm assuming you have already created a ghetto by Kant, and if you haven't, then it's pretty simple. I also have a video on that you have seen it earlier. Spirit A ghetto by account. You can go to get hope dot com on. Then you can go to sign up on this, a pretty straightforward process. Once you sign up on, do you provide all those details? You want to choose a free account because they load off free or enterprise are cones off course. If you're an individual just wanting to use it for educational purposes, then you go ahead and use the free version off course once it's free, can choose. If the repository is public or private at the time, Andi can proceed accordingly. So I'm not going to go through that in this video. There's another video there that is with that. So right now we're going to focus on adding our solution to the source control. All right, so the first thing you want to do is look in the bottom right on the corner. You see, add to source control. If you don't see this, then you probably want to don't know the visual studio plug ins. Forget on get hub. But if your visual studio was installed properly, then you would have the plug ins that I'm talking about. If you don't, then you can drop me a line and then I can assist you with getting them so you can click, add to source control and then you see, get you Go ahead and click that on. What this does is it creates a local repository on the machine that you are on, so source control allows you to track your changes. Um, off course, you have to make sure everything is saved, and once you're tracking it changes. You can tell who changed what file on when. Right? So it's a good way to know when your coders working, what changes you made. That probably broke your court, and you can always go back to the previous working court. It's always recommended that you don't check in any court that has errors, especially if you're working in a team. But, you know, those are our principles that you learned along the way. But right know what I just did was I created the local repository on my machine. The next step, however, is to publish this to give up, so my machine is going to be tracking changes. But then I can synchronize it with the service called Get Tobe or as your develops. But get up is easier for every and anybody to get onto, so that so I'm focusing on get up. But you can synchronize it with get up so that your local machine instructing changes and you are basically backing up. All off those tracked changes toe the clothes repository that get Tobe affords you on, then that makes it easier for it to work in a team. Because then your team members can interact with that clothes repository at a global level on contributes to your court or a sheer your court. So right now, what I'm doing is pushing it to get up so that you can access the court when you need to write. So I published a get up and I just did that, published a get tub and then it's going to ask me. OK, it's just going to verify that it's get up. Know what it may do is prompt you to sign in. So if you have created your cone successfully, then you just sign in with the credentials that used to create that account. I'm already signed in to my visual studio with get up, so it already detects me on. But I'm going to leave the name. There s current up. You can add a description so you can see. Um, you know, win forms our introductory. Let me just write out the description. All right, so I wrote a little paragraph. This is an introduction or Windows form application done in partial fulfilment off the course enterprise application development with Windows forms using C sharp and a diode on it. So this is a nice It's a description, so that if anybody stumbles upon this repository, they can have an idea off. What, exactly? They're what it is exactly, that they're looking at the check box here to make it private or public. So if you make it private, then obviously nobody will be able to just stumble upon it on the Internet. Only you, on whoever you invite, still interact with the repository will be able to see it. But I leave this unchecked because I want mine to be public, right. I want you to be able to go on my profile and seat at the end off all of this. I can this click publish on, then I can give it a few moments on once that operation is finished. Then you see the functions are you see the visual cue that's his repository created, successful, You know, if this step fails, it could be that you probably put in incorrect credentials, or you probably need to change the connection that you're sitting behind because I've seen firewalls block this operation. So it could be one off those things. So I didn't off the day. Whenever you make changes to your well, first, let me log in to my ah ah, you tub account and show you exactly what has been done. So you see current up, Andi, There it is with the introductory. This is the description. All right, So if I click on current up, then you're going to see my entire posit Torrey, with that description that I just wrote in visual studio. It appears here, So if I go into the folder will hear all the facts. So that's a solution that get fires that would have been hidden are created once we added it to the repository on. Then I go to car until up on. Then when it goes into that folder, you be able to see all off those class for. So this is a project folder right on this. These are all of the fathers are associated with our project. So if you wanted to see what I didn in particular file, you can just click on it. So this is a logging that CSC and a slick on logging. That's yes, and it will bring up the entire class fire with all off the cord that I would have checked in. And you can always bro's. Peru's Andi, you know, re acquaint yourself with the court if you need toe from the repository. If you want to fetch a copy off the entire project, then you can go to clone or don't Lord on, then this gives you three options. You can well opening desktop. So in the video where we set up the ghetto Balcones, you would have also don't loaded. Get up for a desktop so you can open it in that desktop application. If you already have it, you can open it in visual studio. So this will actually from two to launch my source code. In an instance, off your own visual studio's. He can see it in your project setting in your visual studio as if you had my project on. You can also just don't know the ZIP file for later use at will. Right, So that is how you go about adding your repository toe. You tobe no, just to show you what the purpose of guitar is if I make an adjustment. If I look in the solution Explorer first, you're going to see the blue padlocks. Blue padlocks mean that these fires are unchanged. All right? No, If I go to you tills and I'm just going to do a simple change, nothing too intrusive. But I just need a space. So whatever the change is, and then I see of it, if I look back at that file, I'm going to see a red tick. So then, if I hover over the ridicu see that it's pending on edit our there's an edit that was made that is being trapped on dit is pending being checked in. All right, So let me let me make something more, actually and a more useful change. So I'm going to go in to put in the common. That's his check if window is already open. Right? So before, if I go to my repository and look at my uterus file, right? So I just went into the repository. Open up the utilities. That's yes, and others zoom in a bit sick and see better. You're going to notice that it's just there. There is no comment or anything, So I just added Ah, comment Toe that line. Tow line 14. The original has no comment in line 14. So what I'm going to do is check it in from visual studio. So I'm just going to right click on the solution, and then I'm going to see a comet on. Then when I say commit, I want to be as explicit as possible because once again, this is good for team settings. So if I'm in a team setting, I want to let the team members know exactly what change. I'm Mead toe the court, someone to say I did comment toe you tills not CS because that's what I did. Right after I write that comment, I can click on this. Don't correct. So I can say Commit are, But I'm going to show you a shark. What? Where? Click on this, don't carrot and it's a commit all and sink, so commit means that is going to commit the change. But it's only going to be to your local repository, which means get aboard know about it, at least not yet. When I say calm it all and sink, it will say okay. Committed to the local repository and then synchronizing with get up synchronizing means that if I have changes made, it will push them to get up on. Then if anybody else on my team made changes, it will also pull those changes to me. So when I do that, you see that it's doing operations while I'm here talking on No, it's synchronizing This might take a while because off the Internet connection that I'm sitting behind. But once it's done, then you'll see that it successfully synchronized all going and incoming comets. And it changes that my teammate will no be on my machine on any changes I made Will know beyond get Tobe on, my team can also come here. So this is a team Explorer. I have a pain here called Team Explorer. If you don't see it, you can go to view and look for Team Explorer on that will give you this pain on by default , it will be on this screen. Let me just go to the home screen just to show you what it will look like so that you all right. So I have This is the whole screen, right? This is when the repository set up, and that's your home screen if you want to get the changes. So somebody made changes and you want to retrieve those changes than what you can do is click sink, right? So any click sink it will automatically just pull. Any changes are, well, it will bring up this menu on. Then you can just say pool. So when you say pull, it will bring in all the changes that your team has made. If you just like, think it will do the push and a pull so pulled means get the changes, push me and send my changes. Think means Alice to both of them in one operation anyway, right? So you have a number of ways to do it after showing your options. But in a team setting that is whole, it can be done toe help collaboration, so know that this is checked in. Let's just verify that it has been checked in successfully, so if I refresh this view, then you're going to see my comment is no in get top. So the change I just made on my machine is no available on get top and then if I just go back to the list off files. I'm just going to show you the visual cue that ghetto gives you. So these were at project files? That's all. I was auto generated. That was the first commit. But then when I I did the changes made so till you till that CSR utilize a C s. I wrote the comment added comment, Toe YouTube's that CS, Right? So it will be a whatever you put as your Corbett message will inform anybody else what changes made to the particulate file. Or at least the last change that was made to the particle affair. No, I talked a lot of boats working a team and I'm just going to show you how you can invite other people to be on your team. So firstly, your teammates need tohave ghetto Buck Owens all. If you need tohave guitar balcones on, then you. What you do is go to settings. So whoever would have created a repository one person can create the repository first on, Then you can invite the other so you would have gone to settings and then you would say, manage access, and then this will probably prompt you toe you know, put in your credentials. So then you do that quickly. And then from here, I can invite collaborators. Right? So I have a few persons who are following ghetto waitress, but in collaborators really are the ones who will be ableto access on push changes to your repository without you needing toe, you know, be there all the time. So that's what I said team. Right? So if I invite a collaborator, we just need to provide a user name or an email. So I think maybe I can look for somebody named dear Itchy. I have a new associate by that handle. I don't remember exactly the handle, but you can see that it's bringing up a number of people. So maybe using well, email address would probably be the most sure we to make sure that you're inventing the cart person. So you put in the person's email address. I just put in one. Okay, there is known by that on. That's my other Gmail account. I thought I still had I get up account. All right, so I'm just going to use my friend Sean McLean on. Then I just click Sean McLean, and then I say add show McQueen to car in tow up. So by doing that, it would trigger an email invitation to him on. Then you would be able to accept on, then have full access to the application, just like I'll associate spending invitation. You can always cancel it. If you want someone to cancel it. I'm sure he's going to come and ask me, Why did I do that? That's fine. I can handle that situation. But that is how you go about adding other collaborators to your project. With all this said and done, that's it for our introduction toe windows Forms application development using C Sharp and video dot net. I hope you had fun. Andi. I look forward to seeing the additional functionality that you put in our common that you put in something to manage. Maybe the users you can probably put in something else toe. Do the audit logging like I mentioned, to see which user interacts with which file on, you know, you can do a number off different things on. I'm interested to hear exactly what ideas you have in mind to improve this application. You can always drop me a line of my contact information on let me know your thoughts