C# Full Course - C# Tutorial For Beginners
C# Full Course - C# Tutorial For Beginners
basically a 100 free full udemy course all crammed into one video there are
hundreds if not thousands of examples in this all the code is organized and
available for free on GitHub and best yet for the rest of theirs this video there
are no advertising as you can see everything is well organized on GitHub and there
is a ton of code in all of these examples there isn't just a little tiny bit
here is just a taste all right and I have a lot to do so let's get into it
alright so in this tutorial I'm going to be using visual studio community and
you're going to be able to get that at Visual Studio microsoft.com forward
slash vs for visual studio slash Community exactly like that and you just click on
download and you download it and it's going to work on Windows as well as on
Mac but of course I'm going to be using Windows for this tutorial and whenever
you open it up here is visual studio and first thing we're going to do is
we're going to go into file and we're going to go new and we're going
to say project and something like this is going to pop up what's most important
however is the visual Studio C sharp right here that's what you want to have
selected all right you're going to click on next then you're going to
decide where exactly you want this to be stored I'm going to select the folder
for it and we're going to start off just leaving the project name be console
app one because this is a tutorial and that's fine and we're going to click
on next you I'm using.net6 which is the latest version all very good click on
Create and whenever you do that you're going to see a window that looks kind of
like this so what we're going to do is this is our program our main application
where our main function is going to be and we're going to come in here and
change this up of course now very first thing I'm going to do is I'm going
to type in using and system and this is going to import a namespace called system
and it has a whole bunch of classes which also have functions inside of them and
what it's going to help us do is let's say if we want to print something to
the screen without this we'd have to say something like system dot console dot
right and so forth and so on but whenever we have system inside of here we can just
type console right like that and that's uh we'll be making namespaces of
our our own actually right now now my interface probably looks different from what
you're used to seeing I enlarged it a little bit and how you can do that is you
can go into tools and then go down to options and I enlarge my entire environment
right here but I'm also I think I want to enlarge my main text area so
environment is the entire like text on all of the different screens and where I
want to also enlarge it is in the text editor spot and let's say that I want to
take this up a notch so that you can see it it's set 24 is the maximum
let's try Authority and see what that looks like so I'm going to say okay
and now you can see the text here has enlarged a lot let's give ourselves some
more room so we can type now the very first thing I'm going to do is I'm
going to Define my name space so I'm going to go namespace like this and
I'm just going to go with console one and then we'll be able to pull from
this all this code that we write here later on not right now we'll get into
classes much later I'm going to Define that my class is going to be called
public class is going to be called program and I'll like I said I'll get
into classes later basically what classes allow us to do to be very very brief is
to try to model Real World objects in code and later on in the tutorial for example
I create Warriors that fight to the death and in doing so I create a class called
Warrior and I Define all the different abilities the warrior has and the different
attributes the warrior has and that is all put into a single Clash but we're
starting off very simple here right now and mainly what we're going to do
inside of our classes is just have a main function that is going to execute code
and all of our code is going to start executing and our main function so I'm
going to say static and what static means as is that this function can run without
creating an object previously so we Define classes which are like blueprint events
for creating objects of that class so if I have a class called dog and I create a
version of that dog then that would be called an object all right and static just
means that we don't if we want to go and use this function main then we do not
need to create an object called program to be able to execute it don't worry if
this doesn't make sense as you see numerous examples over hours all of this
will completely make sense so static doesn't make sense don't worry about
it when I type in void here what that means is our function called main is going to
not return any values that's what that means so it's going to execute
it's going to go and do some things but one thing it will not do is it will not
return any value after it's done executing then what we're going to do is
we're going to Define string this is a string array an array is just a bunch of
boxes that contain data inside of them and in this situation what we're going
to contain inside of here inside of the string array called args is any data that
is passed to our function when it executes in a terminal window again you're
going to see a terminal window here in about five seconds so let's just show
you a terminal window to clear this up you want to print to the screen see I
didn't have to type system I just typed in console and I'm going to go
right line and then inside of this let's just do the obligatory hello world
just so you can see everything and you're also going to get to see the console
window so we have all that there let's save that and I'm going to come up
here just so you can see it I'm going to click on this little play button up
inside of here all right so let's click on the play button and a console window
is going to open up right here and if we zoom in you can see it says hello world
exactly as we would expect it okay so good stuff all right so that is the console
window that we are referring to now there are a bunch of different console methods
I'm going to show you a couple of them some of them fun all right so let's
say we would want to change the text color inside of our console we could come in
and we could say console and foreground color look at that just pops in there for
me is equal to and then you're going to say console color and it defines Yellow
by default I'm going to have my text be black so let's go black like that
and then let's say also that I would like to change the color of the window
behind well that's actually going to take two different commands this first one
is going to change the background color only behind the text and to do this I can
say background color there it is and there it is and I'm gonna have it be white
now if I want the background color to apply to the entire window what I need to do
is say console clear like this and there we are so there is our new application and
we can run this also and here now you can see that we have our window it cleared
the window so it doesn't show the hello world anymore but it's showing me
that it is black text on a sort of white background I guess that's Windows
version of white and of course if we didn't take the hello world part right
here and let's go and throw it after all of that I'm going to be deleting
this in a second so there's some point in clearing that up let's go and
save this and let's go and run it again and now you can see the hello world
part is actually displaying here using our black text just as we would expect it to
now what we're going to do is uh do a little bit of interaction with the user
so this time what I want to do is previously I used right line what right line does
is it inserts a new line after it it prints out text well let's say I don't
want it to do that so I'm going to use right instead I'm going to say what
is your name like that and then maybe I'll put a space here at the end of it
okay so it's going to print what is your name and then directly on that same
line the user is now going to be able to enter their name now we are going to go
and store text like names and letters and numbers and what what for and inside of
what is called a string and I'm call this this is creating a variable called
name that is of type string we're going to get into the different data types
here as soon as this is done now if I want to read information from the user I can
say console and read line and that is going to go and accept user input on the
console and then store whatever they typed in into name after we have that I could
say something like hello and print out a real message using the person's real
name so I'm going to say right line and I want to insert the variables value
when I say variable I mean the values stored inside of name directly inside of
quotes to do so I'm going to go and put a dollar sign here and then I'm
going to say something like hello and wherever I want the value stored inside of
name is going to go between curly brackets and of course close off that with a
closing quote and from now on I'm just going to hit Ctrl F5 so I'm not
going to click on this anymore that is the shortcut so Ctrl F5 and what is your
name shows up directly inside of here and I can
come in to this guy and what I'm going to type in Eric all right can't do
that at the same time so let's type in Derek like that and now you're going
to be able to see what is your name Derek hello Derek okay so good stuff exactly
right there all right so let's get this out of here and let's close that
now I want to talk about variables now variables are just going to be used to store
different types of data and we're going to use very specific types of data
either because it's going to allow us to perform certain functions on that type
of data or it is going to conserve the amount of space that I want to set aside in
memory for storing the values that could potentially go into these different
variables very first thing I want to do is I want to create a Boolean and it is
going to be called can I vote and these Boolean values are either going to have
true or false values that is it and whenever we get later on into conditional
statements which are going to perform you different code depending upon different
conditions you'll see more about booleans at that point in time we also have
integers now integers are going to be 32 or 64 bit and we're going to say
console and I want to go and actually print out the biggest integer you can
possibly have so I'm going to go here and I'm going to say biggest integer
and I am also another way you saw that I used that dollar sign previously to
directly insert the value we can do it that way or we can also come inside of curly
brackets and label these zero through one depending upon what value we want to
store inside of here and to get the maximum size for an integer you just say int
and Mac max value like that and that is going to give us our biggest integer that
we can possibly store inside of our integers let's go and copy this and then
let's make a copy of it and I'll show you the smallest integer you can
store so this was going to be the smallest integer and to get it guess what this is
min value instead of max value let's do a little bit more though there are also
going to be Longs and a you're going to see the biggest long we can have and
let's make this biggest long like this and they are going to store 64 bits of
in size and then we just type in Long here instead of int and we're going to
get that and let's go and let's just leave that as men that's now
let's leave it as Max I have to change the text somewhere so let's just
change it here so the maximum value for a long and copy that and this is going to
be the smallest value and inside the description for this video is a link to
whatever you could possibly want to learn about C sharp all in one place all right
let's do some more before we output this um let's say we want to be able to
use decimals that's the thing with integers there's no decimal points well
if you do that then you're going to want to use a decimal data type so a Dustin
let's go and create one I'll call this decimal some more I value is equal
to and let's go and throw in a big value inside of here boom and boom and
you're going to end this with a uppercase M then I'm going to also come in
and I'm going to create another because I want to show you the Precision that
you get from a decimal data type um they're basically going to store a 128 bit
worth of decimal values which are going to be accurate up to 28 digits which you
will see so I'm going to go decimal and I'll say big num is equal to and
I'm just going to throw a large number inside of here so this and let's
throw that inside of there good now what I want to do is go and add them and show
you how precise they are so I'll say console right line like this and then
inside of here I am going to go quotes and I'll go decimal and pi Plus big num
is going to be equal to or this side of here like that and then I will go and get
this value so I'll say a decimal Pi Val Plus decimal big number and let's
throw let's go and put this on a separate line just so you can see it a little
bit better and there it is and let's go and run this now so run it and
we're going to be able to see these values so the biggest integer you have
right there and also the smallest integer and the biggest long and the smallest
long and you can see here there we were adding our different values working with
decimals like I said they have 28 digits worth of precision in just the decimal
part so let's go get rid of that and get rid of that all right what is the
biggest decimal you may ask well let's just go and grab this and copy this and
paste this down inside here and so this is going to be the biggest decimal biggest
p i g e s and decimal mole and to get that you're going to type in decimal max
value so decimal and max value X value and if we go and run that you're going
to see this big number and there it is all right so decimal is quite large okay
let's get out of that and uh also we're going to work with doubles so
let's just go and get rid of some of this excess so there it is and doubles are
going to be 64-bit float types and let's go did I save that yes I did okay so
this is going to be the biggest double and let's go get this and we're just
going to change this into double Max value and let's go and do some more with
doubles here let's say that we let's move this over also there we are all
right so what else would we like to do let's find out how precise a double is
so so say we have double and double pi value is equal to and I'm going to copy
PI right here because I don't have it memorized to numerous digits all right so
we have our double pi value and then let's go and copy this also so we
don't have to type this out again paste this inside of here and I'm just
going to call this big number again so big number and let's throw in a whole
bunch of zeros so let's just taste this and paste that inside of there and then
after we do that let's go copy this big guy right here biggest dist I just
realized I typed biggest no that's not good all right so let's copy this
also let's paste this down here to see exactly how precise these are so this is
going to be double and I'm going to say that I want let's go right here and
we'll say pi plus big number is going to be equal to and then to get this value
I'm going to go get rid of all of that go down to the next line and I'm
going to say double and I want the pi Value Plus and double and I want the big
number and there that is uh why don't we go and also do pretty much exactly the
same thing for floats okay so let's copy all this and let's come down here
so first we're gonna have doubles then we're gonna have floats so we're
going to say and the difference in them is how accurate they are so let's go
and change this to biggest float and all of them are decimal numbers that's
what every single one of them is so that and then we'll go and highlight this
and change this into float like that max value that's fine and then we'll
go and have this be FLT so FLT and these are not going to be as accurate and the
difference here is with floats when you define them you're going to end them
with an upper case f and let's go get some more of them so this so when
you're working with decimals you end those with an uppercase M when you're
working with floats you end them with an uppercase F when you're working with
doubles which is what you'll very very commonly use you're going to have
nothing at the end of the definition for that so this is going to be float so
let's just make it FLT I plus big number and then we are going to just change
this to float pi value so FLT pi value and Float big number and let's go and
run it up there's a builder no I don't want to change that uh oh this is
should be float also so float like that and run it and and here we're going to
see more information kind of like that okay oh there we are so you can see the
biggest long well it says biggest long that's not good let's go back so
it's not long it's biggest double so biggest double don't want to cause
confusion so save that run it again and here we are so here is our biggest double
and you can see the biggest float quite a lot of difference between them and you
can also see here the doubles are 64 bits and you can see the Precision that we
would get with a double versus a float which is a 32 bit double Precision is 14
decimal digits while the Precision on a float is only going to be six digits now on
top of these data types that you just saw which are the most common data types
you're going to be using there are other different data types there's bytes
characters which hold a single character and S bytes and shorts and unsigned
integers unsigned laws unsigned shorts and you can see how large those different
values are and this is also how you make comments inside of visual studio and also
what you would be able to do let's go and get rid of everything except for this
if you would want to have a multi-line comment you could go and put a slash with a
star and then you could go multiple different lines afterwards let's say I have
other data types and then you're going to close that off with a star and a
forward slash and that ends your multi-line comments all right now something else
that is very useful is to be able to convert between the different data types so
let's go and let's create a Boolean and I'll call this Boolean from
string you just have to make sure that whatever value you would convert into is
going to be logical so for example you wouldn't put like a letter
or something inside of a Boolean necessarily it wouldn't make much sense so to
do this what we're going to do is we're going to use a function that is
specific to each of the different data types which is going to be called parse so
what we want to do here is we want to convert a this is going to be a string and we
want to convert it into a Boolean so we could just throw this in here as a simple
string let's get rid of that guy that slipped in there so let's have this
be true instead like that and let's go and do this with a couple other
different data types so let's go like this and this is going to be an integer
and we're going to convert from strings into integers so this is going to be
int from string equal to and we want it to be turned in into an integer so we put
that first and then we go parse and then we list what it the string version of it
and you could also store a string inside of a variable and then just put the
variable inside of here that would also work let's also go and create a double
which is going to be a double value that we get from a string input so again you
put double what you want it to turn into and parse and then you could list 1.234 or
whatever you would like okay so that is how we can convert from strings to
different data types now let's say you want to convert a double into a string
so let's create a string and we'll go to string value is equal to and
I'm going to say double from string dot two string and that is all you need to
do and that is going to allow you to convert from any the the function to string is
going to allow you to convert from any double into any different string that you
would like uh let's come in and let's say we want to verify this by getting
the data type that this has so we'll say something like whoops didn't mean
to do that um let's go console like this and we'll say right line again
that adds a new line and I'm going to use my dollar sign so I can just go and
place the actual data directly inside of here so I'm going to say data type and
if I get the data type of a variable I go string value for example and get type
like this and there that is and that is going to get me that I just have to make
sure I have my closing quotes at the end of it and uh let's do a couple more
let's say that I want to cast a double into an integer this is called explicit
conversion and it's considered explicit conversion whenever you are going to be
losing some data all right so if I convert a double into an integer that means
I'm losing all of the decimal values that are stored inside of the double so
let's create a double here so I'll say double I can type it and we'll
say double number is equal to 12.345 and then I'm going to say console and
right line so there that is and let's throw this here and then I'm going to
say integer like this and I'm going to get the enter integer representation of
that double value now if you want to convert to any different data type all you do
is put the data type you want to convert into between parentheses before the value
you're converting into an integer so this is a double we're converting it
into an integer all right then this is called explicit conversion we are losing
data whenever we complete this conversion there's another version called
implicit conversion and that occurs anytime you convert from one data type to
another in which the smaller size type is being converted into a larger type so no
data is being lost in that circumstance so I'm going to say int and num is
equal to 10 and then if I wanted to go and create a long which is bigger than an
integer I can go long num like this is equal to int num like that and in that
circumstance you are not going to lose any data and if we come in here and zoom in
you can see whenever we checked for the data type it says it's a string and you
can see that integer is printed out with 12 we lost the three four five which were
part of the decimal inside of our double all right so there is a lot of information
on conversion and what I'd like to talk about now is how we can format our
output all right so we're going to come in here and we're going to be using
console again and right line again and let's say that I would want to format
this as if a value as if it is currency so I'm going to mark this as currency
like that all I can do is between the curly brackets I can simply say 0 Colon c and
it will automatically format this value 2 3.455 as if it is currency let's go
and just copy this because we're going to do this a bunch of different times
show you other different ways to modify this in your output let's say also that
we would want to pad with zeros so let's call this pad with zeros I can go and
say that I want to have at least four spaces inside here so I'm going to go D4
and I'm going to leave this be just 23 in this circumstance I'm going to
show you what it looks like whenever I output everything let's say that I only
want to allow for three decimal places even though there's going to be more
provided so I'm going to say three decimals I can just zoom in here and say F
like this and follow that up with three and then we can go and there's only
three decimals here now so I'm going to go and put another couple inside of
there and also another way we can do this is let's say we wanted to
automatically have commas added inside of it we could come in here and go n and
four and let's go and change this into 2300 or something like that okay
let's run it and then if we come in here and look at it you're going to see
that yes indeed this was converted into currency then on top of that it was padded
with zeros we said that we wanted to have four it's so it filled in the rest
was zeros I said that I wanted it to be converted three decimal places and it
automatically rounded it and then it also added in all of my different commas and
as the tutorial continues I'm going to do more with uh generating random
specific types of output as needed okay so now we're going to talk about
strings I'm going to show you a ton of string functions and like I said before
strings just store a series of characters let's just create a string let's
call this random string and then we're going to say this is a string right like
that and there you are you created a string now what can we do with them well we
could get the number of characters in our string so we could do something like
console right line and let's go and change this well let's just leave that
inside of there and I'm going to go string length and then I'll go and get
this value out of here and I'm going to put this on through the next line and
then I'll just go random string and then follow that up with length exactly
like that let's go and print out a couple more just to get a couple things on
the screen at the same time we're also going to be able to easily check if a
string contains another string so let's just throw this here and we'll say
string contains is so we'll check if it contains is and then it's going to
return either true or false so we're going to get rid of that and we will say
contains and then we will check if that exists and whenever we get into conditional
statements and things like that you'll be able to also check out that oh I
forgot to put a closing parenthesis all right so what else would we like to do
let's say we would like to check or get the index so maybe we went and checked
does is exist in this string and if it does I would like to get the index where is
is located so we can come in here and we could say index of is exactly like that
and closing parentheses again let's go into some more let's say we would
like to remove the number of characters starting at a specific index well let's
say remove string whoops remove string and we just come down here and instead of
length we're going to say remove and we're going to say we want to remove
from index 10 through 6. and let's do it all another one let's say we would
like to add a string starting at a specific index how can we do that well we can go
I'm just printing these out so that we get the output which is the actual
string after we make our changes so I'm going to go insert string and then
hopefully you'll be able to see how the string is changing as we continue so
we'll say insert add index 10 the word short short like this and closing
parentheses and let's say that we also would like to replace a string so
let's replace a string inside of a string so let's go put this inside of
here and we're going to say this time replace string and like this and then we
will show our output directly inside of here again and on the next line I'm
going to go random string dot replace and I want to replace the word string with
the word sentence sentence like that and close in parentheses and let's go and
do a couple more uh let's try let's paste this inside of here let's say
that I would like to compare strings and on top of that I'm going to show you
how to ignore case and to do this let's go whoops there's string length
already have it so what I want to do is I want to compare strings they're
actually characters but they're strings in this situation B are A to B all
right so we have our output and then after this I'm going to say string I have
to get rid of this part I have to say string dot compare and a two B and then if I
just did that part it would um not ignore case but I want it to ignore case in this
situation and if I wanted to ignore case I go string comparison
is that showing up no it is not comparison and ordinal ignore case there we are
and I throw in my parentheses and I think that's a good amount of information
so let's just go and run this and if we run it you're going to see here is
our output and string length is 16 does the string contain the word is yes and that
comes back to true of course index of is starts at the index two if I wanted to
come in and remove a string you can see now that it just say this is a instead of
saying this is a string and then I inserted short string inside of there and then I
replaced the inside of our string the word string with the word sentence and then I
did a comparison between a and b and the way that works is whenever you compare
strings it's going to compare the very first letter inside of the string or
number unless it is the same then it will continue onwards maybe it makes more
sense to actually just show you what you'll get so basically if you do this
comparison you are going to see now you saw that it had a value of 1 when we did
this string comparison that just means they're one character apart so basically
if you get a value that's less than zero returned that means that string one
proceeds string two if you get a zero returned then that means that they are equal
and if you get a value that is greater than zero that means that string two
proceeds string one so that's what those individual values mean let's
continue and when I do um a console whoops didn't mean to do that let's do
a console right line and then let's just throw in some dashes just to break up
the information that is being displayed on the screen and then we will continue so
let's just move this up here there we are I can start right at the top okay so
let's do another one so I'll do console right that is perfectly fine and
what I want to do this time is I want to check if strings are equal but I'm
going to show you that also how the ignore case works I'm going to compare an
uppercase a to a lowercase a and show that they work so I'm going to say a is
equal to a in this situation it will so let's throw that there and then we are
just going to say string and equals like that and we'll compare the uppercase a
to the lowercase a and then we'll do a string comparison again and we will
ignore case so let's just go and grab this and copy and paste that inside of
there and then throw in another parentheses and a semicolon so maybe let's move
this down the line so we can see it our new content a little bit better let's
say that I would like to add padding to a string whenever it is output well let me
do console and we'll do a right line again and there's red line and what I
want to do with this specifically is I'm going to say let's just type in
something like pad left and then throw that inside of here to get our output and
then on the next line I can do random string and Pad left and let's do 20 and
then we'll say that we want there to be periods inside of there let's also
do a pad right this is going to be very similar so let's throw this inside of
here paste that there and this is going to be pad right instead and for pad right
let's uh just change this to pad right H okay what else would I like to try
let's say that we want to trim white space so that means that we want to get
rid of any excess white space at the beginning or the end so will go trim like this
and then I'm going to have random string and then we just simply call trim on
this so I'll say trim like that and also let's say that we would like to
make our string all uppercase we'll grow this inside of here upper case and
then to get that you just go random string and then you're going to say two
upper so I'll say two upper but and then we have an Excel or we need an extra
parenthesis we're going to do almost identically the same thing of course if we
want it converted into lowercase so just change this to lowercase and then this is
going to be 2 lower like that and what else um let's come in and let's
actually go and use format to create our strings this time so what I'm going to
do is I'm going to say string and I'll call this new string equal to and
you call string dot format and I don't think I've used multiple variables
before inside of here so let's go and do that so I'll say something saw a
and so I'll see how I have it is one now and we could this is like a Mad Lib
type of thing where you just insert random words in the and throw three inside of
there all right and then down here we can jump down and we can say Paul and rabbit
and eating and feels if you don't know what mad libs are they are it's kind
of a book and it basically has oops I thought that would work all right let's
get rid of that all together and just type it again it's a book that just has
empty spaces and then you fill in the spaces with random words it's like a
children's book kind of I guess so that's what those are and uh you can
also add new lines to your strings so let's just let's say instead of doing
our normal console right line we could come in and do right you know you don't
there's no nothing shows and or there is no new line added with right of course
and but you could go and add one because sometimes you might manually need to do
this that's perfectly fine and another thing to know is there are things called
Escape characters so for example if you wanted to use a double quote inside of your
string you couldn't do that because the compiler wouldn't know which double
quote was the end of the string and so forth so we have these things called Escape
characters and let's go and do a comment so an escape character would be
something like this and a backslash with a quote two backslashes if you'd like
that you also have tabs okay there's a whole bunch of them so in some
circumstances what you would like to do is use the string verbatim that means that
the Escape characters are completely ignored if you ever find yourself in this
situation in which you would want to do that all you do is put at like this and
then you could say exactly what I typed like that and then you could throw a new
line in here it's just going to print as a new it's not going to print a
new line it's going to actually print backslash and N now of course if
you're having double quotes and stuff that can cause some problems but in
general that's pretty good all right so let's come in and let's look at
our new content and you can see that I was able to pad left by putting periods in
there we also padded on the right and I said I wanted a total of 20 spaces taken up
with what is output on the screen and I trimmed the string I put it in uppercase
lowercase you could see the little mad lib thing where I inserted all the different
words and then you can also see that the backsplash the backslash n showed up
whenever I said that I wanted to use a Verbatim string okay so that is a lot about
strings and we will cover more about strings as the tutorial continues but now I
want to talk about arrays foreign now as I've said before an array is just
basically boxes inside of a bigger box and then they're going to contain
multiple different values all of the same data type and each item is accessed using
an index or a key and the very first index is zero and then they increment up from
there so let's say I'd like to Define an array which is going to hold
it's going to be an integer array and and it's going to hold three values
arrays have fixed sizes very important to know that okay so let's say I do
something like favorite numbers and I would say new and int and then I would say
three okay so that's how we go and create an integer array that has three
spaces inside of it if you would like to add a value to it you could do something
like favorite numbers and zero and we will say equal to 23. all right now we can
also come in here let's say I wanted to go and get that value so I could do
console and let's just do right line and I'll say favorite num at index 0
and go and get that value out of there do so and I just say favorite Noms like that
and then I go and throw in that index that I'm looking for let's continue
what's sad like to create another uh Ray and it's going to contain customer
names customer first names so I can just go string like this I don't have to
Define how many spaces because I'm going to allocate all the spaces whenever
the array is created so I'll say something like Bob and Sally and also Sue okay
so that is how we create those using the curly brackets you can also use VAR to
create arrays but the values again must be of the same type so let's say I want
to say something like employees whoops employees like that is equal to and I could
say new and then I could throw in some random names so I can say mic and Paul give
Paul an uppercase p and let's also say Rick okay so that is my list of
employees you can also create an array of Base objects which is the base type of
all the other different types how you do that is you say object like that and then
I can say random array so this is a way around inserting values that are not
necessarily of the same data type one two three four okay and there we are um if
you wanted to get the type you could come in and we could say console and let's
use right line again I can say random array zero and let's go and get this
value out of here and what I'll do is I'll say random array and the zero
index and call get type like that and there we go and uh let's say I'd like
to get the number of items inside of an array
did I why don't I go and just copy this so I have to keep even though tabbing
in isn't that big of a deal all right so let's throw that there and I'm
going to call this array size so array size and to get our array size there is a
lot of consistency inside of C sharp random array see if you can guess what it is
its length and there it is okay um you can also use a for Loop which I haven't
covered yet so I'm just going to cover it right now and what we're going to
do is cycle through our our arrays and that's all it does it's basically
going to generate a index value you saw that we were able to access them using our
index values and we have to Define that it is an integer and I'm going to say
that we're going to start at index 0 then you put a semicolon then you're
going to have a condition that is going to Define how long you continue cycling
well we're going to say we're going to continue cycling as long as J is
less than the random array length now if we have three items inside of it
they're going to take up 0 1 and 2. if we asked for the length it's going
to be 3. so if we wanted to get the index values from zero and get the index value
0 1 and 2 well in that circumstance it's going to work so all we need to do is
go random array and then in length look at that and see and uh Visual Studio is so
intelligent it automatically knows what I want to do before I do it okay so
let's paste in our console right and I'm going to say array and I'll
throw in zero whoops I already have it in there what am I doing and so this is
array and then uh after this let's say I want to get the value so value like
this and that I'm going to label as one then I'm going to move this down to
the next line and I'm just going to Output I'm going to leave now let's
change that let's go let's just get rid of the whole thing let's go and
J that's going to give us our index value and then I also want to get the value
that is stored at that specific index and I think that's enough information for
us to go and run this all right and if we run it you're going to see uh that I
had 23 stored in the index for our favorite number array that you could see I was
also able to get a string back and the array size is 3 and then we cycled through
the array we created and we output both the index as well as the value stored
inside of it so good stuff what else would I like to do here well we have things
called multi-dimensional arrays and let's come here and let's do another
console and right line and do a couple dashes inside of here just to show that this
is new output so what we're going to do is I'm going to basically create a
multi-dimensional array and whenever you define an array you you're basically
saying you want to create boxes stacked vertically okay not horizontally vertically
in a column now if you would Define a multi-dimensional what it's going to do
is it's going to also create rows so let's go and create one so I'm
going to call this string like this and I'm going to call this customer names
is equal to new and this is these are going to be strings and I'm going to say
I want this to be two rows or two no yeah two rows by two columns and then after
this I'm going to start putting them inside of here so I'm going to say Bob
and also I am going to say Smith okay so I got those inside of there I'm going
to go down to the next line that comes after this and let's tab that in and
let's also say Sally Smith so I'm Sally a and Smith oh Smith like that and
then we have our closing brackets and there they are okay so that's going to
create a multi-dimensional array now if you want to get a value inside of the
multi-dimensional array you just use indexes again just like we did before so
let's say we wanted to go console and right line and I'm going to say I
want to get the multi-dimensional value and get this here for that and grab this
and then we're going to get it by saying customer names and Dot get value did
it get me that yes get value and I'm going to say index one and one and then
closing parentheses and there it is let's go and run that so run it and
we're going to be able to see here that we were able to get Smith well
that's that's not very useful because there's two Smiths inside of
there let's go and change that so let's say 0 1 okay let's get our our
different version inside of there all right and uh in this situation it gave me
smath what is going on here did I spell something wrong okay let's get rid of
this and uh exchange this to one and change this to zero okay so there we are and
let's run it again and here we can see that we're able to get Sally as our
output okay so why is that exactly that we got Sally well quite simply what this is
saying is it wants you to get row 0 or Row one I mean and that remember this is the
zero row so this would be Sally right here and then I want to get column zero and
this works out as column zero okay so that's why we get those now let's say
that we'd like to come in here and cycle through our multi-dimensional array
well we're going to do pretty much the same thing we're going to say 4 int
J is equal to zero and then while J is less than customer names and again I'm
going to get my length all that information all good and let's come down here
and then what we're going to do is we're going to do basically the same
exact thing but we're going to have another index we have two indexes so we
have to Generate random indexes so we're going to just change this to the
letter K this to the letter K and this to the letter K now that we did that
we're going to be able to come in here and cycle through all of those so
let's do right line and then let's go and print those out individually so
I'm going to say 0 like this and then I'm going to go and get the customer
names it's just going to print out first names and last names but it's
going to do that separately so I'll say J and K and there we are and then at
the end of this maybe I want to do like a console and uh right line so I'll do
right line like this just to give me another empty space now if you would create an
array that is going to be like let's say you have an array that is two rows
like that and two columns and then three with like a third dimension you could
think of this like a stack of three spreadsheets with two rows and two columns
worth of data on each of the different pages if that helps so let's go and
let's create that so I'm going to create an integer array here and I'm
going to call it random numbers so Rand nums and let's go and give them a whole
bunch of values so I'm going to say one four nine and two throw that there then
I can actually pass an array to a function and you've heard a lot about
functions but you've never actually created one let's come up here to the
top of our application and where I want to do or where I want to put this function
I'm going to put all my functions right up here and I'm going to actually
Define that this is my function area so let's just call this functions and then
I will put another one inside of here another comment that is going to say end of
functions like that and then we'll throw in some new dashes okay so basically a
function is just going to have a name let's go and create one so you already
saw some of this stuff I talked about static and void and all that stuff previously
but basically I'm going to say this is also static what does static mean I
don't know if you remember but static means that we do not need to create a new
object for our class program to be able to call this function okay that's all
that means this is also not going to return a value we're going to be doing a
lot with functions here very soon the name of the function is going to be called
print array and then you're going to have the things that are passed to it well
I said I want to pass an array to it so you just say if it's whatever the data
type is for the array inside of there then we're going to say that we want the
name these are called parameters that are passed to a function and we're going
to say that inside of our function we are going to call this array that was passed
inside of here into array and then let I wanted to have type of message that I
output okay and then in all of your functions that you create are going to have
curly brackets and the function definition is going to be that which lies between
the curly brackets so what I'm going to do here is another way to cycle through
an array we talked about four well you can also use another handy dandy tool called
for each and with it you can just say int K in and array and it automatically
figures out um the length of your array and only Cycles as long as there is more
content in the array so we're going to go in here and say console right line
and then inside of here I'm going to get rid of this and I will output these
values so this and then I will output whatever message it was that is passed to the
function I will say something like um message and then whatever we want to Output
inside of there just something random to do inside of our code all right so then we
have all of this all set up and I can just copy print array and just remember I
want to pass a integer array inside of here as well as a message I'm going to
come down inside of here and then inside of the function I have to pass an array I
have Rand whoops Rand numbers and the message I'm going to pass in front of it
is going to be for each like that
um let's go and actually run this because I've had a couple different
things go on here all right so let's go in and you can see that we printed out
the customers Bob Smith and Sally Smith and then you can also see I was able to
cycle through all of those array values using for each just like that all right so
let's do some more uh let's go and get another one of these consoles with a
lot a bunch of dashes inside of it and throw it right here just so we can see where
our new output is coming from you can also sort arrays quite easily so we can do
something like array dot sort and we'll do random nums like this and we can
also reverse a array this isn't gonna put them in Reverse uh sorting order
it's just going to reverse them so I'll do random num like that and
let's throw that in there you can also get the index of a match inside of an
array so let's do something like console and right line and I'm going to
say one at index and let's go get this value returned and I'm going to say
array dot index of and pass in random numbers and one and parentheses there
let's say I'd like to change the value at an index so let's go random
or random Noms there we are and let's do set value zero so one there we are and
what else would you like to do let's say we'd like to copy part of an array
to another array we could do let's create another integer array and let's
call this Source array and equal to one two and three there we are whoops hit the
wrong thing there are curly brackets around that and then we'll create a
destination array so I got that and destination array let's make this d s t all
right and to get it we're gonna go like this and I'm gonna say new integer
and two and I'm going to say that I want to start at a specific index so
I'll have this be zero and then I'll have another one and it's going to
be length is going to be equal to 2 then what I can do is I can copy values
starting at index 0 and up to a total length of two spaces let's move this up
here a little bit just by going array and another array is equal to array and
create instance good got it and this is going to be type of and it's going to
be int and then I'm going to put 10 inside of here now we can take all of that
information and let's go and actually copy it so we'll say array dot copy
like that and Source array starting index destination array and we're going to
start at the zero index and the length of that item and let's go and print our
array just to see all of the changes that we have so destination array and
let's also print copy inside of there just as a message um what else would do
we like to do um let's say we want to there's another way we can create
array and that is through the use of create instance so we just say this is an
array another array let's call that and is equal to array dot create instance
and we have to tell it what type it is so I'll say type of int and then the
length is actually going to be 10 so let's just change that to 10 and let's
say also that we'd like to copy values in our source array to a destination
array well we can do that also so we'll say Source array and copy to another
array and five and then let's cycle through all those using four each again so
I'm going to say for each and this is an integer let's use m in another
array and then after that we will just output all of these to the screen so I'm
going to say console right line and I'll say copy two just so it's a little
bit easier to see where this information is coming from and we will throw M inside
of there and of course close that off there's so many other things you can do
with arrays uh Let's do let's let's run this it's starting to get a
little busy okay so you can see how all of these are going to work so one at index
you see we get a value of three you see we were able to copy those you saw that we
placed values starting in index five inside of there and also that there are zero
values in all of the other individual places whenever you create a array it
automatically has default values of zero which is also important to know all right
good and uh let's try one more example let's say we would like to search
for an element that matches a specific condition that we have to find so let's
create another integer array and let's have this be number array is equal to
and and we'll get into like Lambda functions later games right now that allow
you to do all kinds of string manipulations but let's say console and let's
do right line and let's say that we would like to get values that are greater
than 10. okay grab that and then it's going to insert those array values
we'll say array and find and you pass it whatever you're searching for
number array like this and then you have to put your condition inside of here which
is just simply greater than 10 like that pretty cool stuff alright so that is a lot
of information on arrays and I'll be covering of course more with raise as the
tutorial continues but now I want to talk about conditionals and specifically if
else and else if all right so whenever we are using conditional statements
there's two important ideas you have to understand there are relational
operators this of course is greater than less than greater than or equal to less
than or equal to equal to and not equal to now in some circumstances you want to
stack or you want to perform multiple different relational conditionals and
whenever you want to do that you use and and you use or and you use not whenever
you want to convert trues to falces and false is the truth that's why we have
those all right so let's go through an example basically what I'm going to
be provided with in my program is an age and let's have it be 17 and what I
want to do is I'm going to say if age is greater than or equal to five and so
I'm going to have multiple conditions inside here so I'm going to say and
the age is less than or equal to seven well in that situation I'm going to tell
the student to go to a specific type of school so it's going to Output either
elementary middle school high school college or it's going to say retire or
something like that all right so we're going to say right line and we're
going to say go to elementary school in this circumstance and let's throw in
there okay so it's going to do this and it is going to Output that if those
conditions are indeed true now we also want to do if the age is greater than 7 then
we want to do something different age is less than so we're getting rid of the
equal sign in this situation and we're going to change this to 13 exactly like
that well in this situation the student should go to middle school so we'll say
middle school and we're going to have another condition and this condition is
going to be if their age is greater than 13 or less than 19. like that in that
situation they're going to go to high school so high school and then if as a
default what we can do is we could say else and this is going to handle every other
condition that we could have age-wise anyway we could say go to college in that
circumstance and let's close this off and close that off all right and there is
that situation and we could also come in here why don't I do another just so I
can demonstrate or so if we have something like if this has nothing to do with this
string of conditionals here one of these is going to be picked well what I want to
do is just demonstrate or so we can say something like if age is less than 14 or if
age is greater than say 67 we could say something like you shouldn't work okay
so this has nothing to do with school it's just like something random so
we'll say laws will not allow you to work or something I don't know so it
says to God's all obviously gonna work if you're over the age of 67 I'm
just just throwing some ideas inside of here so here we are and we could say you
shouldn't work go retire and or if you're under 14 whatever okay I
don't know how I accidentally slipped into the world of uh trying to explain
when somebody could work but we did okay so let's do another right line here
before we get more confused and I'll also demonstrate not so let's go
something like not true it's just going to turn two rows and falses and false
is into churros that's what it does so we can do this oh and another thing you
can do is you can go and add you can put a plus sign inside of there if you so wish
to get um combined strings here for output also so there's something else
we're able to do all right good stuff and if we go and we run this you can see
that we get a result over here that says go to high school and it also turned a
true into a false and the reason why we didn't get any output for the other one
is because the age is 17 right here and that means that person should work another
conditional you're going to likely come in contact with is what is called a
ternary operator and it's best to see them in action to understand them so
let's say you would want to assign one value depending upon if a condition is
true or another value so you're going to list the value is going to be stored
in can drive so we're going to say if the age is greater than or equal to 16
and then you're going to put a question mark if that comes if this condition
comes back as true it is going to assign the word true to can drive and if it comes
back as false it's going to store false inside of there so that is a ternary
operator and another way of doing pretty much exactly the
same thing that we just did with if and else if and all that is with switch and
switch is used whenever you have a very limited number of options and uh so
let's go and show you another example so we'll say switch this and
we'll throw age inside of here and then after that let's just move this up
here we're going to say case one and case two whoops case two like that and
this is going to match both if their age if their age is one or two it's going
to Output this specific piece of information the specific string we can say go to
daycare something like that both of those are going to work and then if you want to
break out of the switch statement and never check anything else afterwards you put
break and there that is let's do another set of conditionals this is going to
be almost exactly the same so let's just copy this and let's just come
right down here oops back there throw that there now this is going to match four
three and four so we'll throw those inside of there and we'll say something
like go to preschool free school like that and again the break statement does the
same thing it did before let's throw in another situation we'll say case
five well in this situation we're going to say that we want our student to go
to kindergarten so we'll say go to kindergarten like that and then we could
come down here and just have a default value and this is going to be if nothing
else comes back as true and if that happens we are going to say go to another
school go to another school we're fed up and we just want this to have some
outputs so we'll say go to another school and another thing you can do is you
can also jump out of a switch statement with go to and it will jump to wherever
this is bad practice don't do this but I'm just showing it to you just in
case you ever see go to somewhere you could have other school like that and then
what you could do down here is say other school and it's going to jump to this
keyword down here and in this situation we'll go console and right line and
we'll go elementary middle have them figure it out high school that's good
enough okay so high school and uh good stuff so there it is all right and that is
how go to works also and let's go and run this oh boom and oops no I don't
want to run it where do I have a bug and I don't know let's see here where
is it at oh I see I forgot to put break inside here whoops break there it is and
now it'll go away and now we can run it and we can see our output here is
it's going to another skull and it lists elementary middle and high school okay
stuff another thing we can do is you can compare strings using equals another thing
that is useful so before we fall out of this let's just come down here and
let's go and create two strings so I'm going to call one of the strings
name two and I'll have that be Derek and then I will have another string and
it's going to be called name three and it's also going to be dark so throw
that inside of there okay so we have those two we can say something like if and
name two and equals this name three and I'm going to do an ordinal comparison
with these string and comparison there it is like that and ordinal all right and I
have that covered yes I do so in that situation I can say the names are equal
console Dot rightline and names are equal and it's going to come back as true
indeed if we run it and names are equal of course does show up inside of there see
names are equal all right I forgot to show you that before and I'm showing it
to you now all right so good stuff and now we're going to get I know we covered
looping but I'm going to get more into giving you a couple more examples to
completely understand it and we're going to start with a while loop okay so now
let's talk about the while loop um basically you use a while loop when you want
to execute as long as a condition is true and what I want to do is I want to create
a while loop that is going to cycle through numbers and it's only going to
print out odd numbers now with while Loops your index like we had with the for Loop
is actually going to be outside of your looping structure then you're going to
have your or condition and I'm going to say wow I is less than or equal to 10.
so I want to print those values out then what I'm going to do is I'm going
to say if and I modulus 2 is equal to 0 well in that situation we know we have an
even value so I do not want to um I don't want to print that out so what
I'm going to do instead is just increment that value and then what I want to do
is I want to jump back up here where the while loop starts and continue
incrementing now of course with I having in having an increased value how you do
that is if you want to not continue executing what is in the while loop you just
say continue exactly like that all right so if this condition is not reached then
we do want to print but let's say on top of that we do if we ever get a to a
value where I is equal to 9 we want to jump out of the loop and not check anything
further all right we can do that also we can say if I equal to 9 well I want to
break and stop executing the while loop all together so I'm going to jump down
here and the while loop is no longer going to execute but however I said I wanted
to print my odd numbers so I'm going to say console if I can spell console
correct and right line and then I'm just going to Output that value and then
I'm going to say that I want to increment the value of I because I want to get
to the even value and so forth and so on and proceed through what we were doing
here before all right so good stuff let's go and save that and if we run it
you're going to see that it does indeed print out those even values right here
okay so one three five and seven but it doesn't print out nine why because of
our break statement all right so good stuff and there is an example of while loop
which you would see more of as we continue now what I want to do is I want to talk
about a do while loop and you're going to use a do while loop whenever you must
execute the code at least one time so what I'm going to do here is I'm
going to Institute like a guessing game so I'm going to say random and new Rand
is equal to I want to generate a random value so I say new and random like this
whoops how'd that happen all right so there we are and what we're going to
do is we're going to create a secret number so secret number is equal to Rand
dot next and then you put inside of here the minimum value and the maximum value
you want to generate and then I'm also going to have the number that was
guessed by my user so I'll say number guest is equal to zero and then I'm
going to Output onto the screen console right line and I'm going to say random
num like this and I am going to say secret number like that and then close this off
with a parenthesis and there we go now with the do while loop part the guy that
we're here to talk about we're going to say do and the conditions actually
going to come down here after the loop has been executed at least one time so
we're going to say we want to continue looping as long as the secret number is
not equal to the number guessed right like that and then you have a semicolon here
at the end so inside of the do part we're going to ask the user for a number
and we're going to say enter a number between one and ten like that and that is
going to allow them to now receive a value all right so we're going to say no
number guest is going to get the value and we are going to convert this from a
string into a well the value they enter to in that they provide to us is always
going to be a string so we want to convert it into an integer something else I
should have mentioned up here it goes up to 11 but doesn't include 11. sorry if
I didn't say that I don't think I did okay so there we are it's amazing
how quickly it gets out all right so it's going to convert this string that it
reads from the user and converts it into an integer and it's going to continue
on then what we're going to do after the do while loop ends we're going to
say console and right line and we are going to say something to the effect of you
guest it uh you guessed it it was and whatever all right and then we'll go and
throw the value inside of there which is going to be our secret number we are and
there we go on top of this just to save time I'm also going to print out the
random number oh yeah I got that there okay so good stuff let's go and run this
now and you can see right up here we have our values so it says enter a number
between 1 and 10. I actually have to go outside of here to be able to provide this
value so I'm going to say five and that didn't work so you can see we have
a value of like it just didn't tell us anything so now what I got to do is just
continue entering values until I guess it whoops and oops I accidentally hit enter
which caused an error I hit it enter two times so let's run that again and
let's do it right all right well I'm just going to do this a long way so
two three four five there I got it and so our number was five and we could do other
cool things like we could say higher or lower and things like that I'll leave
that for your homework and there we go so there is a useful example of how we can
use a do while loop now what I want to do is I want to briefly talk about exception
handling to ease you into it and then I'll get more in exception handling later
in the tutorial basically we use exception handling to catch errors that could cash
crash our program so let's
say we're going to do a division by zero problem and we are going to handle it
so let's say we have a numerator of five and a denominator which is going to be
number two which is going to be equal to zero okay now what I want to do here is
you're going to have to surround all of the potential problems in your code
with with what is called a try block and what we're going to do here is I'm
going to say console and right line and I'm going to say 5 divided by 0 is
equal to this is going to throw an error and we are going to catch it so we'll
throw that right there and then on here and then I'm going to call a function
get more into functions here in a minute called do division like this and we will
pass a number one and a number two inside of it so let's come up here inside of
our function area and static again what's that mean it means we don't have
to create a specific object of type program to be able to use this function okay
then we'll say double X and we will say double and Y all right and what I'm
going to say here is if Y is equal to zero then what do I want to do in that
situation well I'm going to say that I want to throw an error and I'll say
new system like that and Dot and this is going to be a divide by zero exception and
close that off and throw a semicolon inside of there and otherwise that means there
is no error no potential for an error and we will say x divided by y there's
our function pretty simple stuff so now we will come back down inside of here and
we will catch this error down inside of our main function we always have to if you
have a try you have to have catch so I'm going to say catch and I'm going
to catch the divide by zero exception and ex and then I'll come down inside of
here and I could say something like you can't divide by zero so so let's
get this whoops let's come down here get that out of there and I'll say you
can't divide by zero like this and then if I want to get some additional
information on my exception I can say console and right line and let's say that
I want to get the message that's good like that so let's get that and
I'll put that and then on top of that let's also say I want to get the name
here so that's good and get rid of this part right here I'm going to say
get type like that and then I'll say dot name like that so we know this is
going to throw an error because you can't divide by zero all right and uh more
so let's come down here we can catch multiple different exceptions maybe
something else happens this is the default exception you shouldn't use this
unless you use it last and because this is going to catch every single potential
problem and we want to try to work with problems that we expect so that we can
easily solve them rather than completely crashing the whole program so we're
going to say right line and oh typed in console two times let's get this out of
here so there that is and I will say right line and I'll say something like
well I don't know anything because this catches every single possible problem
so I'll just say an error occurred like that and then let's go and get some
more information let's say that I want to get the same information so let's
grab this copy that and we'll paste this down inside of here and there we go
then after we have all of these exceptions caught if you ever always want to
provide some type of cleanup like for example let's say you had a database
Connection open and you want to close the database always no matter what happens
whether that that you know we have this exception handled or not well what
we're going to do is inside of our finally block this is always going to
execute and we are going to go and just see simply put out a message that says
cleaning up and we'll whatever cleanup is done will be done all right so there
you go an example of using exceptions and whoops there's an error I do not want
to build it okay so where's our error at do division what's wrong with this
um ah okay I see like this and throw that there wait a minute okay oh yeah
that's gonna work okay good let's run it and you can see here that we do
indeed have a problem it says you can't divide by zero it prints out the exact
exception name that triggered it and you can see here also that cleaning up shows
up there there you go that's a quick example of exceptions and I'm going to
cover them more later on in the tutorial but now I want to talk about string
builders okay so of course you wouldn't know this but every time you change a
string what you're actually doing is creating a brand new string and this is
very inefficient especially if you're working with large blocks of text and
string Builders however actually allow you to change the text directly in in memory
rather than making a new copy and how we create one as we say string Builder there
it is and I'm just going to call this SB is equal to new and whenever you
create a string Builder by default they have a default size of 16 characters but
what's good about it is they grow automatically as needed so we're going to
say string Builder and like this and let's just throw some text inside of here
so I'll say random text like that okay so we have that uppercase just so it
looks fancier okay um let's go now and let's say that I wanted to build a
string Builder that has a defined size well it's almost exactly the same as
what we have here so let's say we want to paste this inside of here and
let's just call this string Builder 2 new string Builder and let's throw
some different text in so we'll say something like more stuff that is very
important okay and let's say that we want our default size in this situation
over here whoops put that over there let's move this out here we don't even
use this let's just get rid of it all together all right now we got more screen
real estate so I'm going to say important and I'm going to give it a size
of 256 characters instead of the default 16. all right now we can go and get our
default size from this or get our let's say we want to get our maximum size so
I'm going to say that I want to go console and right line right like that and I
am just going to Simply come in here and change this guy right here and I'm
going to do something like capacity and then this and output that and to get that
capacity this is the default size for our string Builder we pass in or our maximum
size I mean so here is capacity so T there we are we can also come in here and get
the length of the text stored inside of it boom and let's see if you can guess
what that is called if you guessed length you are correct so let's go length
and throw length inside of there as well you can also add text to A String Builder
so let's come down here how you do that as you go let's go string Builder
two we can say append line and we'll get this and we'll throw in a new line
and more important text is going to be placed inside of there we have that you can
also Define culture specific formatting so let's go culture info do English us
is equal to and culture and info Dot create specific culture and we're going to
use English Us in that circumstance we can then append a formatted string let's
go and create a new string inside of here so we'll go string and best customer
is equal to and we'll say Bob Smith and then let's be 2 dot pens format and
we'll use English us and on top of that we'll say best customer and put old
Bob inside of here there we are and there he is he's our best customer and
close that off we can also output a string Builder so we're going to do console
right line same stuff as before and we're going to say sb2 dot to string
we're going to use two string a lot and close that off what else we like to do
let's say that we would like to replace a string we can say SB 2 dot replace
replace and we'll do let's say we want to replace text with characters
characters and what else let's do another console out and to string to show how
that changed let's copy this paste that down inside of there you can also clear
a string Builder so you do that by saying clear there we are and we can also then
go and do some more what's append another one just straight away by saying sb2
Dot pens and we'll do random text like that we can go and check if string
Builder objects are equal now and we'll say right line whoops right line and to
do that we go and get the string Builder name we want to compare follow that up
with equals and string Builder we want to compare to there's that what else do
we like to do let's say we would like to insert another string at a specific
index we could say SP2 dot insert at index 11 and what we want to put inside of
here is that's great and anything else yeah let's go and also print out our
string so we can see it again so right line we don't want that uh let's do
this one right here whoops let's grab this one right here copy that throw that
down inside of there and what else we'd like to do let's say we want to
remove a number of characters starting in an index with a length we give you sb2
dot remove 11 through 7. and then let's go and output our new string again so
let's go copy that and our new string Builder there we are so there's a
whole bunch of different things let's run it and we typed everything in
correctly and you can see here our capacity which is the maximum size 256 our
length was 33 that's the number of characters inside of it and then you can see
the multiple different changes we
made as we went and manipulated our string Builder so string Builders as you can
see are quite easy to work with and of course they will be also be covered a little
bit later in the tutorial but I've been beating around the bush here and doing
a little bit here and there with functions so now I want to cover functions all
right so as you have already seen as we've been coding along here functions are
basically going to allow us to avoid code duplication and it's going to provide
much better organization and later on the tutorial you're also going to see
that it goes also they are also going to help us simulate different systems and the
basic layout for a function is going to be like this you're going to have your
access specifier and access specifier you may say okay what is an access specifier
well let's go and look at that a little bit closer access specifier is going to
determine whether the function can be called from another class if it's marked
as public it can be accessed by another class if it's marked as private it
can't be accessed from another class and if it's marked as protected it can
be accessed by another class or it can't be accessed let's put can't
how do I do that wrong can't be accessed by class but it can be accessed by a
derived class and that means ugh don't worry about it whenever we get into
object oriented programming I'll cover protected in more detail but basically
that is all you need to know about it right now so that is what our access
specifier just think it's either public meaning it can be accessed from another
class or private which means it can't be can't be accessed from another
class all right so just know that then what do we have well I have the return type
you've already seen void that means we're not returning anything this could
be an integer let's say we do some type of addition or something that's the
return type then you're going to have your method or function name functions
and are referred to as methods and methods referred to as functions we'll get
more into that when we get into classes and such don't worry about it
parameters we've already talked about that these are values that are passed
inside of functions and then you have your curly brackets and the body of the
function inside of there let's go and create a function okay so here's
where we have all our functions right here and let's create a function that is
what you've seen before so we're going to have static I've already
talked about private talked about static void what's that mean it's not
returning anything whenever it's done executing and hello say hello all right
so this is something that we've already defined we've used this code
already previously in the tutorial so inside of this function we're going to
create a variable this variable exists only inside of this function and nowhere
else I'll show that through examples as we continue here we're going to
have it be named and we're going to say that it has no value all right so
what's this going to do it's going to say what is your name so what is your
name and like this and let's do right instead of right line so that it
doesn't skip to the next line what is your name good stuff all right so now
what I want to do is I want to save whatever they have entered inside of here so
I'll say console and read line good stuff and then I'm going to say console
like that and right line Let's Do Right line instead and I am simply going to
say to the user hello and whatever that user's name is that they just provided
to me name like that okay so there we have our function and how do we call it we
just say say hello exactly like this down inside of our main function and it
executes and wonderful let's run it I'm going to type in Derek here like
that and now you can see the input so it says what is your name and I type Dirk and
it says hello Derek okay very very simple type of function let's go and get a
little bit more complex now so let's create a function that adds values
let's just get rid of this we don't need this anymore and let's get rid
of this of course because the function doesn't exist all right so let's say
that we want to create a function here and it is going to be passed two values and
it is going to sum them say static again double and let's call it get some and
you have to define the type for the value passed inside you can provide a default
value if you do that and they do not provide a parameter value then it's going
to use this default value Double Y is equal to one so we're going to have both
of them be one and now what we can simply do is just come in here and say return
and X Plus y exactly like that and that's going to work out wonderful and then
we can come down here to our get some part and we can call for this to execute so
we're going to say that we want to create a double and it's going to be X
is equal to 5 and then another double and Y is going to be equal to 4 and then we
can go console like this and then we can go and call our function so let's get
this out of here and we will instead say that let's just call it X is going to
be well let's not do that let's do a little bit fancier we'll say 5
plus 4 is going to be equal to and whatever our value is like that and then we will
simply say get sum call our function pass in the values for X and Y exactly like
that and if we do it what is wrong here oh I need another parenthesis there error
went away let's run it and you can see here that yes indeed we got 5 plus 4 is
equal to 9. okay so that's how we can pass parameters in and also return values
on top of that something that's kind of interesting let's say that we went
up here and we see we have a value of x here and a value of y here and X and Y down
here I just want to demonstrate something you would never do this in a million
years but I just want to demonstrate actually changing these values so I'm
going to do something like double temp is equal to X okay so we have that and then
what I'm going to do is I'm going to say x is equal to Y like that and
I'm going to say Y is equal to 10. okay so in this circumstance I change the
value of x to the value of y all right now I have X and Y down here also so does
that mean that those values are going to be switched down inside of here well
let's go and find out so let's come down and just simply output this so
I'll say console and right line and we'll say hey what's the value for
x because we changed it in the function does that mean it has been changed outside
of the function does that make sense let's go X like this and like that and
save it and run it and we can see that say x is a value of five but in the function
we changed it to four what does it do here it stays the same whatever happens in
the function stays in the function and it is not going to be changed so what is
happening up here is X is actually not being passed inside of it only the value for
x which is 5 right here is being passed so this name X here is a completely
different X here and it's very important to understand that just because they
have the same names it does not mean that they are going to work in exactly the
same names another thing that's very interesting let's get rid of this is
you can actually have um a parameter passed and then have its value assigned inside
of the actual function that you're working with let's get that sounds
confusing let me show you an example so let's put this up here and then
we're going to create a function and what we're going to do is we're
going it's called an out parameter so we're going to say static you may
never use this but I like to cover everything so I'm going to call this double
it and it's going to be passed int and x and then I'm going to say out like
this this is actually used in database programming very often which is probably
where Microsoft got this idea so what this is going to do is it's going to have
solution be equal to X let's just go like this times whoops hit the wrong
button times two okay so that is defined right there and what's cool is
we're actually assigning the value of the solution here inside of our function
then what we can do down inside of main whenever we call this I can keep that on
the screen I can actually come in and I can go double it like this and 15 and out
solution like that and pretty neat and then what we'll be able to do is say
console and right line like that and I'll go and put quotes and I'll say
something like 15. 2 is equal to and throw that there and this here and then say
solution right like that so pretty oops I forgot to define solution outside of here
so let's go solution like that oops that's fine and let's run it and
you can see that it actually is able to work so we were able to double that value
that was passed inside of it so pretty neat stuff another thing that's cool is
you can we've always passed values but it's actually possible to pass by
reference so that you can actually have the change the value inside of the function
and have it change outside of the function as well so let's say we have
something like int number three is equal to 10 and int number four is equal to 20.
uh well we can go and create a function so first off what I want to do though is I
want to say console and right line and I'm going to do E4 first four Swap and
I'll say number one is equal to that there and number two is going to be
I'm going to Output both of these values and then change them
and you'll see how they change so I'll say number three and number four so
that is before any changes have been made now what I'm going to do is I'm
going to create a function called Swap and what it's going to do is it's
actually going to swap those values and the swap will actually the basically the
value for 20 is going to be assigned to 3 down here inside of main so how you pass
by reference I'm going to make this public static static void like this my
function is called swap how you pass by reference as you go rough like that and now
you have access to the value outside of your function I'm going to do the same
for both of them and and number two and let's say whoops let's make this
this will be three and this will be four in this situation and then I'm just
simply going to go and swap the values and this is an interesting concept to
understand especially later on whenever we started getting into algorithms and
things like that so I'm going to take number three I'm going to store it in
a temporary value then I'm going to get number three and assign it the value
for number four and then I'm going to store the value of temp back into number
four all right so neat stuff now what I'm going to be able to do is come down
inside of main I'm going to call that swap function so I have to go exactly
like this they're passing a reference to of number three and number four to the
swap function and then what I can do is get this guy copy this and you're going
to see how the values have been changed outside of the functions we'll call
this something like after swap Swap and you can see those stay exactly the same and
we can go and run this run it and indeed if we zoom in here you can see the value
before the swap 10 to 20 and after the swap they work out or they are maintaining
their change even outside of our function another thing is you can also use a
variable number of parameters passed to a function so let's say you don't
know how many different parameters are going to be passed that's perfectly fine
we are going to just come in here and be able to accept any number all right so
just understand that well let's just go and create it so this is going to be
it's going to return a double in this circumstance so I'll say double and
let's call we use get sum what's called get some more like that and we are
going to just pass in an array and it has to be called params like that and double
and then I'm going to call the name of this is actually going to be nums just
understand if you have any other parameters that are not going to be infinitely
long this part right here always has to come last all right that's all you need
to know get rid of that and we're going to say double sum is equal to zero and
then we're going to use our for each block to come in and say in I in Noms the
Noms part was passed inside of our function now we'll just sum all these values
that are passed inside of here like that and then we can come in and go return and
sum there it is good stuff so well now what we can do is we can come down here to
get some more let's copy get some more and no matter how many values we pass
inside of here it's going to work so we'll say get some more and we'll
say something like 1 comma two comma three and that will work well we go and print
that out I think that works a little bit better so we'll say console oops
console didn't work in that situation let's get this out of here console
and right line like that and we'll say one plus two plus three is equal to and
then we'll go and get our value that is going to be returned to us from our
function so let's go here and then let's go and get get some more so
I'll copy that and paste that right here let me got rid of that let's get
rid of it is it's gone and through that right there all right good stuff and
then we can go and run it it doesn't matter how many values we put inside of
there it's always going to work as you can see one two three is equal to six
all right all right and we can close that and keep on going another trick we have
here is we're actually able to pass in our parameters in any order if we use
what are called named parameters let's create another function and I'm
going to call this print info it's going to receive a name and it's going
to receive a zip code ZIP code like that all right now what I'm going to do is
I'm going to say console and right line and I'll say something like that
lives in the zip code zip code zip code two words or one word I don't know
it's two words in this circumstance there it is and then we'll come down
here and we will output both so we'll say name and zip code all right now what
we're going to do down here that's going to make this a little bit weird is
we're going to call these out of order I'm going to pass the ZIP code first
even though it's second and the name part second or name part I mean the name
part second all right so we'll go print info there it is and how we use name
parameters if we can say zip code like this and and one five one four seven
something like that and then we will say the name like this and we will say dark
anise as and run it and you can see that it's still going to work even though
they're out of order see there we are and I do not live at that zip code but
either way and uh so there's named parameters and how they work now another
thing that's really cool and let's get rid of this is that we are also
going to be able to use what's called method overloading and what that means is
that we are going to be able to create functions with exactly the same um the same
name but they can actually be past different types of parameters so let's
create data types you know you'll see okay so let's go and I'll have
this and I have a function called get sum2 and let's say that it receives a
double it has a default value of one and another Double Y is equal to whoops is
equal to give it a default value of one again and what it's simply going to do
is it's going to return X Plus y all right so very simple easy to understand
now let's say that we also want this function to be called and can receive
strings we can set it up that way so let's just change this to string and
let's change this quote that whoops one like that and change this to string and
now the same function name is going to be able to receive either strings or doubles
either one will work and then down here what we're going to have to do inside
of our function is to convert these into doubles so I'm going to say double and
I'm going to call this double X so get rid of this part so we'll say double
X is equal to and we'll say convert to double I'm going to use all these
great functions and then we'll also do the same for y so we'll go like this
paste this inside of here and we'll say Double Y is going to be equal to and
let's convert this from a string into a double and then what we'll be able
to do is quite simply just say double X Plus double why exactly like that and then
what we'll be able to do is inside of our main function we're going to be
able to pass these inside of it so we'll go right here and we'll say
console whoops console and right line and then we will say 5.0 plus 4.0 is going to
be equal to and whatever the output is for that and then we will call get sum two
and then we can say 5.0 and 0.5 so in this circumstance we're passing a double
inside of it Why didn't it print that there we are okay also however we're
going to be able to pass inside of it a string representation of both of these so
let's highlight these and whoops 5.0 and 4.5 and they are also going to work
4.5 exactly like that and if we run it you're going to see that yes indeed we
are going to be able to work with strings or doubles with our great function so our
our overloaded functions okay so there we are and good stuff and for right now that
is all we're going to do with functions much more is coming up however and up
next I'd like to talk about date times all right so now let's talk about
date times and time spans now I am going to create a date time using probably the
most important date in history say date time like this and then you define your
year and then your month and then your day exactly like that and then what
we'll do is let's say I wanted to get the day of the week I could do
something like whoops console [Music] and right line and we'll just say day of
the week and go and get this by simply coming in and saying awesome date dot day of
the week day of week and there we go oops forgot to put the parentheses in here
there we are and now that's fixed all right let's do a couple more things
before we output that um you're going to be able to change values so we could
do awesome date is equal to awesome dates add days look at that and let's
change that to four and then let's say awesome date and equal to awesome date
and we'll say add months where's months at there it is ADD months and
we'll change this to one and let's go and let's just copy this so
I'm going to copy that page to some side of here and I want to add years and
let's add just one whatever that's perfectly fine and then let's come
in here and copy this and output this and let's just get our new date so
I'll say new date like that and then go awesome date and then just call for
date to get that all right what else would like to do well let's talk about
time spans they can be used to define a time so we
could say time span and we could say something like lunch time is equal to new
time span there it is and let's go like this and then let's just change
this to 12 and let's change this to 30 and 0 and we don't need the
additional time on there after that and what else can we do well we can go and
change values also so we could say lunch time there's lunch time is equal to
lunch time and uh let's say we want to subtract and we're going to create a
new time span so we'll say new time span and we'll just go and just have
this be zero and change this to 15. so 15 like that and then let's come in and
output that I think I have yes I still have that so we'll say new time new time
output that and then we'll use lunchtime and convert this into a string so
we'll say lunchtime dot two string like that and throw in another parentheses
and I think everything looks good and let's go and run that and there it goes
so if we zoom in here you can see the day of the week on that specific day was
Saturday and you can see here all our first date and then also our time span and
how we were able to change it from 12 30 to 12 15. okay so a couple little things
to mess around with there and you should just go in and play around with it if
messing around with date times is important to you otherwise I'm going to
briefly talk about enumerated types and like I said we're going to go and uh
Define enumerated types is whenever you want to create a data type that is going to
be of a limited number of possible values and they are going to operate kind of
like with key value Pairs and basically why you would use them is they allow you to
use symbolic names to represent data and so what we're going to do is we're
going to let's come up here maybe right here after our function area and we are
going to Define some different colors that are going to be available for our
fictional car so we'll say ah yeah caps lock okay car color like this and that
is going to be the name of our new custom type and we are going to say that we that
it is represented with a byte and then we just do key value pairs so we'll have
orange and we could have it be zero or we could actually come in and give it a
value of one and what it'll do is automatically increment then for all of the
other different enumerated types that you have so you don't even have to put
them in there blue will automatically just be two and we're going to say that
we want green and also that we want red and um yellow there we are and then after
that let's put this onto a separate line and while we're up here why
don't we inside of our functions area actually create a function that works
with the numerator types so I'm going to say static what this is going to do is
it's going to Output the color that is represented with um what we pass into it
the enumerated type car color and it's also going to print out the code which
is going to be like the ones and twos and threes and fours so we're going to go
static print it out it's not going to return it and we'll say paint car and
it's going to get a car color passed inside of it let's just abbreviate
that as CC and then inside of here we're just going to do a console right line
this and then we'll say the car was painted and this is going to Output the
actual color and the code is going to come after that and let's come down here
and on to the next line and to go and get the color by default it just is whatever
the variable's name is and then to get the code associated with it you just
convert that into an integer okay so let's play around with these all right so
we'll go down inside of Main I'll go car color and let's just call this
car one is going to be and if we want to assign one of those enumerated types we go
car color dot and let's make it blue and then we can call our custom function
that we created paint car and there's paint car and we want to pass in our
collar and see exactly what comes back and if we do we could see what we wanted
which is the car was painted blue with the code two exactly as we see we're
going to do more with enumerated types I just wanted to briefly cover them before I
start jumping into more heavy information which is going to be classes and objects
which is going to come up right now all right so we are going to come in here and
we are going to create some custom classes and they are going to allow us to model
real world scenarios so I'm just going to right click over here and I'm
going to say that I want to command so I'm just going to come over here and
right click on the console app one and I'm going to come down here where it
says add and then specifically what I want to add is a custom class have C sharp
items selected right here and I'm just going to come in and name this animal
we're gonna model real world animals inside of this and then again as the
tutorial continues we'll start getting more specific on what we're working
with so what are we going to do here um this is going to be defined as class animal
there it is and what it's going to do is you have to think about when
you're trying to model Real World objects what are you how are you going to do
it and one of the things that we would like to do is to describe the actual
attributes of an animal so we could say things like the animal's name
that's an attribute that an animal would have and a sound that it would have
well inside of C sharp whenever we're talking about creating classes which are
blueprints for creating objects and those objects or actual animals so this is the
blueprint and then you're going to have an actual animal that's going to
have its own specific name and have its own specific sound it makes so we have
these attributes and they are called Fields inside of C sharp as they are in most
other programming languages or object-orienting languages so what we're going
to do is we're going to mark this as public and say this is a string and it is
a name okay so every animal we create is going to have a name we're also going
to create another string and it is going to be a custom sound that our animal is
going to have now whenever we create animals they are going to have to be assigned
default values or if none are provided otherwise we will go and receive names when
the animals are created at and sounds and assign them where we initialize
that's called initialization in uh inside of our class is in what is called a
Constructor and the Constructor has exactly the same name as the name of the class
and the object itself so we're going to say if they try to create an animal and
they don't pass along a name we're just going to assign them automatically
the name of No Name likewise no sound is provided so we will say that that animal
has no sound and likewise let's say that we would like to track the number of
animals that we have that are ever created well we are going to go and do that also
so we're going to say every time an animal is created we're going to go and
store that information but what I'm going to want to do here is I'm going
to want to store that inside of another variable and I'm going to make this
static what static means it means that if we want to access it they these actually
belong to the class so this number is going to be the same for every single animal
object that is ever created it is not a number that is specific to an animal object
like its name or the sound that it makes so this is something that I would like
every one of them to have the same number of animals so each object that is created
of type animal is going to have access to actually um get a hold of this this value
but this value is not going to be different for every single object okay so we have
our number of animals very important so let's get back to this guy and
let's go and create our animal oh we don't want it here we want this
outside of this all right so let's throw it down here all right so there it is
and let's go here and then what I'm going to do is just automatically
increment number of animals every time we need a new animal or we created a new
animal okay so there that is and there that is all right so that's the first
Constructor but what do we do if they provide us with um with a name but they
don't give us a specific sound well this is just another form of method
overloading which we saw previously and we can come in here as long as the
attributes are different in some way we can go and use the same method name however
we are going to have different attributes so what I'm going to do is I have
string here oh let's just tab that in there do this and we'll have name but
we're not going to get past a sound in this situation Let's Just Tab the
whole thing in all right good all right so we have this so let's go and assign
a default name so no name but if they provide us with a name we're going to be
able to work with it going to do is we're going to say this name is equal to
name whatever they passed in or it's going to be no name and they we know
they're not going to be able to pass us a sound because we do not have that as
a parameter in in our Constructor that we're using so what we're going to
say is this sound is equal to and we're going to change this to no sound so we
know we'll always have that and then of course we're going to increment the
number of animals so copy this paste that down inside of there and also if
you're referring to if you see the word this you know what that means what it
does is it actually refers to the object's name so that's just
some other piece of information it's useful and why don't we just use the
object's name that we created in our program well this is like the Chicken and
the Egg type of scenario in which this is the blueprint for creating animals it is
created first then we use the blueprint to actually create animal objects well that
animal object's name doesn't exist so it's very useful to use this to
be able to access it okay hopefully that makes sense some reason people get
confused about how to use or what this is it's just it's just like a way
it's just like saying if somebody you don't know their name you say he or
she or whatever you say all right let's do this now in this situation we are
going to receive both a name as well as a sound we're going to Define some
defaults directly inside of our parameter area so we're going to say the
default is no name and here the default is going to be no sound but we expect to be
able to get a value pass back to us then what we're going to do we have this
guy right here and we're going to I don't know why that went up there
let's go and get rid of this so there that is throw that inside here
there's our parentheses and let's just copy this out of here and paste it
inside of here and let's get rid of this all together that and here we will
assign either default or we will assign the value that they passed over in this
circumstance and we'll say this name is equal to the name passed in as a
parameter and then let's go and increment the number of animals just like we
did previously all right I think that is enough in regards to creating Constructors
now and what I'd like to do is talk about the capabilities now whenever we are
modeling the capabilities of an animal what we are working with are methods these
are special functions that exist inside of classes let's create one and
let's call it make sound and what it is going to do is whenever it's called
it is going to say whatever the animal's name is says whatever its sound is so
let's come in and we'll say console so it's definitely possible that it
says no name says no sound you know that's that's something that is
possible not convenient but possible uh so we'll say says and there we go and
now what we can simply say is name and sound and they will automatically go inside
of that method and another thing we would like to do is to be able to get the total
number of animal objects that are created now like we went up here and put this as
static we also would like this function to be static meaning we could get the value
of the total number of animals created without creating an animal object just to do
that would make so in that situation this also is going to be static it's going
to provide an integer I'm going to call this get num animals and it does not
receive any parameters it's just providing a value and what does it do it
Returns the number of animals exactly like that and that is all we're going to
do for this simple class at this point in time let's go and create another
class however that might be a little bit closer to a real world scenario however
let's create something that we're going to be calling shape math all right
so what this is going to do is it is going to work with what are called structs
which are quite similar to classes and in our main program here inside of program
CS we are going to actually create structs and I'll talk about them as we
continue there but you know what maybe we should jump over into Main and actually
go and start working with the animals right now I think that I know it's like
kind of like what do I want to do here I want to work with that let's go and
create shape math first all right so we're going to go over to console app and
write and we're going to come up here to add it doesn't really matter when
we do it and class and then we'll call this shape math so give it the name of
shape math all right good there it is okay so what are we going to do with our
shape math type here well uh this class is only going to contain static methods and
it's going to do things like get areas depending upon what type of shape is
passed inside of it so I'm going to say public static again stat anytime you
see static that means that we do not need to create a object to be able to use it
so it is going to return a double and let's say git whoops must know that I
like to use git at the beginning here and I'm going to have a string which is
going to be of type shape or is going to be called shape and it is not going to
have a name and then it is going to have double and length one default value of
zero and then it's also going to have double and length two which is going to
also have a default value of zero now what I want to do with this is I'm going
to perform different um array calculations depending upon whatever string was
passed inside of this so I can here let's get rid of this and we'll say if
the and we'll use equals from our string Library so we'll say equals
rectangle so we'll say rectangle like that and then we're going to have
we're going to compare it to shape so whatever that string is called and then
we'll do string comparison exactly like we have and uh or no ignore case I like
both of those so let's just use our default we have let's put this on a
separate line however because that's getting a little long well if it has that
then we want to return and we will return length one but we will have it be length
one times length two okay so that is if we have a rectangle just multiply the one
length times the other length and boom now you have your special rectangle area
function then what we're going to do is we're going to say if and let's
go and just copy this whole thing let's just copy it so we'll go like this
copy and paste it inside of here so we'll say if string and in this situation
we're going to say triangle so if they threw in a triangle for our actual
string name well in this situation we're going to compare these and if it comes
back as triangle then we're going to do length one times length two so get this
and divided by 2 and that's going to give us our triangle area and otherwise
let's say if they pass in a circle instead let's just come down here circle
like that is the shape well in this situation we want to go return and let's
just get rid of all of this it will say 3.14159 times the math function uh function
of power of length one to the power of 2 and that is going to provide us with our
area for our Circle and then what we'll do is otherwise if they didn't send
any of those we're just going to return negative 1 because they didn't
provide us the information that we were expecting okay so that is a way of creating
like a utility type of function instead or class instead of what we've been
used to all right so now let's go over into our program area and let's go
and actually use these guys very first thing I want to do is I'm going to
create a struct and what a struct is is it is basically a user defined type and
much like classes they have Fields as well as methods so I'm going to create a
struct and I'm going to call it rectangle and for homework you can go and
create the circle ones if you would like to try that out so we'll say struct
and rectangle it's going to have public double length and it's also going
to have public double a width inside of it you could go and create a Constructor
method as well so let's say public rectangle and double let's just make
this L is equal to give it a default value of one and then double width and give it
a default value of one also and then look at that it went and just whoops I went
and took away what it was giving me it gave me all that good stuff so I didn't
have to type it out and I was Dumb and accidentally got rid of it so let's go
like this and get it back again there we are so there it goes and it's
automatically going to assign those values for us now what I can do is come in here
and actually create this so let's go out of here and we are going to calculate
our area so we'll say area like this and this is not useful so we'll say
area like that and what this is going to do is return length times width length
times width exactly like that and now what I'll do is I will show you how we
can use or use this similar information to calculate the area in multiple different
ways no actually this needs to be placed outside of main inside the class but
outside of Maine about that error so like this paste it inside there okay so now we
can come in and start messing around with these different classes that we have very
first thing we're going to do is we're going to command and we're going
to create a rectangle using our struct so I'm going to say wreck tangle and
let's call it rectangle one then I want to add values so let's just say
rectangle one and Dot length and let's have this be equal to 200 and then
I'm going to say rect one I'm using the struct version right now I'm
trying to show you this in multiple different ways and to use it so that you can
decide on your own which you'd like or which you would prefer then I'm
going to say right line like this and I'm going to say area of rect 1 and we
can use our struct area function here so there it is like that and then we can just
go Direct One Dot area and create that so there that is and there this is now what
we can do is we can use a Constructor to actually create the rectangle also so
we're going to say rectangle create a second rectangle is
equal to new rectangle and let's have that be a hundred and forty just to try
something different if you want to assign one rectangle to another it's very
important to know that you are actually setting the values and not creating a new
reference so we're going to say rectangle two is equal to rectangle one and
then we can say rectangle one dot length is equal to 33. and we can go and actually
type this out so console right line and we can verify this so length like that and
let's go and get these and then we'll come down here and let's go and
get that second rectangle's length length like that and there we go and what
else would we like to do well why don't we just jump in here and start playing
around well let's run this okay so let's run it oh there's a build
error what's wrong what do we have here that's incorrect oh it's
actually giving me an error because I had this misplaced bracket inside of here
okay so you have that set and let's come in again and let's run it okay so
what we can see here is that we have the area of rectangle one is ten thousand and
rectangle two's length is set for 200. so let's go look at that and we can
see that that's correct 200 times 50 does equal ten thousand and whenever we
went and took the value in rectangle one and assigned it to rectangle two it was
originally 100 and now it's 200 so that was output as well all right so good
stuff and that's how we can work with structs now what I'd like to do is go
and actually create our animals so let's create an animal and let's call it
Fox and how you do that is you say new and we're not going going to assign any
names in this regard what we're going to do instead is come down here and say
name is equal to and let's say the foxes name is red and we'll say the
fox's sound is equal to raw okay there that is and we just created ourselves an
animal now what I'd like to do is come in and get the total number of animals
that have so far been created remember I said we do not need to create an animal
object to be able to get this information but we want something inside of there so
I'm going to say number of animals on like this and we will output it and
that's expected to give us a value of one because we just created one animal so
we'll say animal and a mole Dot and we're actually referring to the animal
class name whenever we are typing animal there and that is how you're able to
access values as well as static functions okay so that's expected to give us a
value of one because we've created one animal you can also create static
utility classes so let's come in and let's do console right line again and
I'm going to say area of rectangle and go and throw zero inside of here and
then on my next line I'm going to call shape map our special class we created
and we'll say get area and I can pass in the word rectangle because we know how
to get areas four rectangles and we could throw five and six inside of here oops
there we are and what else is there that's pretty good let's go and
actually run this so run it and we see that we have our original information which
is our areas for our struct rectangle we have number of animals is one as we
expected because we only ever created one fox and we also found the area for our
rectangle using our shape math class by passing 5 times 6 inside of there now we
can we're going to be doing much more complicated things with classes but
before I end this part of the tutorial in the net very very soon something that I
have not covered that I think is very important to cover before I drop out here
right now is what are called nullable types now data types by default cannot have a
value of null and this is sometimes a problem because often null is needed whenever
you're working with things like databases and such and you can actually come in
here and create a null type in a lesser used Way by just going int and a question
mark like that and so I'm just going to throw a random number in there just to
throw something in okay and we can assign it a value of null in that circumstance
and if we got rid of this it would cause say now it's red and it says
represents 30 dividend cannot convert null to an end because it is a non-nullable
value type if I put in this question mark inside of here that's going to allow
us to work with null and we can also come in and check for null so we could do
something like random number is equal to null and we could go and output something
so we'll say something like console right line and we'll go random number
is null and that's going to give us a positive in that situation and you can
also check for null in other ways so you could say something like if and random
number and has value and then what we can do is come down and also output the same
thing that it is null there's two different ways to check for an all types and
also a way to actually store null types inside of here so very useful let's run
it and you can see that in birth situations we do get random number is null okay so
something important that popped into my head that I thought would be useful to
cover because we're going to talk about it later and now I'm going to talk
about new classes and new objects all right so now what we're going to do is
we're going to take our animal class and we're going to make a lot of
changes to it so that I can cover public private protected constants read-only
fields and Setters and Getters and properties I think that's everything and uh
so let's just come in here and we're continuing with what we have and
we're going to make some changes here as we walk through here and I explain a
couple different ideas one thing is what we call Setters or mutators and what they
do is they protect our fields or our attributes and so for example if we have a bad
name pass so let's say we deem a animal name with a digit inside of it as being
a bad name we can go and reject that so what we're going to do is we're
going to say public void and I'm going to call this set name and it's going
to be passed a new name that that user wants to change for the animal what we want
to do here is we want to check if the name passed inside of it has any numbers
inside of it and how we do that is go character is digit and it will check every
character entered and if they enter something that has a number in it we will
reject it otherwise we can say this dot name is equal to the name they passed
inside of it which is perfectly fine and if it does have a digit inside of it
we're going to reject that by saying this and name is equal to and no name so
there it is and then after that maybe we uh give them some type of an error message
so we could say something like name can't contain numbers like that all right
all right so this is a valid Setter specifically for name what else would we like
to do well if we have a way for them to set the name we should also have a way for
them to get the name and these are called Getters or accessors and one of the main
reasons why you would use them is to provide custom output so let's say we have
height stored as a um in in meters or in uh or in inches or whatever see if you say
what's the height of this it could become confusing so what we could do with
the getter is actually provide that information so meters or or inches or something
like that in the output in this situation we're just going to say a return name
just to keep everything very simple another thing we'd like to do let's go
and do much the same with the sound so I'm going to say public and string sound
let's go like this and you can also Define your Getters and Setters through
what we call properties so you can see right here it automatically just did it for
me and that and that's going to return the sound and then with the setter part
we could do something a little bit more custom so we could say if and value length
is greater than 10 well we'll deem that that is not valid so we will say sound
is equal to no sound and I'm just throwing in just any reason to reject the
sound there's you know no other purpose inside of that so we'll say console
Dot and right line whoops meant to hit right line with a tab and then we'll say
something like sound is too long or something so sound is too long and there we
have that and then what we could do is we could come in and say else and sound is
equal to the value that we have assigned to it all right so that's how we can
set that up um let's come out of this area right here and you can have the
Getters and Setters generated for you also so let's say I wanted to assign each
animal an owner I could have public and it'll have a string return value and
then we'll have owner and then inside of here we could say get and set and then
we could have this be set to default of no owner for whoops own ER like that and so
it depends on if you want them to be set or you don't want them to be set um
where do I have number of animals I have that stored inside of here from the last
one you know static ant number of animals let's go in here and let's also
increment this using a method so I'm going to come in also and say public so
let's say that we want to go and store more than one no animal in number of
animals we want to increment by more than one that's something that could be
possible well in this situation we'll have everything handled with a method so
we're going to say number of animals like this and then we'll say get and
we will go return number of animals like that and then afterward and then
afterwards we could say
set and the number of animals is going to be incremented by whatever value is
passed or is assigned to the number of animals all right and number of animals why
is that not giving me a name type animal already contains a definition for a number
of animals oh okay well I'll just go and fix that so we're going to come up
inside of here and let's make this public and then I'm going to make some
changes to how we are accessing the name and sound because of up here I have them
all set as public well if I do that it sort of defeats the purpose of having these
be um have having them have Setters and Getters so what I want to do instead is to
mark them as private and then they can only be accessed through their Setters and
Getters there's no other way to get access to them so Mark both of those as
private one thing that's important to know however is if we mark them as
private that they are not going to be accessible by subclasses and what that means
is I can inherit every single thing the properties and the functions inside of the
animal class into a new class called dog and then go about making changes as I
might deem fit and if it's marked as private we wouldn't be able to access
that protected Fields however can only be accessed by methods in the class but they
can also be accessed by subclass and it's up to you whether subclasses mean
inherited classes and it's up to you whether you want those or not another
thing is I'd like to define a constant just to Define one and we can Define
constants in classes or outside of them so we can let's say that I want this to
be called shelter and this is going to be called Derrick's home for Animals now
I'm going to throw something inside of there and what else would we like to do
well I'd like to Define an identification number for each of my animals so
I'm going to come in and have this be public and then I'm going to Define
this as read only read only and read only fields that are set at runtime in
Constructors can't be changed so I think that's good we don't want our
ID numbers to be changed for our animals because that wouldn't make any sense
uh what else would we like to do here well um make sound am I going to have to make
any changes to it I don't think so um let's go up to our Constructors and
see if there's anything that makes more sense here for them I'd like to
have a default Constructor that I would then use for everything else so I'm
going to have this be public animal and then what I'm going to do is I am just
going to come in I'm going to get rid of all of this because what we can quite
simply do is just simply come up here and go this like this and Define this the
values we want inside of it so it would be no name and no sound exactly like that
and that would automatically set those and then we put empty curly brackets at the
end of it so this is the default animal if nothing is set and let's go and
create another one when I just go and get rid of all this so I'm going to get
rid of these default Constructors that we created in the last one and we're
going to make them better ones okay so what I'm going to do here is I'm
going to create a Constructor that is going to be called only if a name alone is
passed and no sound so I'm going to go public again animal like that and then
I'll have string and name passed inside of it and then what it can do is go
this and name and for our other value we will say no sound because none was
provided and then again we will throw empty curly brackets after that then what I
can do is create the Constructor that's going to make all these error messages
go away so this is going to be the Constructor that receives parameters I'm
going to call this animal and string and name and string and sound like that we can
come down inside of here and we can use our Setters so I'm going to say set
name equal to and it's going to be the name that they passed inside of it and
for sound we can say sound equals to sound exactly like that and that also works
now let's say that we want to increment the number of animals using the
property we created we can say number of animals like this and let's come down
here and make this uppercase so there's number of animals and where am I lost
my place okay so this is actually calling that method down there to increment the
number of animals by one and then I need to Define my read-only value which is the
ID for my animal so to do that I say random and we'll go Rand like that is
equal to new random that and I will do ID number and equal to Rand and then I need
to go and generate a big number that hopefully we have no chance of getting any
other types of animal side of it so I'll do the food one four seven four eight
three six four oh three six four oh I don't think we have to worry about having
any more animals than what we have right there and I have make sound set I have set
name set I have everything else here set and it doesn't look like I have any
errors and so let's go over into our program area now and start messing around
with our new class we just created all right so I'm going to go into here
I'm going to get rid of this struct we do not need it anymore and all of the
code is on GitHub there's a link in the description of course and then
we'll go into here's main we don't want any of this stuff that is
inside of main so let's just keep going down here until we get to this part and
delete it all right nothing else here that is of importance so now let's go and
create a new animal object so I'm going to go animal I'm going to call it
cat is equal to new and animal all right so we have a new cat animal and we would
like to set its name can't set it directly anymore now we have to use our
Setter and I'm going to say whiskers is the cat's name and then after that
I can also go in and set the property which also calls a our method and this is
going to be for sound and I'm going to say meow and you can see here how
they're different so we have set name and set sound so set name is structured
like this it's just going to verify that there's no number inside of it and
while sound is going to be set up right here and we have our getter automatically
set up here and our Setter setup like that so that is the difference that we have
in how we have those set up and like I said before this way of setting up uh sound
right here this is called a property this is the best way to set it up not like
this with separate Getters and Setters I just provided that is a way of looking at
the right way versus the wrong way but I'm going to change anything clearly you
can go in and change anything that you would like to change with them for homework
okay so now what I'm going to do is I'm going to say console and let's
do a right line and I'm going to say the cat is named let's get that value
and says and let's go and get that value and I can go and use my getter's
cat dot get name like this and I can go and get my sound by saying cat dot sound
again you see the properties are much easier to understand and that's why you
should use them instead of using these methods but like I said perfectly fine
either way and now what I'd like to do is say that I wanted to assign a owner
for my cat and it is going to be me so I'm gonna say Eric and then I can come
in and get the cat's name as well as the owner who is me so console Dot and
let's do right line again and inside of this I am going to go and get the
cat's name so there it is and uh owner is and then owner whoops yeah owner is
and get my name and there it is it is and then we can go and get both of them just
simply by saying cat dot get name fat and Cat dot owner like that and close those
parentheses all right what else would I like to do well I'd like to get the
read-only ID number for my animal so why not just copy this whole thing so copy and
paste there it is and what it can do is I can say shelter ID is so I'll say
shelter ID is and then I can get the name just like I always got it and if I want
to access the ID number that's stored inside of there remember it's read
only I just say ID number like that oops it's ID num with a capital N and
anything else I'd like to do yeah I'd like to test my static property to
show you how to access those so console right line and here I'll say number of
animals and uh let's leave that there and let's just do a number sign of
animals and like this and this should be changed to a0 however and then to get
access to that static property you just call the class name followed by number of
animals exactly like that and that's good stuff and we can run this to see it
work I think it'll work yeah it looks good and then we can come in and look and
it says cat is named Whiskers and says meow whiskers owner is dark you can see my
automatically generated ID and the number of animals is equal to one and I think
that is clearly a better way of structuring a class using properties all right so
now what I want to do is talk about inheritance and a bunch of related topics as
well now remember I said that if we have a subclass we are not going to be able to
access this name field from it and a subclass means that we're going to create
a new class that's going to be based off of everything an animal but it's
going to make a couple different changes and that subclass's name is going to
be dog now if I have this instead of marked as private but I have it marked as
protected that means that I it can be changed inside of our subclass and as
you're going to see that's something that I will
want to do I'm going to get rid of a couple pieces of information that are
stored inside this class I mainly created them just so I could demonstrate them so
I'm going to get rid of all of this information and whenever we talk about
classes well if we have a inheritance situation this is kind of jargon but it's
something that's important to understand if we have some if we use inheritance
which means we go and get all the properties and methods inside of this animal and
put them into a new class once again it's going to be called dog this is called
a is a relationship now we have another way a type of of uh relationship when
we're creating classes or whenever we have properties inside of classes and
this is called a has a relationship and it's often referred to as a delegate so
what we want to do here is we want to mark this as protected and we want to have
this be called animal ID info and let's go and create this class so I'm
going to go up here where it says console app and I'm going to say add and
class and the name of this new class is going to be animal ID info exactly like
that and as you're going to see as we do more and more complicated things here
we are going to go and um set them up in numerous different classes so inside of
this class I'm going to have public and int ID number and I'm going to
create the Getters and Setters so I'll say get and set exactly like this and we
will set this to zero like that then what I'm going to do is I'm going to
say public string and as you can see makes sense for a animal to have a has a
relationship with both of these so there's the Getters and Setters and then
we're going to mark this by default as no owner and there we go so the ID
number and the owner is going to be stored inside of it now back into animal there
it is and protected animal ID info is stored right there and I'll call this
animal ID info and we'll say equal to new animal ID info whoops info there we
are and there we go all right so now what I'm going to do inside of here is
create a method that's going to set this information so I'm going to say
public void set animal ID info and it's going to receive a integer and I ID
number and a string which is going to be animal ID info and anything else that I
would like to do with this whoops well that's not right that's going to be
owner I mean all right though okay so owner owner there we go all right and then
what I can do is go and call animal ID info dot ID number and have it be set to
whatever the ID number is that was passed inside of it animal ID info dot owner and
have that be set to whatever the owner was that was passed inside of it then I can
also provide this information so I'll say public void and get animal ID info
and then we will print out a custom message so it's going to be console and
right line and inside of it we'll go and set this up so that we can put the
variables directly inside of here so we'll say name like that has the ID of and
then we'll say animal ID info dot ID number and is owned whoops let's go
and turn on and to do that we go tools and options and text editor uh text editor
where is this there it is text editor and then you're going to for C sharp
General and then you're going to look for word wrap on that click on OK and now
it'll automatically word wrap around so that you keep everything here nice and
neat okay so we'll say owned by animal ID info and get the owner information
and throw this here and throw that there okay what else would I like to do well
I'd like to set it up so that the make sound can function can be overwritten in
the new class that's going to inherit everything from this class so I'm
going to say public and I'm going to mark this as virtual and that just allows
me to override it in the subclass which is going to be called dog so I'll say
make sound like this and what we're going to do is we're going to have the
dog make more than one sound and here I'm just going to say console dot right
line whoops right nine let's get rid of that right nine there we are and inside
of here I'm going to say name and says whatever the sound is so let's get
our sound and there we go and what other changes would I like to make this is
perfectly fine um this is going to be perfectly fine as well and the animal here
also looks pretty good we went and changed this why don't we set up um the name
properly so we don't have that and let's get rid of the I think you get the
point of how the get numbers is going to work so we're just going to get rid of
all of or get the number of animals that are created let's just get rid of that
all together we don't need that anymore and what else would we like to have
change here well I said that this is messy with the way that we're handling the
name so let's get rid of that and said to do that or homework but you know that
was in the last video and instead what we're going to do is do the same exact
thing we did for sound but we are going to do it for name now so let's go like
this and paste that right there and I'm just going to change this to name like
that and return and this is going to be name as well and and we said we're not
going to allow them to have values that are digits inside of them I guess I should
have saved that but let's just have this be if and then what we did was we said
let's just get rid of all that um name dot any and character dot is digit so
that's going to block them from using numbers inside of their animal names and
we just need another parenthesis here to have that piece set right and then this is
going to be name is equal to and we'll say no name and uh we could say
can't use can't use number numbers in name that's good and then
otherwise we will just set the name to be equal to No Name and then down here we
will set the name to be the value that was assigned to it okay so now name is just
as good it's set up as a property just like sound is set up and any other
things that I would like to do yeah uh I might just welcome in here and also handle
we don't need owner because we don't have this anymore I like to talk about
inner classes it's uh actually very common to create classes inside of classes
and whenever you do this these are normally considered to be helper classes for the
outer class and what we want to do here is Judge the animal's health so I'm
going to go public and then inside of here let's call this uh public class
Animal Health and it is going to judge if the animal has a healthy weight or not so
I'm going to go public bull healthy weight and it's going to get double and
height and obviously this isn't scientific this is just this is just me
throwing some more functions in here just to do something useful all right so we
have healthy weight and then inside of here what we're going to do is I'm
going to create another double and just call this calc is equal to height divided
by the weight and then I'm going to say if the calculation is greater than or
equal to 0.18 and the calc is less than or equal to 0.27 well in that situation I
will return that yes they have a healthy weight and otherwise I can say else and I
can just say return four or return false I mean it's perfectly fine all right
just to do something a little bit different there now what I'm going to do is
I'm going to create my an or my dog class and inherit everything from animals
so I'm going to say add and class and this is going to be called dog so
we'll go dog like that and we'll click on ADD now remember I said that I
want to inherit everything from my animal class and put it inside of my new dog
class well to do so that's all you have to do and you're going to get all
the fields and methods automatically inside of your new class now what I'd like
to do is I want to add some additional properties to make it more dog-like so
I'm going to say hey my dog can make more than one sound so I'm gonna go
sound two and automatically generate my Getters and my Setters for dog two and
I'm going to have the default second sound just be gir like that all right so
we have that set and I said that over inside of my animal class whoops that's I
don't even need this anymore so let's just get rid of it here's animal
and where is it I have the make sound function and I have a marked as virtual well
I want to be able to change this inside of my dog class so what I'm going to do
is I'm going to say public and to show that you want to override that original
function in the animal class you go override Boyd and make sound now if you call
make sound with dog it's going to use this version of make sound instead of the
animal version so I'll say console and I'll just overwrite this so I'm
going to say right line and then inside of here I'm going to say name says oops
says sound and also sound two so there is sound two and then close that off and
close that off and it's giving me an error because I accidentally jumped out of
the class so let's go up here and paste that in there okay so now all those
problems are gone all right so now what I have to do here to finish up everything
is just create a custom Constructor that is also going to allow the animal it is
basically going to initialize everything that's different about dogs which is
sound too and it's going to let the original animal class handle everything
else so just like always the name of the Constructor is the same as the name of the
class so it's going to be dog and I'm going to receive a string which is
going to be name
that default value of no name for it and then let's go to the next line
I'm also going to have a string for sound which is going to be equal to no
sounds and then also another string because our dog is more intelligent than your
average animal sound two is going to be equal to no sound uh two I don't know
something fine like that and what we can do is if we want the original animal class
to handle initializing the name and the sound we could pass this over to the animal
Constructor and just pass name and sound inside of it it's automatically going
to work but we have to handle sound 2 on our own and to do so we say sound two is
equal to whatever sound two was that was passed inside of it and there you go there
is your uh slightly more accurate very slightly more accurate dog class so now what
we're going to do is we're going to go over inside of our program area and
mess around with it so we have animal whiskers new animal what we can do here since
we now have the name set up using the uh properties is we can go and create new
cats let's just get rid of all that and we're going to say animal and
let's call it whiskers so let's go whiskers like that is equal to new
animal and then let's get rid of the semicolon and inside of it directly call
the name so we'll say name is equal to whiskers like this and the sound is
going to be equal to yes I know cats make more than one sound but I'm just
playing around here and then going through a semicolon at the end of that now what
we can do is then so the cat is still going to be considered a regular animal but
we're going to create a new dog type so we'll say dog and let's have
the dog's name as Grover is equal to new dog so there that is and name
let's change this to Grover so Grover like that and the sound is going to be
equal to woof and we'll also say that it is going to have a second sound and
our second sound is going to be equal to gur all right good and then that's
also a semicolon at the end and then I can demonstrate changing the protected field
sound here by just saying Grover dot sound is equal to and let's change it to
Wolf okay all right so a big wolf instead of a little wolf and then I can come in
and like and say whiskers Dot make sound and whiskers are going to be able to make
a sound and then I can also say Grover dot make sound and there we go with that I
can also come in and set let's just get rid of all of this information because
a lot of it doesn't even apply anymore let's command to the uh let's
set our animal information for whiskers so this is going to be set animal ID info
and we'll say one two three four five for whiskers and whiskers is owned by
Sally Smith and then we'll do the same thing for Grover let's just copy
this paste this inside of here and let's move this up here so you can get more
information on the screen and here this is going to be the second animal we create
let's increment that and let's have this be Paul Brown like that and then
we could say that we want to get whiskers animal information so get animal
information for whiskers and uh what else would you like to do well I I should go
in here and test my inner class that I created so I'm going to say animal Dot
Animal Health and I'll go get Health equal to new animal and Dot Animal Health
all right so now that I have that all set I could come in and say console dot right
line right line and what do I want to do uh it didn't take a right line all
right there it is and I could say is my animal healthy and like this and then I can
plug in the information of course it'll be even better yet if I stored the
height and weight for the animal but then do that don't worry we're going
to get into a much more complicated things and I'll say get health Dot and then
I'm going to call healthy weight which is right here and I'll just pass in
11 and 46 inside of there what else would I like to do um let's go create a
couple more animal uh animal types so I'll go animal monkey is equal to new
animal and set the name for it whoops yeah yeah it's corrected it for me
automatically great let's say our monkey's name happy and our monkey says E
I don't know what a monkey says something like that and uh a semicolon and uh
let's go and create another dog on top of that animal let's call it spot is
equal to new dog and name is equal to spot of course hot and we'll be able to
demonstrate here how we're able to access overridden methods even though spot
is an animal it inside of the dog class here because well let me see right here
because this is marked as virtual if this wasn't marked as virtual we
couldn't do that um let's also come in and say sound is equal to I
don't know and uh what else would we like to do sound two is equal to and maybe
he has a weird girl noise he makes okay so here we are created all those different
types of animals now we can commit and say spot make sound like that and it's
going to automatically make the right sound and I think that's enough
demonstrations for this let's go and run it uh oh we have a bug where is our
bug at oh I see over inside of an animal I need to change this I forgot that I had
streamlined everything let's get this put that there we say name is equal to
name and sound equal to sound that did I get rid of all the bugs I think so
let's go and run it again and find out for certain and up nope there is a bug
okay let's figure out what that is all right and looking at the code one thing
I noticed probably because I crammed all this code together this is supposed to be
set for Grover because we want both of them to have an owner so let's save that
and another problem we have here we don't need this else part inside of here we
could just go and get rid of that old together and let's just get rid of this
and we could set this to name is equal to Value like that just to get rid of the
else that doesn't need to be there anyway okay good all right so I ran it again
I'm still getting an error here as you can see what it's saying is I guess
this is a good thing we can work our way through an error all right so it's
saying that throw argument null exception exception argument and it says animal
line 49 so we're going to investigate that that's where we have css49
it's better to check at the end so let's check animal 49 what is going on
on animal 49 so we can figure this out animal 49. um what is different okay so I
basically did exactly the same thing with name as I did with sound no I didn't
look at that I value and accidentally wrote name all right that'll get rid of
the error okay so let's save that jump over here and let's run it and that
did it okay make sure you don't put uh name inside of their use value of course
and now we can see here whisker says meow Grover says wolf and ger whiskers has the
ID is my animal healthy it says true and spot says wolf anger all right so there we
go we're ratcheting up the complexity of our classes and in the next video
I'm gonna continue doing that all right so now what I want to do is to provide
another deeper example on polymorphism which we'll talk more about along with
abstract classes base classes and a whole bunch of other different topics what
I'm going to do here is sometimes we want to define a class that we don't
want to be instantiated we don't want an object created for it and in those
situations we would use what is called an abstract class so let's come in here
and let's create one so I'm going to say add and class all right and what
am I going to call this I'm going to create a shape class and then I'm
going to create a I'm going to inherit from it with a circle and a rectangle
clasp so what are we going to do here well I am going to change this to abstract
and that just simply means that it is not going to ever be instantiated all right
got that set up so what are we going to do well inside of it let's try to get
that in position I'm going to go and create public string name and Getters and
Setters look at that created it for me good stuff so now what I want to do is you
can Define non-abstract methods inside of an abstract class and how you would do
that is you would say public and virtual it's all virtual here a couple of a
little bit ago and this isn't going to return anything and it's going to be
called get info and it's not going to be passed anything either and what
it's going to do is it's quite simply just going to command and let's
call right line on this and it's going to say uh this is a and Prince whatever
the value for name is this is a and throne name inside here well this will be
capitalized so name like that and then whoops throw in my quotes and there we go
all right and that is all this is going to do for us now if we want subclasses to
override this method we can mark it as abstract and another thing to know is you
can only make abstract methods inside of abstract classes so we're going to go
public abstract double and we're going to demand that area be inside of it and
there you go so there we went and we created our shape class now what we want to do
is we want to create a circle class that is going to inherit and then recreate
everything that is inside of our shape class in its own special way and then add in
anything else that needs to be done so we're going to say add and I'm going
to say class and this is going to be called Circle so circle like this okay so what
are we gonna do inside of our Circle class well we're going to say class Circle
and we are going to inherit everything from
our shape class exactly like that I already have the curly brackets inside of
there okay so what are we going to store inside of it well with circles we are
going to have a double which is going to be our radius and there's our Getters
ad Setters created for us then we're going to also have a Constructor that is
going to receive a radius value so there that is and what are we going to do here
well we're going to have radius stored inside of this and then also I am going
to have name be assigned the value of circle right like that all right so this is
all that our Constructor is going to do for us at this point in time now what we
want to do is we want to come in here and override the area method so I'm going
to say public over ride double and this is area and what are we going to do here
well we're just going to return the calculation value so we can call math dot
pi times math and we'll get our whoops math and get our power function and then
inside of here we'll have radius to the power of two all right and that's
all that's going to need to do now for our get info area let's come in and
let's just retype this I'm going to override that as well so I'm going
to say public and over ride void get info and nothing else changes here and what I
want to do here is first to execute my Base Class version of get info and then
I'm going to come in and I'm going to say console right line and oops
let's get this in here got a little bit fancy all right so let's go get
this and we're going to say it it's going to automatically print circle
inside of there by the way I'm going to say it has a radius of and get whatever
the radius value is so there is our radius value and then close that off all right
so good and now we have everything that we want from our Circle class now let's
save that just to make sure it's saved I'm going to do now pretty much the
same thing for my rectangle class so come in here go class and then this is going
to be called rectangle there we are and add that to it and inside of our rectangle
class how many things are we going to change a good number all right so let's
say class and this is going to be rectangle also going to inherit from our shape
and already have those curly brackets don't need those okay and then come down
here and we are going to have two different fields inside of here so we're
going to have a double which is going to be length and Getters and Setters and then
we're going to also have another one double which is going to be width look at
that okay then we're going to have a Constructor we're going to say a
public rectangle and it's going to receive a double which is going to be the
length and also a double which is going to be a width and then it is going to store
those different values and then we're going to say that the name for this is
rectangle and there we are now I need to come in and override my area method so
I'm going to say public over ride returns a double and area and here I can just
simply come in and say return and it'll be length times width and there we are
okay so we have all that set now I need to change my um my get info why don't I
copy the circle version of this so I'm going to go copy this and then jump over
into rectangle and just paste this down inside of here paste that there get info
again we're going to execute the base version of get info which is going to
print out the name and then I'm going to say it has a length we're going to
have length inside of here instead of and this will be length length like that and
then we'll say and a width so a and I always just say width of width and
it'll throw that inside of there for output to the screen and uh we don't
need this extra space here okay so there we go we created all of those now we can
go in and we can actually work with both of them inside of our main function so
let's come into our main area and inside of here we can store our shapes in a
shape array as long as Eddie shapes stored inside of it contains subclasses of
shape so we could say shape there's a shape array and we could say shapes is
equal to new oh yeah whoops yeah let's throw our curly brackets new circle with
a radius of five and then also a new rectangle like that which is going to have a
length and width of 4 and 5. so very easy to store those then we can come in and
cycle through the shapes and print out the areas because everything that is needed
is already there so we could say for each and shape s in shapes and this is true
even though these are circles and rectangles instead of shapes they're still
ultimately shapes so we're going to cycle through all of these first thing that
I would like to do inside of here is go and get the shape that we're cycling
through and then call get info on it don't need to do anything else I'd
also like to come in let's you and print out our area so I'm going to say
right line and let's get rid of this part right here and instead we'll
change this into zero and area and let's say that we want to format this to
only say like to have two decimal places or something along those lines we could
say F and 2. and I don't think we need to do anything else with that and then
we'll just say that we want the name and also we want to calculate our area
like that and then we have to throw in another parentheses here to close that off
and another thing is you can use as to check if an object is of a specific type so
let's go and try that out as well so I'm going to go now let's keep it
in the same area here so I'm going to say Circle best Circ is equal to S as
Circle and then I can say if test Circ equals null then I'm going to say
console right line and this isn't a circle oops I think that's all that I
want to do there and then you can use is to check the data type as well so you can
use as or is so let's go right here and we'll say if s is circle then we
could say this isn't a rectangle let's copy that paste that right there
this isn't a rectangle rectangle there we are and is there anything else that I
would like to do yes let's demonstrate polymorphism so this is the four each I
want to get out of that area and I'm gonna it's basically what you can do
I'm going to create object Circ one is equal to new Circle Four and then
I'm going to say Circle Circ 2 is equal to and what I'm doing here is
basically you can store any class as a base class and then call the subclass
methods even if they don't exist in the base class but you have to go in and
cast them to that specific type for that to be able to work so we'll say
console and let's do right line and then inside of here we're going to say
uh quotes the and get the name for the circle area is and let's output that
value as well so we'll go get this and then what we need to do is just go Circ
2 dot name and Circ 2 dot area and that is all going to work as long as you cast it
all right and that is all we need to do as long as there aren't any errors and
it looks like it worked great okay so let's zoom in you can see as we're
cycling through here this is a circle you can see it has a radius of five prints
out you can see it calculated its area you can also see remember we are currently
looking at circles and it says this isn't a rectangle versus saying it is a
circle um then we have a situation in which we are working with a rectangle it says
this is a rectangle and it prints out the length and the width it calculates the
area and again this isn't a circle and finally with our last example there even
though I marked the object as type object it was still able to calculate the area
because I was able to go in and cast it to the proper type all right so there is
yet another example of how we can work with classes abstract classes override
methods and more now what I want to do is talk about interfaces and I'm going
to provide you with actually three examples one is going to be not complicated at
all and then we're going to proceed to being more and more complicated so
I'm going to create uh let's go here and go add and I want another class
and this is going to be called vehicle so vehicle and that's good and you have
to think okay what do we need with our vehicle let's create another class
let's create our interface too also so let's come in here and basically an
interface is a contract and it basically just means that if you inherit from the
interface you must Implement certain methods defined inside of the interface so
let's create that let's work with the interface here first I think that
makes more sense and basically there's nothing in an interface except for
abstract methods and like I said they're basically like signing a contract if
you go and inherit from them and another thing that's important to know is
interfaces commonly have names that are adjectives and on top of that it's very
common for and with objects it's normally nouns and it's also important to
know that it's considered common to store interfaces with a capital A letter I
now this is an interface oh let's get this and change that to interface
drivable and interfaces can have properties so let's go in what I'm doing
here let's go right here and I'm going to say that we are going to have
wheels if it's going to be something that is drivable and we're also going
to have speed and there that is and have that implemented okay and then what we do
is we are going to put in some different abstract methods inside of here so
we'll say move like this and close that off and then
void and another thing our vehicle must be able to do is to stop okay there you go
there is our drivable interface now inside of vehicle we are now going to implement
the drivable so we'll say that we want a class called vehicle and it is going
to inherit everything that is in drivable then we are going to come in and
let's say what exactly I mean we're we're basically trying to create a
real world object so what does a vehicle have well one thing a vehicle has is a
brand name so we're going to call this brand and get that set and we're
going to command and create a Constructor for this so I'm going to say public
and vehicle and I'm going to say string and I'm brand and I'm going to
give brand a default value of no brand so there that is and I'm going to say
int Wheels are equal to zero and uh well let's oh let's have that be
implemented here so let's get rid of that this is going to be passed inside of
here so I'm going to say and let's put it on separate lines also I think
that makes more sense so we'll say int Wheels is equal to zero and let's
put this here and we'll say that it also oops has a speed and we'll give
that a default value of zero as well all right now that we have that set we can say
brand is going to be equal to there we are the brand wheels are going to be equal
to Wheels and speed is going to be equal to whatever speed they entered and these
have to be uppercase oops wheels and speed and they are going to match up speed
there we go they're going to match up with what we have in our interface see
wheels and speed there they are okay so now that we have that set what we need to
do is go and create our properties from our interface so let's get out of this
and I'm going to say public double speed and let's go this and Getters and
Setters and then we'll also say public int whoops it put it up there for me
there we are and now we have wheels set up as well and now we need to implement
move and stop because that is a demand so we'll say void move and this is just
simply going to say uh console and right line and uh what are we gonna have here
we'll say hopefully it has a brand name so we're going to say the brand
moves forward at and whatever the speed is there we are and uh the speed and then
I'm just gonna have this be miles per hour okay there we go so we have that and
we're going to do pretty much the same thing for stop and these must be
implemented so we'll come in just change this to stop and we'll just say
the brand and then we'll just say stops and uh on top of that we're going
to set our speed to be equal to zero there we are okay so now over in our program
file we can go and Implement all this so I'm going to create inside of main a
new vehicle and I'm going to call it a Buick is equal to new uh V pickle and
I'm going to give it a name of Buick and I'm going to say what do we have
here we have speed and wheels okay that's what's passed in right in that
order well wheels and speed okay so we'll say that the wheels are four and the
top speed is 160 okay what I want to do is if you want to check if the vehicle
implements the ID drivable interface you can say if and Buick is I drivable well
then I can say since it is then I know that I have my move function that I can use
and I know that I also have my stop function that I have available to me and if not
it doesn't inherit that then we're going to say well this didn't
implement the interface that we want you to use so we are going to say that this
car is faulty and cannot be driven so we'll say the whatever can't be
driven like that and then we'll come down and we'll say Buick and we will
go and get the brand name set for our Buick all right so pretty simple example
don't worry the next one is going to be way more complicated and we're
going to come in here and we'll run it and there it is and you can see right
here Buick moves forward at 160 miles per hour and then we call for it to stop and
it stops okay so now we're starting to get more into the world of implementing
objects and having them work together now what I want to do is take it up another
notch and we're going to simulate TVs TV remotes individual buttons and even
the act of picking up a remote control alright so the very first thing we want to
do is we want to come in and we want to create an interface that is going to
represent electronic devices so I'm going to come in here and I'm going to
say add and class and what are we going to do well let's call this I electronic
the mice okay so there's our electronic device and what exactly are we gonna do
in here um well basically interfaces can be used to create extremely flexible
systems so for lack of a better word let's go and let's call this an
interface so enter face like that I electronic device so what we're going to do
is we're going to model a generic electronic device devices and uh you can
we're going to use a television but you could have it work with anything like
radios and so forth and it's also going to have a remote and we're also
going to program the buttons so everything can be very easily switched between now
what do we want to do here well we want to list in this interface just those
capabilities we want anything deemed to be an electronic device to be able to work
so we want to be able to turn it on so there's on what else we want to be able
to turn it off we want to be able to turn the volume up and we want to be able to
turn the volume down look at that how smart that was okay so there we go those are
all the capabilities that we want anything deemed to be an electronic device to be
able to do now what we're going to do is we're going to create a remote
control and what is let's go uh did I click on our yeah there it is there's
this and class and this is going to be remote control and we're going to
interface these different devices together so I'm going to call this TV remote
and there it is all right so what is this it's a class it's just a class TV
remote I mean it's basically a first class type of object all right so now what
we're going to do with this is we are modeling the action of picking up the
remote with our hand that is literally so an action and in the next video that
follows you're going to see that I can break down a fight between multiple
people some people with superpowers and some people without superpowers and things
like that into tiny little pieces and then have the interaction with them inside of
Maine even though the whole interaction is quite complex be very very simplistic so
I'm gonna have public static eye electronic device git device and what
we're going to return is a television I'm going to actually create this in
a minute so I'll say return new television okay I just wanted to start with the
act of picking up the television let's create the television now so we'll
say add and class here it is and this is going to be our television so we'll
say television and create that all right so what is television going to do well it
is going to inherit from I electronic device so let's come in here I electronic
device there it is and because we implemented the electronic device interface any
other device we create will know exactly how to interface with this new device just
created here so we're going to say and that's one of the importance one of
the most important reasons why we use interfaces is it's a contract that
everybody knows that if you implement that contract you will have these certain
functions and everything will just work so I'm going to have volume and Setter
and then what do I need to implement well I can actually come up here on this and
then go down to show potential fixes and say implement the interface and look at
that it automatically creates everything that I need to implement so what am I
going to do well whenever the off method is called I am simply going to command and
go console right line and say the TV is off and print that out and what else are we
going to do let's go copy this and of course instead of having it just simply
print this we could have it interface with an actual piece of hardware and have it
work and then we have on and we'll say the TV is on and volume down and change
this and we could say if volume is not equal to zero well then we'll call
volume and decrement it all right and then we'll command to this guy right here
and we'll say something like the TV and volume volume is at and whatever the
value for volume is after it's been decremented all right and then we're
going to do basically the same thing for our volume up so we'll copy this come
in here paste that right there and if it is not equal to 100 then we know that we
can continue to increment it so we will and then it'll say the TV volume is set
at and whatever the volume is for that all right and then we have our TV remote
which just simply creates a new television it's just like grabbing the remote
now what we can do is we can actually go and create another interface that can line
up with a button and perform different actions depending upon the state of said
button so let's say we just want to work with our power button and make that
work well we could come in here and we could say add and class and here is an
interface and I'm going to call this I command there it is all right this is an
interface interface I command and what is I command going to do well it is just
going to have two commands one's going to be execute and the other is going to
be avoid or undo let's have it be undo all
right that is all it does all right and then we basically leave the individual
buttons to Define what they should do so I'm going to come in and I'm going
to say that I want to create a new class and it is going to be for our power button
so turn our television on and off and by breaking everything apart like this to the
individual buttons makes it very easy to change these just a couple lines of code
and have everything just simply work so what do we have to do here well this is
going to be a class called power button and what is it going to what interface is
it going to work with it's going to work with I command all right and we can
come in here on top of this show potential fixes and say Implement my interface all
right good oh I got that all set up now what I need to do is you can actually refer
to instances using the interface so I can say something like I electronic device
and let's call this just simply device without well like this let's just
call it device all right so we have that set and now we can get into the specifics
of what happens whenever a power button is pressed so I'm going to come down
here and create a Constructor for our power button and it's going to be passed
a electronic device there we are and then we're just going to say this device
is equal to device all right very simple now what happens whenever we are call
execute is called on this well we can just simply say device like that and then go
dot on and interface directly with the electronic device I electronic device see
there's the on and we're also going to use the off all right and now
I'm getting lost okay so that's good and then on do we can have that
instantly also work except undo is going to turn it off paste that there device and
this will be off we're really getting this to work on the micro level all right
so now that we have all that set up we can just go into program and and we can
interface with our television quite simply with just a few lines of code so we want
to simulate the act of picking up the television remote how many lines of code does
that take it is not going to take much so say I electronic device and we'll
call this TV is equal to and TV remote there it is and git device see now
everything is acting like a real world thing so that is the act of picking up the
device and then we want to implement our power button so we say power button we
just call it power but like that is equal to new power button and Pass TV to it so
now we know specifically we are working with a TV power button Now to turn the
television on and off a couple times we just say power but and execute and it's
just gonna work and likewise let's just copy this so copy paste and what do we
call we call undo so that's pressing the button twice and then we can go like
this and copy and paste and run it and you can see that simply by calling that
command a couple times we're able to turn our TV on and off and of course we
could get much more complicated and I leave adding the volume up and down and the
more complex parts of working with an application like that to you for homework and
up next I'm going to cover one of an example that is probably one of my most
liked examples I'm going to show you how to implement a fight to the death
between four and the Incredible Hulk alright so now we're gonna have some fun
what I'm going to do here is this is going to be our final output I'm going
to create a Thor object and a Hulk object and they are going to fight to the death
and then what I'm going to do is I am actually I think I'm gonna have four
fight low key because it will be fun to then use interfaces to give the current
people fighting different capabilities and I think that would expand upon it so if
we look here what what's happening okay so Thor attacks hulky does 74 damage so
that's an attack amount and then Hulk is going to lose a certain amount of
health so what what this is describing here is Thor has an attack value and Hulk
has a defense value and then of course they also have a maximum health so what we
want to do here what our aim is is to take this somewhat complicated sort of format
and we want to simplify it as much as possible so that whenever we are executing
our code it's very straightforward so what we in essence want to have here is
we want to Define four and we're going to say that 4 is uh a type of warrior
and a name is going to be defined a health amount is going to be defined so that
would show how much punishment he can take a how much damage can he do and how much
can he block that damage or how effective is he at doing that and then in essence
we're going to do the same exact thing with the Hulk alright so we'll have
Hulk and so we'll throw Hulk right here and then whenever they battle we want
to keep that also as simplistic as possible what we want to do is say okay
here's a battle and start fight and who's fighting Thor and Hulk there we
go that's our aim right there and and this is the whole goal of object-oriented
programming is to keep the apps the execution of our code extremely simplistic
extremely short and extremely understandable and you know this is Hulk but I said I
wanted to use Loki instead so all right so that is your first ideas how could I
what different attributes are my uh Warriors going to have what different functions
are they going to have what sorts of capabilities do they have and then I want to
keep the actual creation or the start of the fight as simplistic as humanly
possible and it just prints out all that stuff well what am I going to have to do
here I'm gonna have to come in and I'm gonna have to create a new class and
we're going to call it Warrior so this is Warrior Warrior all right there it is
we created Warrior now what is what you're gonna have to do all right well like
I said before we are going to have different attributes what do those attributes go
to be we already describe them we are going to have public and string and name okay
we have to define the names and why didn't it bring up my there it is all right
so we have a name what else are we going to have well let's just go like this
and we also have a health amount so Health we know that and up next didn't give
me my string let's go public uh this is actually health is not a string
it's going to be a double so let's make that a double and what else did we
say we need well we need a attack amount but is it going to be a fun game if every
single time they fight they do exactly the same amount of damage no in that
situation if they both had the same block amount and the same attack amount well
every single time whoever goes first is going to win so that's boring so what
we want to do instead is have an attack maximum and then what we'll do is we
will go and um calculate a random number and multiply at times that attack maximum
to judge how much damage was done and then also let's add that same amount of
Randomness to the block amount and like I said later on we're even going to add
like magic moves and things like that just to make things even more cool all right
so we need to do random number Generations so we're going to say random and
Rand is equal to new random exactly like this and whenever you're doing random
values like this you always want to create a single random instance and reuse it
and if you you don't do that you're going to get the same value over and
over again so we're just going to stick with that okay so now it gets to the
point where we have to create our Constructor for our Warrior very very simplistic
we're just going to say public and Warrior and it's going to be past a
string which is going to be the name for our Warrior so let's just have
everything Auto generate here so we're going to have string and name and
let's give this a default value of warrior all right so we have that and then I
like to put all these on separate lines just so it's a little bit easier to see
everything and I don't need random I'm just going to need these guys and
I'm not going to be past a random so let's get rid of that and I'm
going to give default values for all of this also so I'll say that the health
is going to have a default value of zero and attack zero and block also whoops why
did it give me that there we are we are uh that's also going to be zero okay so
then we have name is name Health attack about and I'm going to get rid of this
because I decided I do not need that anything else nope we want to keep everything
simple all right so there is the Constructor what do we need to do now well we need
to be able to generate a random attack amount so I'm going to go public and
you're going to notice as I'm doing this that for the most part all of the
functions are extremely short in length so I'm going to get them as short as
humanly possible so I'm going to say return and then what I'm going to do
is go random and next one through whatever the maximum attack amount is so there is
attack Max and there we go and that is all we're going to do for our attack and
then we get over here and paste this in and this is going to be block so we'll
just change this to block and um let's also Mark this as a virtual so we'll
say virtual because we might want to overload this and or override it I mean and
random next and then this is going to be block Max so changes to block Max and
there you go very simplistic anybody that looks at this is going to very easily be
able to understand what this class does what the attributes are referring to and
what types of capabilities our Warriors are going to have which is our ultimate
goal now we get
into the whole point of being able to Simply say battle start fight and they just
fight that's you know taking it to the next level so we're going to create
another class I'm going to go class like this and I am going to call this
battle so there's battle we just created it all right so this is going to act
as a utility class so it's going to make sense that we only have static methods
inside of it that is all it's going to be used for so we'll say class and
battle and what types of things are we going to need to do well we're going to
have to start the fight okay so start fight that's our very first thing because
we call that and then everything else just works its way out then what else are we
going to have to do well we're not really sure so let's go through the
process of exactly what happens whenever a fight is started well um let's just
we'll say warrior one attacks Warrior let's just say warrior two like this
so we'll say warrior one attacks Warrior Two and Warrior two well they'll
say Warrior Two uh is damaged and Health decreases all right and then what's
going to happen well we're going to have the Warriors have an opportunity
One's Gonna attack then the other one is going to attack so we're basically
going to do this but we're just gonna have it be Warrior two and Warrior one so
we'll just go like this and paste this here and then this is just simply going
to be Warrior Two attacks Warrior one and then Warrior one is going to be damaged
and health decreases that's what's going to happen so let's keep that
as simple as humanly possible what uh well what let's think more about what
actually is going to go on okay so Within These two what is actually going to
happen we are going to need to get a result so we're going to say get attack
result and remember we want to get our or keep our functions as short as humanly
possible so we're gonna go and we are going to want to have this part right
here in a separate function all of its own so we'll come down here and we know
that we're going to have another function called get attack result anything
else that we're going to have to do um not really at this point I can't
think of anything so what we're going to have to do is just simply decide
what's going to happen inside of start fight so let's go and let's
create that we'll do it here so that we don't get rid of our comments as
we're thinking through all right so we're going to have both Warriors
attack so why don't we just Loop through both of those Warriors attacks and see
what our results are so we're gonna say public and remember these are utility
methods so they're going to be static void and I'm just going to call it
start fight because that makes the most sense if I can stop hitting my caps locks
okay so start fight there it is and what's going to be passed to it well
Warrior one so we'll call it Warrior one and Warrior two all right Warrior two
all right so we have both of our Warriors passed inside of here to start to fight
but then we're gonna have a fight between them so we need to figure out what
exactly git attack result is gonna do to us um or do for us now basically what I
want this Loop to do is to continue looping forever until we get to a point in
which one Warriors Health goes below zero then that's the end of the game so
what exactly is going to happen in git attack result well we are just going to
Define it so public static and it's going to return let's say a string and
inside of our looping structure inside of start fight it's going to look for
that string and cancel whenever it's returned from get attack result how about
that sounds good to me so we got string and then git attack result and what are we
going to be passed we're going to be past a warrior and let's call this
Warrior a to differentiate it from the Warrior One and Warrior Two that we used
before so Warrior and Warrior B all right so we have both of those what needs to
happen inside of get attack result well we need to calculate the uh Warriors attack
and the block for the other Warrior so we're gonna say double will go Warrior a
attack amount and that is going to be equal to Warrior a and get that attack from
that Warrior that was created and also we're going to do the same thing for the
warrior Bay so let's go like this copy and paste so this is going to be Warrior
B and this is going to be Warrior B also all right so we have those attack amounts
then what we want to do is we want to subtract the uh Warrior B's attack from
or we want to take Warrior A's attack and subtract the block amount I think
that makes the most sense yeah so let's change that let's change this to
block like this and then inside of start fight what we can do is the first time
throw will pass Warrior one as the very first warrior in Warrior Two as the second
Warrior and then the second time around we'll just flip those and have this be
Warrior Two and this be Warrior one all right that makes more sense okay so this is
going to be block amount makes more sense block amount and and since they're
Warrior a and Warrior B it doesn't make any any uh difference between what
order they're passed in so we'll say double and we want to get the total
amount of damage done to the warrior so we'll say damage to Warrior B is equal
to the warrior a attack amount minus the warrior B block amount all right so now we
know the total amount of damage that's going to be inside of this let's
change this to attack like that better all right so now we know the total amount of
damage so what do we need to do now well we need to verify that Health hasn't
gone below zero if it does that's the end of the game and otherwise we just
subtract the damage amount from our Warrior so we're going to say if damage to
Warrior B is greater than zero because it is possible for it to not be greater than
zero well in that situation we'll say warrior B DOT health is equal to Warrior
B DOT health minus the damage to Warrior Bay okay got that set and else we're
going to say damage to Warrior B is equal to zero all right good then what I need
to do is I need to print out the message that I originally wrote down and I showed
you in the program file so what we're going to do is we're going to say
console and right line and let's go and zero and attacks and this is going to
be the other name so zero and one are going to be the names for our Warriors and
Deals and we want to say how much damage was done exactly like that and then we
just need to go and get those values so we can say warrior and we'll do Warrior
a dot name and this is going to be Warrior B dot name and damage to Warrior so two
Warrior B all right good there it is so that's going to print out the first
message then what we want to do is provide output on the health change because we
also said that that was important whenever we were deciding what we wanted it to
look like when these Warriors attacked each other so I'm going to say zero zero
one has and I want to get the amount of Health that the warrior now has oh like
this and that looks pretty good also and also maybe we want to throw a new line
inside of there so that everything is separated nicely on the screen now we're
only dealing with Warrior B name and Warrior B's health so we'll throw elf
inside of there all right so we got all those set now what we need to do finally is
we need to check if the warrior has died so I'm going to say if Warrior B DOT
health is less than or equal to zero well that means warrior B sadly has died well
in that situation let's just paste this inside of there we'll say the
warrior has died and the other Warrior is Victorious is Vic Victorious Victorious
there we are and then let's also throw a new line in that situation so that
everything is divided up and in this situation it's going to be Warrior B has
died and Warrior a is Victorious and we do not need anything else to be passed to
that so let's get rid of that damage amount and then remember what we set up
here is that this is going to be returning a string and inside of start fight
we're going to look for a string and if it matches we're going to end the
fight okay because we don't want to Loop forever so in that situation let's
return a string and just to keep it very simple and very understandable just say
game over we could return negative one or we could return something else but no
we're just going to say game over and then after the return statement we're
going to say else return and maybe we say fight again just to make everything very
descriptive we normally wouldn't do this in a program but I just want you know
everything to be understandable okay so what we're going to do now is go back
up into our start fight area and we are just going to have the Warriors fight until
they die so I'm going to say if what do we want to do we want to get our attack
result and what are we going to do here we're going to pass in our Warrior one
and our Warrior Two and we're going to say equal to game over so if the attack
result that is returned is equal to game over let's change this to lowercase
word just notice that then we will come down here and say console and right line
like that and then inside of here we'll say game over and there we are and then
we can call break to jump out of here all together but we're going to do this
inside of a loop because remember we have to have the Warriors fight each other so
we'll say while true continue looping forever or until you know everything ends
so we got
to get this part right here and throw it up into the top so let's paste that
inside of there and there we go and let's go and do another one because both
Warriors need an opportunity to try to kill each other so we got Warrior one and
Warrior Two what's gonna change about this well we can change this to Warrior
Two and change this to Warrior one all right and I can optimize this a little bit
more to make this function shorter but I just want to keep it like this because I
think this is extremely understandable so I think it's pretty easy to
understand that the words are Taking Chances or taking their turns attacking each
other and we're going to continue doing that until one of these Warriors is
dead so that is the goal and this needs to be lowercase and this needs to be
lowercase also save that do I have any other errors nothing I see okay so let's
come back over into program and I'm going to test this out see if it works and
then if this works what I can do is go in and actually have the Warriors fight it
out with uh maybe I'll give Loki superpowers or something like that all right
so I'm going to say warrior 4 is equal to new Warrior and let's get rid of
this and I'll say that I want to also Define another Warrior which is going to
be Loki paste that inside of there and this is going to be low-key and Loki then
the goal is I can just run this one line of code after creating this and it's
going to just automatically work so we're going to say battle start fight and
four and low gear passed inside of it like that and we'll save it and let's
run it and see if we have any errors boom don't look like it all right good
stuff so Loki attacks Thor deals 10 damage and Thor has 49 health and they go and
they continue attacking each other until finally Loki has negative 14 health and it
says Loki has died and Thor is Victorious and game over so pretty cool you know
what would be more cool is to add even more Randomness to what we have right here
and I want to add some more Randomness first off they have exactly the same Health
they have exactly the same power they have exactly the same blocking ability well
we know that that the that isn't true okay so Thor is bigger which means he can
take more damage and he's also more than likely going to be able to cause more
damage but also we know that Loki is a magic user so with that knowledge how can we
make this fight more interesting well I think one thing that we could do is we
could use an interface and let's give Loki just one superpower the ability to
teleport so if four tries to a attack Loki maybe sometimes again adding
randomization to this whole entire program sometimes Loki's able to teleport
away and not get hit so let's go and anytime I want to add any additional
capabilities an interface normally makes pretty good sense for that so the
superpower we want to give them is going to be called teleports and add now inside
of this like I said this is an interface so we'll say interface teleports and
what's going to happen well it's just going to Define string and teleport
so Loki is going to either have the ability to teleport or not have the ability to
teleport so that means we need to have a couple more classes so we'll submit
here and we'll say add and class and what's this one going to do well this
one's going to be that can teleport okay so maybe the hero doesn't have the
ability to teleport and other Heroes do so we'll say add and in here we'll
just say class and then we're going to say that we want to inherit LL ports
like this and it's going to give me an error message which is good and it's
going to say show potential fixes Implement interface good writes a code for me and
all this is going to do is it's just simply going to say return tell uh ports
away okay so there in this situation Loki would successfully teleport away well we
also want to have the situation in which a hero can't teleport so we'll go
up here and we're going to say class and we'll just call this can't
teleport so can't tell uh port then what we can do is we can have different
Heroes and we could give them certain capabilities and some of them will work for
teleportation and others will not work for teleportation so this is how we can go
about stacking different capabilities in other classes and then having them work
quite differently so I'm going to say can't teleport I'm going to
inherit from teleports and have it create my my function for me show potential
fixes there it is Implement interface and then we'll say return and fails at
teleporting okay just throwing this in here to make this a little bit more fun okay
so now what do we need to do well we have kind of a different type of warrior and
this different type of warrior has magical abilities however and that is going to
affect how block works but the attack is going to work in exactly the same way so
there's no reason for us to change it and also we don't want to have to
completely recreate Warrior so what we're going to do is we're going to
create a new class and we're going to inherit what we want from the warrior
class and then create new stuff for our uh Magic Warrior okay so magic Warrior you
can have all kinds of different types of words all right so we have the magic
warrior class and we want to inherit everything from Warrior because we did all
kinds of work and we already know that Warrior works so you know there's no
reason to go in there and mess that up okay so what's different about our magic
warrior in this specific situation well the one thing that's different is they
can teleport that's it so far that's the only capability I think it's a
great idea if you're watching this video that you go and try to add like armor
and different weapons and all that stuff and take them away and add them and so
forth and that's a great way to learn that's how I started programming many
many years ago is by making video games so to make this more interesting what do we
do we add randomization so we're going to have a Teleport chance if they have
the ability to teleport maybe it doesn't work sometimes that adds some interest
what else do we need well we need to come in here and get the information about
teleporting whether they were successful or they were not so teleport type and
that's going to either be new can teleport and there it goes and then what we
can do is we can change inside of here we can actually change this to can't
teleport if there's certain situations in which the hero can't teleport or
the villain all right so what do we need to do now well we need to create a
Constructor so we'll go Constructor magic Warrior like this and pretty much the
same exact stuff is going to be populated inside of here um actually I know not
what they put here though I'm going to be past a string which is going to be
name equal to and Warrior like that and also we're going to be past double
which is going to be health and let's give that a let's give that a default
value of zero also and double attack Max equal to zero and double block Max equal
to zero and then we have a new one which is teleport chance so this is going to be
like a percentage teleport chance is equal to zero and then what we want to do is
we want to have our base warrior class handle everything else and it automatically
popped up there for us so name Health attack Max block Max all done for us so come
in here and then all we need to do is add the little tiny bit that has changed
which is the teleport change our teleport chance all right now what do we need to
do well our blocking is going to be completely different from Magic Warrior because
sometimes the magic order is going to get attacked and they successfully teleport
and take zero damage so we're going to override that block function so
we'll say novel everything else is the same so we'll say dot uh double
block like that and let's get rid of this we need to come in here and generate
a random value from 1 to 100 based off of the block chance so we're just uh
create a random Rand is equal to new random and then int random Dodge like this is
equal to Rand dot next one two one hundred and then what do we need to do well we
need to calculate and decide if the teleportation is going to work based off of the
percent that was assigned so to do that we're going to say if Rand Dodge is
less than this dot teleport chance then in that situation we're going to say
console and a right line and let's go and uh let's just put these variables
directly inside of here so I'm going to say name um and then call our teleport
type which is going to print out the message can teleport or can't teleport
depending upon the situation so teleport this is type is what we're working
with and then we call teleport makes it very easy to work with Warriors that either
can teleport or can't teleport and then we'll come down here and how can we
make it so that whoops let's make sure we throw our final quote in there how
can we ver absolutely guarantee that remember we're returning a double which is
going to be the block amount how can we guarantee that the warrior has zero damage
just return a really big number so we'll do ten thousand all right no chance of
where it's gonna take ten thousand in damage so we'll say else and in this
situation we are just going to call the block method in the super class and let it
handle every other scenario so again very very simple so we'll say base call
the base block function and have it do what it does and we know it already works so
everything's gold so basically
the only time we'll ever use this is if the randomization the random number
that generates tells us that yes we can successfully block this attack and now we
can go into program are we going to change a lot of code here since we added all
these new capabilities nope we're not going to change really anything except we
are now going to have uh Loki be a magic order so we're gonna say um let's
have him be magic Warrior magic Warrior and this will be magic Warrior magic order
nothing breaks and the only thing that's different here is we have the
probability of teleporting let's say that's 50 on top of that we know Thor
has is stronger than Loki so let's make him 75 percent uh he can take 75
percent less damage before dying but remember he has a 50 chance of of teleporting
away and not getting hit at all let's also say that Thor is more powerful with
his attacks than Loki so let's do that and then let's say that their
defensibility is roughly the same do I change anything else nope code's still
going to work everything's good even though we added that new capability and
now you can see that everything is much more interesting so Loki attacks Thor does
eight damage and then right here Loki teleports away Thor attack salokian does zero
damage because Loki teleported away the health is not affected and you can see that
they battle away sometimes he's able to teleport sometimes he is not
successfully able to teleport and in the end because of the teleportation ability
of Loki even though he was weaker and couldn't uh provide more damage to
attacks whenever he attacked Thor Thor has died and Loki is Victorious so go ahead
have fun with that if you'd like like you could do things like does Thor have
his hammer if he does maybe he does more damage with an attack and you could put a
multiplier on Thor's attack that increases it maybe Thor throws his hammer to
do a certain amount of damage and but you know maybe it's bonus damage but
whenever he throws it it like goes away and he has to wait for it to return you can
give either of them different weapons different armor different all kinds of things
I've done this tutorial specific I've changed it every single time I do it
but basically a version of this tutorial many times and people always have a lot of
fun and in the end ultimately have a very good grasp on object oriented programming
all right so now we're going to move on all right so now I'm going to talk
about collections now collections unlike arrays can be resized and there's a
couple of them that I'm going to cover here let's go and import some
information so let's go uh using system dot collections this is going to be
used for arraylist and also let's say using system collections and generic
that's going to be used for dictionaries and we'll get into covering
generics here soon enough so what do we want to do here well one thing I
haven't covered is region is actually going to provide us with a way to
collapse blocks of code so we can do something like region like this and then have
this be array list code or something like that and then end region so like this and
you'll see here in a minute what this actually does so and region all right and
then we'll be able to collapse these there's a little tiny thing right here
let's see if I can zoom in on it see this guy right here I can't click on
it when I do that but if I go like this see it just says arraylist code and I
thought I'd cover that okay so basically arraylists are resizable arrays that
can hold multiple different data types so they are extremely useful so let's
say array list and create one I'll just call this a list is equal to new array
list and of course they have numerous different functions available to them which
are very useful so let's say you want to add some different values to this list
well we can say a list dot pad and Bob and there you go you just added Bob now like
I said you can add different data types so we can come in and say add and 40. okay
so that's how easy it is to add them we can get the number of different values
that are inside of them by going right line and let's just say something like
count and get these values by saying a list and Dot count and that will give us our
total number of values stored inside of it I'm going to move this up here all
right and uh you can get the and another thing that's important to know is that
the capacity is automatically going to increase as the different items are added so
we can come in and say something like console and right line and capacity capacity
and this that and go a list dot capacity there's capacity and throw that inside
of there uh let's create another array list I'm going to call this a list
two um let's go yeah let's go to array list all right list two is going to
be equal to new array list that's good and then I can also come in and add an
object array so I can say a list two dot add range and new object this and then we
can go and throw in a bunch of values so we'll say Mike and Sally and egg just
to mix it up okay so we have all those inside of there we can add one arraylist to
another so we could say a list add range and a list two um we can also sort the
list if the types are the same so I'll say a list and let's do two dot sort
and you can also reverse them so a list two Dot reverse you can also come in and
add at a very specific position so these are zero indexed so if I wanted to insert
a value in the second index it starts at zero second would be one say turkey you
can also remove items so I could come in and say a list two dot remove at and
let's remove the very first one and likewise you could say list two you could
move multiple items so you could say remove range and 0 through 2. um you can also
search for a match starting at a provided index so let's come in and do console
right and uh turkey index and a list two dot index of turkey and zero and let's
run this whoops what's wrong with this oh I forgot it up parentheses probably
yes and run it and here we go and you can see we have our count which is two our
capacity which is four and the turkey index would come back as negative one does
that mean it's last weird um not really weird but okay is turkey the last index
oh maybe because I removed all these different items so I had a like let's try
let's try uh not removing all these so let's do this running again whoops I
can't do that um there we are running again there we are that still comes back
in negative one all right well either way okay so nah we'll cycle through and
then we'll see exactly what we have here it's probably the most important
thing to do or something that makes sense we can cycle through them as well by
saying for each and we'll do uh let's try object o in a list like that and
then let's just do console right line that works and save and run that and you
can see we output all those different values that we have right there Bob turkey 40
Mike Sally and egg okay um let's go and get rid of that what else can we do
here we can also convert an arraylist into a string array which is very useful so
we'll say string array and let's just call this my array equal to and then
what you do is you say string array is what we want to convert it's just like
we did everything in the past does this actually line up to array type of string
yes everything's cool so let's just do everything there awesome we can also
convert a string array into an array list so let's create one right like this
and we'll call this customers is equal to and Bob and Sally and Sue and then we
say array list and we'll call this customer array list is equal to new
arraylist like that and customer arraylist dot add range and customers is what we
want this to be so it says to have this be customers customers right like that and
there you go all right so pretty cool stuff there I'll run it oh that's not
going to do anything it's just converting it into the different parts all right
so there's a rundown of arraylists and as the the video continues we'll do
more with them but I'd also now like to talk about dictionaries now but
basically dictionaries are going to store key value Pairs and how you create them
is you say dictionary like that and then you define what you want to have inside of
them I'm just going to keep this format and then just change it so I'm
going to store superheroes in mine so I'll say super Euros is equal to new
dictionary and string and everything else there is good so to add superheroes to
this just go superheroes dot add and I'm going to use the key whoops of Clark
Kent and Superman like that and then I'll add a couple other ones so let's
go copy this and paste this down inside of here and then we'll say Bruce Wayne
Bruce Wayne and this will be Batman and then we'll do very West and the Flash
The Flash like that okay and if you'd like to remove a key value pair like
let's say the flash is going off the rails and we don't want him in our
super group anymore say very West like that it's Barry Allen who's Barry
West Barry West is uh is that the name of the new Flash Alan is that right I think
Allen's right you can tell me in the comments if any human being has lasted
this long in this video tutorial okay so that's how you remove them you can go
and get the number of keys you have inside so you can say console Dot rightline and
count and get that and Super Heroes Dot count and throw that on there and what else
we like to do well we could check if a key is present so we can do console right
line and
whoops Clark Kent and so what we want to do with this well we can commit let's
go down the next line we say superheroes I don't know why it's not picking
that up superheroes oh because I didn't close off the quote let's just
throw the quote right there now it would have shown up superheroes Dot and then
contains key you can see contains value also popped up you probably couldn't
because it was so extremely small but contains value also shows up inside of there
close that off with parentheses what else can we do we can get the value for the
key and store it into a string if we would like so we could say super heroes and
try get value and Clark Kent like this and then we could say out string test like
that and then what's neat is we could come in and say console and right line
and then let's go and get this so we'll say Clark Kent and test it's
our our value for us which is cool close that all close that off close it off we
can also cycle through our key value pairs so let's say uh we're going to
use four each for this like we almost always will do whenever we're working
with collections value pair and this is going to be string string string so
let's do all of those I prefer to call this item in superheroes that works and
then inside of here let's say console right line and go and get the key and the
value so we'll get this like this and maybe we'll output them like this and
then let's go down to the next line and the first one is going to be the item
key and the next one is going to be the item value oops and that's all
we're going to do for that Loop and also you're able to come in here and
clear the dictionary quite easily by saying superheroes Dot and clear like that and
there we go and we can run this well there was an error nope I don't want to
build from something that was incorrect um try get values what was wrong with that
try again oh let's try to get value I just I just went wrong there's that
run it and now you can see all of our different superheroes and remember we kicked
the flash out of the group and that's the reason why we only have two there and
you can see all of the other output we got from everything else that we were
working with so pretty neat stuff all right so there we go two collections down not
only talk about cues next now A Q is kind of like an old school type of collection
and the way it's going to work is it's going to be a collection that is
first in first out let's create one to see how it works if I can spell Q
properly all right so Q Q like that new Q all right I figured that out all right so
let's go and add some values so we'll say Q dot in Q like that and
we'll throw a one inside of there and we'll just add like two more values
so copy that paste and paste and we'll change this to two and this to three all
right well we can come in and check is an item in our queue so console right line
and one in Q and let's find out if it is and we do that by just saying Q that
part right there and this is going to be contains though so contains and we'll
look for one closing parentheses like that we can also come in and remove the first
item from a queue why don't we just do this so I'm going to copy this come
down here and to remove it you go DQ so DQ D oop Q like that and it's d e q u e
yeah that's correct oh no we don't we don't put any uh parameters into
it that's why it was giving me an error and I'm just going to call I'm
just going to say remove one or something like that and what else would we like to
do you can also look at the item but not remove it and how you do that is by using
Peak so we'll throw this here and I'll just say peak one and that's
fine and then this is just Peak exactly like this and let's come in and copy a
q to an array so I'm going to create a object array I'm going to call this
a number array and how you do it is you go Q dot to array and that's a format
that is very often used inside of C sharp so you should be kind of used to it
what's wrong with this Q's not in all here okay don't worry about that
and uh let's go and print the array you know you can quite simply just come in
and do right line and string dot join and you can say that all the individual
pieces are going to be separated with commas and number array like that and you can
also print out the items using for each so I forgot a parenthesis No Doubt there we
are all right and how we would do that is saying for each object in Q and I'm
just going to say console and uh right line and we will go and get all those items
so I'm going to say Q like that and oh which is our value so that and whoops I
don't need an extra parenthesis this time I forgot the quote so let's zoom
in get the quote and I thought I labeled that as o oh I labeled that no okay
we'll just object though there we are oops get that extra space out of there it
would still work even if I didn't have it there and see if you can take a wild
guess on how you how you would clear a queue I'll just give you the answer
right there all right and there you go and if we run that whoops I accidentally hit
it on the wrong thing let's go and do this all right we're in there it
works and here you can see said output one in Q true I removed it you can see that
whenever we remove a value it's passed back and remember we put one two three
in whenever we remove the second item we got two and remember the one is gone so we
only get two and three and so forth and so on so there is our output and up next uh
let's move on to the collection called a stack as you're going to notice
the collections have very similar format to how they work which is a good thing so
we'll say stack is equal to new stack like that don't have any arguments in
this circumstance so there we are and if you want to add items you use push instead
of Q so I guess that's not quite so uh quite so similar but copy and and
we'll change this to two change this to three okay so I'm going to do the
same things here I'm going to do a console right line and what I want to do is
I'm going to take a look at an item oh I didn't even tell you what a stack
is it's a last in first out collection okay so the Q is a first in first out
and a stack is a last in first out you can think of a stack like a stack of plates
if you made a stack of plates you're not going to pull the plate off the bottom
and break all the other ones all right makes sense all right so we don't want
this part right here that's how you convert it to a string by the way if
you're wondering I'm going to say that I want to Peak that means I want to
look but I do not want to remove anything so let's go um get that item out of
there and the way that you get it is you say stack dot Peak like this and
that's going to give you that and we'll throw that on there um I'm just
going to copy this because I'm going to do very similar things with each part
let's say that I would like to remove an item well we call this popping an item
so we'll say pop and we're going to pop the first one and you just say pop
and it's going to return the value that was popped whenever you call Pop play
this you can check if an item exists on a stack without doing anything destructive
to it listen let's just say contain one and then what I'm going to do
inside of this is change this to contains and one and it'll tell me if it was
or not with a true or false statement you can also copy a stack into an array and
object arrays are very commonly used for this so we're going to say num array 2
is equal to stack dot two array like that and what else can we do we can also print
using uh the separation it also just like we did previously with cues so I'm
going to say right line and then I'm going to say string let's go like this
and we'll say string and we're using join again just like we did previously
so just join and we're going to say what's our separator going to be
that's going to be a comma and then we want to cycle through this number array
too and output all of those values and of course we can use for each to go through
and we'll call this object o n stack there it is and output let's just do
like this and instead of this part right here we're going to come in and dollar
sign like that and say stack and this is going to Output all the individual values
that we have stored inside of there and throw in a comma or quote and there we are
and let's run it and you can see all of our output once again as we peaked
without breaking well then whenever we did a pop you see that the three is still in
there and then and you can also see the fact that we're getting last in first
out as I said previously so all good stuff and that is an overview of how to use
multiple different Collections and since I mentioned generics previously I'm
going to cover generics right now all right so now we're going to focus in on
an example that will demonstrate generics and we're going to do some work also
with delegates now basically anytime you need a many overloaded methods that differ
only by their parameter a generic is normally a great solution and what we're
going to do here is I'm going to create a new class like this and just for the
heck of it I'm just going to call it animal it's going to be doing other
different things though that have nothing to do with the animal I'm just going
to call it that okay so there we are we created this all right so what we're
going to do is I'm going to have this be class animal and I'm going
to give it a name so I'm going to say public string name get our Getters and
Setters and then I'm going to create a Constructor and it's going to be
past string name equal to and I'm just going to say no name like I said this
isn't going to have much of anything to do let's just throw that in there
just to get that done okay so now we're going to have our generic and this is
going to be a get some method some call this public and static doesn't return
anything get some and then how you define a generic is you are going to define a
letter that is going to represent this unknown type then you're going to go
reference T and we're going to have number one and also reference T the T
representing the unknown data type for your parameter that you're working with
all right so we got number two and then what I'm going to do is I'm just
going to take whatever was passed inside of here and I'm going to go double X
is equal to convert and convert it into a double and it's going to work for us
and I'm going to do the same thing for my number two so take this paste this
down inside of here and here is two and then I'm going to just output the
information so the right line and let's get this and just put in our our
information that we want to provide so there's that and then I'm going to
go double X Plus and Double Y is going to be oops have to change this to y whoops y
like that is going to be equal to and then we'll just go and perform our
calculation so I'll say double X Plus Double Y like that and that's all
it's going to do like I said it has absolutely nothing to do with the animal
but I just wanted to do this just to demonstrate okay so now what we're going
to do is we're going to go over into our program area and we'll start
playing around with this stuff and now what I need to do is if I'm creating a
list that is going to be a generic I need to Define my data type inside of here so
I'm going to say animal like this and then let's just call this animal list
is equal to new list of type animal and there we go another way of creating a list
of standard types is to just go list like this and int and don't want to call
this I don't like that name I'm going to call this number list number list
is equal to new list and I'm going to come in here and just add an integer
inside of here so I'm going to say add and I'm going to throw in 24. and
then I'm going to add some animals to this so I'm going to say animal list
dot add and new animal and throw in a name so we'll say something like name is
equal to and Doug seems good enough so let's keep that like that now let's
throw a couple other different animals inside of here so I'm going to say copy
paste and paste and then I'm going to name this other animal Paul and then the
other one is going to be called Sally all right so I got all of these inside of
here now I want to insert into a specific index like animal list dot add new animal
and um no let's put it into our first or into the number one index and I'll
say new animal and I will give this one the name of Steve like that okay good and
then if I want to remove at a specific index I can just say list and Dot remove at
and one so there's that and oops it auto-complete and put add inside of there
instead of insert which is what I meant okay so what else we'd like to do well
we could get our number of different animals inside of here so let's go console
Dot right line and get our num of animals and that is going to be stored in the
count so I'll say animal list Dot count like that and I can go and cycle
through all these different animals as well a in and we'll say animal list and
just output them on the screen dot right line and a name perfectly fine and now I
want to go and also let's go and run this just so you can see what's going
on here okay you can see that I just simply cycled through the animals and also we
printed out the number of animals which was three all right now I want to
demonstrate what I talked about previously which is generics all right so what
I'm going to be able to do is I'm going to say int X is equal to 5 and Y is
equal to four now with our get sum method we have inside here we can basically put
any type of data type in here that would make sense for summing these values and we
could also go in and make things that normally wouldn't make sense also make
sense in that situation all we need to do because it's marked as static is say
animal Dot and get some and then we have to Define after the get sum what the t is
going to represent in this circumstance with this specific method and it's
going to be an integer say reference X and reference y exactly like that now we can
do it for other different data types so let's do it with other types I'm
just going to copy this let's do it with strings so I'll come down and just
put it right here and in this circumstance I'm going to say string and I'll
say string X like that keep the five and a four and string Y and for these To Be
Strings however they need to be surrounded with quotes so let's put that there
so say like this the Hat I don't know why it's not doing that point okay
that's good and then in this circumstance when we call the function we just say
hey we're passing strings this time and it's going to automatically work
for us we'll say string X and string y exactly like that and you're going
to see that both of them work so save it run it and if we come in here you can see
five plus four they both work okay so it's what a generic is allowing you to do
is basically to go in there and be able to work with multiple different data types
now what I want to do is I want to show you how to do basically a similar type of
thing using structs so I'm just going to come outside of our main function that
we have right here and I'm going to define a struct and this is going to be a
struct called rectangle so we'll say rectangle like this and what you need to
do here is Define that it is a generic which I just did with that right there now I
can move this up don't need to do anything else and I'm going to say that
these are both going to be I'm going to have to define a height and a width
whoops didn't mean to do that let's just say private T and this will be
width and then private T again so that it can work with multiple different types
this will be length in the circumstance all right and then we can also do set up a
generic property so we can say public t width like this and we can create Getters
and Setters for it so let's go in here and manually do this we'll say
return width like that and then also we're going to do set and width is going
to be equal to whatever value we have assigned there oops value like that there we
are and let's do the same thing for our length so I'm going to copy this
and bounce down here paste that inside of there whoops this needs to be uppercase
for this property and of course this is going to be length like that and then we
just need to change these to length also so this will be length and the length
right here will be this guy so length there we are all right and now if you want to
create a generic Constructor what you're going to do is you're going to say
public wreck angle and Define t and let's just have this be W and T and have
that be l and we can come in and say width is equal to W and the length is equal to
l right like that and just to make this a little bit more useful let's go and
calculate our areas so we'll say public string git area and like that and then
we need to do our conversions to a double that we will be able to work with so
we'll say double and I'll call this double width is equal to convert and
two this is going to be double there it is and throw our width inside of there and
then we'll do exactly the same thing with our length so copy this paste that
there change this to length um length like that and this was going to be length
also good stuff and then what we can do is return our string with our calculation
inside of it so we'll say string format and dollar sign and go and put our
different information inside here so we'll have our width times our length is
going to be equal to and then we will use our converted form so we'll say
double width times R double length and is oops forgot my quote throw that there and
throw that there all right so that's good stuff all right so now let's come
back up inside of Main and let's mess around with throwing different data types
into this struct and see how that works so let me just say rectangle and this has
to be uppercase so rectangle like that and just like before we're just going to
Define what type T is going to represent in this situation so it's going to be
an integer I can call this Rec 1 is going to be equal to new rectangle like that
and then pass in 20 and 54 our width and our length then we can come in and
let's get that's some output here so we'll say right line and call Rec
1 dot get area right like that and like this and there we are and then we're
going to do the same but with a different data type and we're going to use
strings in this circumstance so paste this here and let's call this rectangle
two again the t is now going to be considered a string and likewise must put a
string right here and what we're going to have to do is to come in and have
these B strings of course so let's go get the rid of that and 50 and this will
be rectangle 2 that we're working with this time and let's run it and you
can see that we are
able to come in and perform those calculations regardless of the data type okay so
let's get rid of that and what I want to talk about now are delegates and
delegates basically allow you to reference methods inside what we call a delegate
object and the specifics is the delicate object can then be passed to other methods
that can call methods assigned to the delegate and it can also stack methods and
then it's going to perform executions in a specified order so I want to do is
I'm going to come down and create a delegate so we're going to go after
this closes off this struct so it closes off right here so there we go and I'm
going to say public delegit like this and what we're going to to do here is
declare a delegate type that performs different arithmetic operations so we're
going to say void and animal delegate no I don't want any of that so I'm
going to say void riff my tech like this and get to pass a double num one and
double num two right like that and then we can assign different methods to the
delegate so I'm going to say that I want to create public and static void and
we'll say double number one and double number two then inside of here we can
just output this information so I'll say console and right line and then inside
of here I will just output the adding of these two or the sum of these two values
so we'll say num one Plus num2 is going to be equal to and this will be num1
plus exactly like that and then we're going to do the same but we're going
to do a subtraction in our next situation so let's go get this copy and paste
this down inside of here and here I'll just say subtract so so subtract double
all the rest of that stays exactly the same except down here we're going to
subtract so let's do that and like that now what we can do is we can come back
up inside of Main figure out okay so right after this and let's go right about
here and we are going to say arithmetic arithmetic and I'll Define add and
I'll Define sub and add and sub whoops like that and this is pretty neat
because you can stack these and then have them be executed in order okay so I want
to assign just the add method to the object add that we just created so I'll
say add is equal to new arithmetic add and then let's go and add just sub so
I'll say sub is equal to new arithmetic there it is and subtract and then after
that I will perform the operation of adding and then sub and then using subtraction
so I'll say add sub is equal to add plus sub right like that and uh now we can
go and print out our results so we'll say console Dot right line and [Music]
add and we'll say six and 10. and I have to close that off this and then come
in here like this let's copy that paste that there and what I want to do here
is call both methods with ADD sub so again I'll say add and subtract and change
this to 10 and four right like that and then we can go like this and say add sub
10. and 4. and I forgot to come in here and change this so let's come down here
forgot to call the add so we'll go to six and ten right like that and if I come
in and run it it's going to call all those methods so remember before we zoom
in here 6 and 10 we're adding those so we should get 16 as an answer and then
here we're doing add and subtract so we have 10 and 4. so we're actually
going to first off add 4 to the 10 and then afterwards we are going to subtract 4
from the 10 you're going to get two different outputs as you'll see so we
can zoom in and you can see that's exactly what happened zoom in a little bit
more so we said we wanted to add six and ten we did we got 16 and then we said we
wanted to add the values but then subtract the values also and we got two outputs
just by calling one method so pretty neat stuff and we'll get more into that as
we continue but now what I want to do is show you a whole bunch of different
functions you can use to work and manipulate lists of data as well as show you
another example of using delegates all right just so I can show you another example
using delegates what I'm going to do is I'm going to create a delegate that
is assigned a Lambda expression I'll show you what a Lambda expression here is
in a just a second so I'm going to say delegate and double and I will call this
double it and it receives a double value all right this is really neat okay so what
we're going to do is we're going to come down inside of here and basically
what a Lambda expression is going to allow you to do is use anonymous methods that
Define the input parameters on the left and the code to execute on the right so
I'm going to say double it and double it and assign a Lambda to the delegate so
this is going to be equal to X and then it is going to basically multiply what is
provided by two exactly like that all right so what's neat here is I'm
going to say console and right line and then let's just come in here get rid of
that and I will say 5 times 2 is equal to and I'll say double it so double it
like that and pass Five inside of here and our closing quotes right like that I
accidentally put this in the wrong place so let's get this out of here and
throw it right here instead so there it goes and that looks much better let's
get rid of these Extra Spaces all right so there we are got that set up and
let's run it and you can see that we were able to come in here and perform that
calculation but it gets a lot more that's just like extremely simple version
let's go and make this a little bit more complicated you don't have to use
delegates by the way let's tap these in you don't have to use delegates I
can also just go do something like list and int and number list and say equal to
new and list and integer to find this and then we'll say one nine two six and
three and then I can put the number in the list and then what I can do is I can
create a new list like a list of evens just by putting only those even values into
my list so I can say VAR and even list is equal to num list dot where and here is
the input of a as it Cycles through the list and I can say a modulus 2 and Define
who gets into the list ultimately and then I can go and convert this into a list
throw that inside of there and I can cycle through all of these guys so I can say
something like VAR J in even list and then let's just do console line there it
is and output all of those values which will all be only the even values so
let's run it and you can see the audience dude it outputs those even values
which is 2 and 6. those are the only ones that are even out of those potential
values inside of our list now what I want to do is I want to add values in a Range
so let's come in like this and I'll just call this range list equal to take
our number list we had before and I'll say where and here's our input and
there it is X is greater than 2 or X is less than nine and then convert that into a
list so two list like that and like that and run it and why didn't that work oh
because I didn't output so I'll just cycle through this again so say for
each and copy and this time I'll work with our range list so range list range
list and output all of those values there we are run it and you can see we went
1926 and 3 as our output all right out of that now I want to do a more interesting
example basically I want to find the number of heads and tails that are random
randomly generated okay so I'm going to create a list and like this and
let's call this flip list is equal to new oops list like that and there and
they're going to be integers and I'm going to say int I equal to 0 random
Rand is equal to new random and I'll say wow I is less than 100 I'm going
to say flip list and add a randomly generated head or tail so let's say this
will be Rand next and I'm going to say one through three and I will just
increment the value for I in that circumstance now what I can do is after I
generate this I can print out the number of heads and tails so let's just do
this and I'll say heads but heads and get that value out of there so I can say
flip list let's put this on a separate line so that a little easier to read so
we'll say flip list and then I just use my where Clause again so I'll take
each of these individual values where a is equal to one then it gets put in the
list so I'll say to list and I specifically want a count for this and throw
that right there and then I'm going to do the same for my Tails so let's go
down here oops paste that in there and then this is going to be tails so s and here
I'll say where it is equal to two Okay so I got that and I got that so
let's run that and you can see that we randomly generated heads and tails and
we got 54 and 46. so just all kinds of really cool ways of interacting and doing
some pretty neat stuff let's do some more so now what I want to do is I want to
find I want to create a list and then I want to find all names inside of the list
that start with a specific letter so I'm going to go VAR and call this name
list is equal to new list and string and we'll have dog and Sally and Sue all
right so to find those let's say I want to find everything that begins with a
letter s so I'm going to say s name list is equal to name list Dot and then put
my condition inside of it so I'll say X and X starts with so now I'm going
to check whether this string starts with the letter S or not does it goes inside
and if not it doesn't so we can say for each and r m n name s name list and
then whoops I should have left that there there we are console and print those out
if we run it you're going
to see that it goes and shows us Sally and Sue but it doesn't show us doug
because his name doesn't begin with the letter s all right let's do some
more examples I'm going to talk about select now what select is going to allow
us to do is to execute a function on each item in a list so let's go and
generate a list from one to ten and I'll just call this one two ten is equal to
new list and int throw that right there and then I'm going to say one two ten
dot add range and I'll call numerable range 1 through 10. and we've got
that all right now let's say I wanted to get the squares for all these values
I'm going to say squares is equal to 1 2 10 dot select and say X like this and
then multiply each of the values in that list times themselves and then we can come
in and say for each and VAR and L in squares and then console Dot right line and
output all of those different values run it whoops what did I do wrong um oh one
two ten let's change that to one to ten save F5 and you can see that it did
indeed multiply all those values up to from one to ten by themselves and output
that value okay good stuff now let's talk about the zip and zip basically
applies a function to two lists so what I want to do here is let's say I want
to generate two lists and I want to add the values in those two lists times each or
to each other so I'll call this list one is equal to new list int and then new
int like this and no I don't want that there and um I'll go and say one and
three and four is what we want to place inside of there and then I'll create
another list that's almost exactly the same so let's paste this here and
change this to list two new list I want to have this before six and eight four six
and eight now I can use zip to add those together I'll say or some list is
equal to list one dot zip and then go list two and I'm going to get X and Y
values one of those values from each of it and it automatically did it for me and
then I can come in and say to list right like that and then we can go and output
that so we'll say four each and VAR item in some list looks pretty good to me
and then I will go and output this so I'll say console Dot right line and item
there it is run it and you can see that we were able to add values in separate
lists and create a brand new list from them pretty cool now let's say you would
like to come in and you'd like to be able to let's keep this part right
here you'd like to go and sum all the values inside of a list let's change
this into gnome list two and the naming conventions I'm using are just so
whenever you download the code you don't mess up anything all right let's
come in and I just want this part right yeah let's get rid of this oh let's
just get rid of the whole thing so here's our number list and I'm gonna say
one two three four five all right just to keep it simple and then I'm going to
say console right line but what I want to do here is sum these values so I'm
going to say I want to get the sum like that and how I can do it is using an
aggregate so I can say num list two dot aggregate like that and a and b and then we
will pass this inside of here let's throw our closing parentheses pass those
together and what it's going to do is it's going to perform an operation on
each item in the list and then carry the results forward so we'll say a plus b
because that is what I want to do in this circumstance and did I get all of this
right what do I need another parenthesis yeah and there that goes so let's run
it and you can see that we were able to successfully sum all those values all on
one line of code so good stuff we can also very easily get the average I'm just
going to leave everything here exactly the same I'm going to say average that
and to get the average you're going to say number list and then you're
going to have to say let's just get rid of all this so get rid of all well not
that ending part get rid of this and you say as queryable like that and then
average and it'll get you the average of those items inside of the list and
once again that works out to three okay what else can we do um well we can go and
uh determine if all items in a list meet a certain condition so let's say
here's our list we're just gonna use the same one and I'm going to say
all greater than three all right does our list meet those requirements well you
already know the answer to that but so let's come in and say all just get rid
of this all like that and let's get rid of average and then inside of all
we'll Define our condition so does everything meet this condition which is that
X is greater than three throwing another parenthesis and run it and of course you
as you know the answer to that is false another useful function um we can also
determine if any items meet a condition so we just use the same list again and
we're going to say any items greater than three and we just change the all into
an any that's all we have to do and run it and you can see in that circumstance
it comes back as true all right what else would I like to do um well we could come
in and easily eliminate any duplicates inside of a list so let's just do one
two three and then we'll do two and three inside of here so three and we use
the distinct to do this so we'll say distinct and here I'm gonna have to
get rid of all this so let's get rid of all that and it said we're going to
say string dot join and I want to join everything in here separated with commas and
spaces and then I can just say number list two there it is and call distinct that
and closing parentheses do I get them all there you are got it all run it and you
can see here that is only going to show those values and not repeat any values
unless they are distinct we can also go and receive two lists and return values not
found in both of those lists so let's say I have our number list here one two
three two three let's just leave it that way let's create another list and
copy paste that there and let's call this number list three and in this one
I'm just going to say three so let's say we want to find out um which
values in this list are not found in this new list down here call this accept
because that's what we're going to use to find it and we'll go string
join all perfectly fine and this is going to be number list two and we'll say
accept like that and number list three inside of here so number list three there we
go run it see those values also in a list one and two are not found in the second
list you can also go and return values that both lists have and you're going to
use intersect with that let's go and change this to intersect enter sect oops
sect like that and here we'll join them and I'll just say intersect enter
sect like this intersect like that and number three and and run it and you can see
that it shows that only 3 is in both of the lists okay so a whole bunch of
different functions for manipulating and working with lists and up next I want to
talk about innumerable all right so now let's learn about innumerables I have
my regular animal class I have my views many times and now we're going to make
a whole form so we're going to come in here and we're going to go to add
and class and let's make an animal form so animal form there we go all right so
what are we going to do inside of our Animal Farm well we are going to go and have
animal Form B or inherit from I innumerable and what that's going to do for us
is let's come down inside of here is we're going to create a list of
animals so I'm going to private list of animals and just go like this and then
I'm going to call this animal list is equal to new list and of course animal
all right and I should have just thrown this here there we go and there we go all
right so we have to create a Constructor for it so we'll say public and Animal
Farm and it's going to be past a list of animals and we are going to call this
animal list that sounds good whoops it didn't take it all right so animal list
and there we go all right so we got animal list passed inside of here and then we
just say this animal list is equal to the animal list that was passed inside of it
and then we can create also a empty Constructor animal farm and there we go so got
it okay so what do we want to do here well we want to create an indexer of so that
we can sort through our list and we can come right here and go to show potential
fixes and what do we want to do here go up to using system collections and that
error should go away and now it says it wants me to go and get the git emulator so
Implement interface and it's going to throw that in there for me which is very
useful and what this guy's going to do for us is it is going to return an
enumerator that's going to be used to iterate through our collection of animals
say return and animal list get enumerator there we go pretty simple stuff then like
I said before uh before we got all those errors that I wanted to create an indexer
for our animals so let's say public and animal and to do so you type this
followed with whatever you want the indexer to be called and then inside of here we
go and create our Getters and our Setters so here I'm going to return animal
and animal list there it is and fill this with index right like that and what else
oh we have to also throw a semicolon inside of here and then for our Setters
we're going to go set and animal list and insert and we'll insert index and
whatever our value is right like that okay so now what we want to do is let's
say we would also like to
be able to get the number of animals that are stored we can continue to add
functionality to this so we can say something like count and then we can go get and
return animal list count just that easily and for now I don't think I want to
add anything else to this so now what we want to do is go and actually test this
out so I'm going to go back into my program area and I'm going to create
myself an animal farm object so animal farm and I'll just call this my animals
is equal to new animal Farm like that and then I can add Animals by going my
animals and just start at our first index new animal and let's call this animal
Wilbur and let's go and add a whole bunch of additional animals so I'm
going to copy this and just throw in some more little guys so let's make this
second index and be Templeton Templeton and then in our next we are going to throw
Gander inside of here so Gander and then finally if you're not catching on what
I'm talking about maybe this will give you a hint Charlotte there's
Charlotte now if I want to cycle through these I can say for each ions animal I in
my animals and just output those onto the screen and I can say I and what I
specifically want to get is name which is set up inside of my animals all right so
let's go and we can run this and you can see that we are able to cycle through
Wilbur Templeton Gander and Charlotte all right all of our different animals in our
Animal Farm and this is a great way to create very customized collection types what
I'd like to do now is create another class so let's go into here and
we'll say add and new class and I'm going to call this one box so
there's box and click on ADD and now I want to show a bunch of examples where
we can use operator overloading and what that will allow us to do is allow our
users to interact with very customized objects in really interesting ways so
we're just going to call this clasp box and I'm going to create public and
this will be double length and Getters and Setters Getters and Setters there we go
and we're going to do the same for width and breadth is very interesting okay
so I'll throw this inside of there and that side of there and we'll go with
and breadth so the r e a d t h okay so we need to create a Constructor and there it
is and I'm just going to have it be exactly like that have it auto generate but
actually you know what rather than do well let's do that and we'll have our
length be equal to whatever um this is passed in so this is going to be length like
that and width and press yeah let's just leave it that way that works out
pretty good likewise we could come in here and say public and box like this and
then we can quite simply actually don't do it that way better way we can just
go like this and get a colon and this and pass in R1 values exactly like that and
this is kind of sloppy let's get rid of that let's bring this up here and
just have this be at the end just think it looks a little bit better okay and then
let's start adding some more additional functionality so let's say that we
would like to be able to add two of these boxes together how could we do it so we
could say static and box we type in operator and then the operator we want to work
with which is going to be just our regular edition operator and I'm going to
say box one and box two and what we can do here is Define exactly what it means to
add two boxes together well what it means is we're going to say box exactly
like what they did right here so let's just allow them to do that however I am
going to Define what's going on here so I'm going to say length of the
output box and it figured out exactly what I wanted it to do so that's good and
then we can say width and it's going to add those so pick the most logical to
do and automatically generated the code which is cool cool and there we go and that
is a great way to add those together and then we can come in and just say return
and this will return a new box with all of those formatted for us I named this box
so that would be box all right cool and we can also do the same thing basically uh
let's get the total list of different things different operators we can add
here so there is a list of different things we can Define for operator overloading
I'm going to show you a couple more so let's go and show how we're
going to subtract them so we're going to say again static returns box we Define
the operator we want to work with we want to be able to subtract them so I'm
going to say box and again box one and box and again Box 2 right like that then
what we can do is again we're going to generate almost at precisely the same
exact thing so I might as well just copy this come down inside here paste that in
and what we're going to do here instead is subtract so we'll just subtract
them right like this and return those values once again so how do you define if
they are equal to each other why don't we just copy all of this right here
we'll just say copy and you're basically just defining your own rules so
what we want to do is the operator we want to use this time is that equality
operator and we're just going to say inside of here if and Define our rules so
we're going to say box one dot length is equal to whoops I think it was going
to do it for me yes it did suppose the length and the width but I also want why
don't we go and put this here go down to the next line and also say and again
and it wasn't quite smart enough to figure out that I also was very interested
in breadth being equal that's okay so we'll say breadth here breath and
breath like that so all of those things are equal if they are well in that
situation we want to not return box one we want to return true and otherwise we
want to return false and have that all set up and oh this is a bull not a box
that's why it messed up I messed up all right Visual Studio never messes up
only I messed up now if you want to check for inequality we're going to go like
this paste this inside of there and we're going to say not equal to like that
and then we'll say if the Box length is not equal to so we'll just throw
not equal to in every single one of these and it's not going to be and they
don't have to all not be equal to just one of them so we're going to say or
for all of those and again do the same exact thing you also are going let's
make sure I don't okay yeah you also can Define how your object is converted
into a string and how you do that is you can say over ride this returns a string
and we are overriding to string and what we need to do here is return and then we
need to format this so we're going to get rid of this whole entire thing like
that and go string dot format and output is box with height and go and get that
that specific height and then we'll just say width and get that specific width
and then we'll say and Brett and get that specific breath so this is a way to
customize your string outputs and then of course we can just come in and go link
length with and breath like that and there's customized output you can also
convert from a box into another data type so let's say we wanted to convert
into an integer how can we do that well we can just say public down static explicit
operator and int and if you're past a box you want to convert it into an
integer that's how we can do this and looks like I accidentally deleted yes one
of my curly brackets and then what you're going to do inside of here is just
return and like this and we're gonna sum them together and then divide by three
so we could say something like B dot length and we're going to add them so we
could say B times width that works and then it doesn't like breadth for some
reason so we'll say B and breadth there's breadth and then what we're
going to do after this is divide by three we'll say divide by three I don't
know I'm just doing something to do something you can also convert from an
integer into a box so we're going to do implicit instead of explicit so
we'll say static implicit Operator just like before and box and we'll say
int I oops get rid of that and then what we can do is return a box with the links
all set to whatever integers passed because I don't know that seems to make
sense so we'll say new and box and it'll just be whatever they passed okay
there's a bunch of interesting ways of overloading operators to do some pretty
interesting things with let's go back into our program section and mess around
with this so I'll leave that there it seems fine I'm going to create a new
box and we'll call it box one equal to new box and I'll say two three and
four just to be simple create another box after this so go copy paste that in there
let's call this box two just to be original and now let's have this be five
six and seven five six and seven you want to add them together now with your custom
object you can do so so you can say box and let's just have it be box three
that seems perfectly fine and I will just add my boxes together and it'll just
work box one plus box two and now we can add boxes let's go and convert the box
into a string with our two string override that we went and created so right line
like this and go and output this I'm going to say box three and pass in box
three and it's going to automatically generate that string of text that we said
that we wanted before and also let's just copy this let's call the
conversion into an integer just so you can see what that looks like paste that
there and I'll call this box as an integer and to
get this we just go and get our value and then we say that we want it convert it
into an integer and of course this works with the other different data types and
like I also created we're going to be able to convert it in into a box so we
can say box 4 is equal to and we'll say box of course this is going to cause
some manipulation so there's box four that's how easy that is to do and
let's go and run this and if we do we can see that we were able to get our
custom string generated for box three and also we were able to convert our box into
a integer which was also interesting and another thing is that sometimes you're
going to feel the need to build a simple class that contains fields and Anonymous
types are going to work great in some situations let's pull out the old
Shopkins from the past and say I'm going to create new and I'm going to say
how much a Shopkin is worth it's a toy if you don't know what a Shopkin is
so Shopkins and if I can spell Shopkins yeah I got a little extra B in there and
we're going to say that a Shopkins cost 4.99 so 4.99 exactly like that well
we're going to be able to go and output these so we can say console and right
line and we can just simply say that we want the first the name and the price for
the Shopkins so we'll say cost and uh dollar sign and there we go and go and
get those individual names so we'll say Shopkins dot name and Shopkins dot
price and there we go and what's cool about these is these Anonymous types can
also be stored inside of an array so we can say so we want to create a toy array is
equal to new right like that and then we're going to Define this by saying new
right like this and then go and Define a name let's say yo Chi these are
playing cards again toys whoops what I accidentally do there accidentally jumped
way up there let's come back here to this and Define a price equal to and
we'll say this is 4.99 also and then after this I have an extra parenthesis
inside of here so we'll say new and get rid of this parenthesis oops
accidentally did that and then let's go and do something similar so we're
going to say new just copy this because there's no point in retyping it say new
grab this whole thing right here come down to the next line paste whoops let's
move that new up here so it's a little bit neater so new and we're gonna do
another of my curler brackets and name and maybe we have Legos or something like
that okay so there's Legos and they're 9.99 of course they are because
Legos are expensive there we go all right so got those all set up and now what it
can do is I can cycle through them just like anything else I'll say for each
and VAR item in our toy Ray and output our information so we'll say console Dot
rightline and then inside of here let's go and just copy what we did before so
we have all of this this is all going to be the same and we'll just change it
the item instead so paste this inside of here and then we'll change this to
item that and item like this is there anything else I want to do I don't know I
think that's a good enough example and then we can go and run it and we can go
and look at our output all right and you can see how we were able to go and create
those lists and go and output that information all right so good stuff all right so
now I want to talk about Link and Link stands for language integrated query and
basically it provides a whole bunch of different tools for working with data and it
is quite similar to SQL but it can work with data aside from just working with
databases and let's just come in here and let's just do a couple little
examples so let's say we want to create a string array and let's call it
dogs is equal to and I'll throw in some like famous dog names so I'll say
canine and there you go so there's no reason for you to watch me type all that
out so there's some different famous dog names now what I'd like to do is I
would like to get all the dogs that have spaces in their name so I'm going to
say VAR and dog spaces is equal to and you say from and this is a reference to
where you want to pull data from so let's say uh well let's mark this as
dog what's this is dog in dogs array that we have here and let's go and put
this on a separate line as well I'm going to say where dog I'm saying each
individual dog is going to contain what I said I want dogs with spaces in their
names so there is a space and then I want to order by see it looks just like a SQL
dog and this will be their name in descending order and then I want to say select
dog all right so here is our example code now what I can simply do now is if I want
to cycle through all of my results I just say four each and VAR I uh in dog spaces
and we could output all these names everything that matches anyway so console right
line good and I that is all we need to do save that and run it and it is going to
list only the dogs with spaces in their name okay and we can get much more
complicated than this so let's go and get rid of that and let's get rid of
this also so there we are and for our next example what I want to do is actually
create a function so I'm going to say function right here and I'm going to
call this static int and it's going to return an integer array I'm going to
say query and array and click this saying like this okay so what we want to do now
is I want to create an array of integers so I will just let's just get part of
this all right so we'll go like this I'm going to call this Noms equal to
and then I'm just going to throw some values inside of here so let's say I
want to say 510 [Music] 15 20 25 30 and 35. there we go so I have all of those
values stored inside of that array now let's say that I would like to be able
to get all the numbers that are bigger than 20. how could I do that let's get
VAR and greater than 20 is going to be equal to I'll say from Nom temporary
holding space and in Noms which is the array we're working for it with then
I'm going to say where number is greater than 20. and I'll say that I want
to order by num and then finally I am going to select num here we go now I can go
in and cycle through those results again just by saying for each and int num in
nums uh that's not right I want to get uh greater than 20 gt20 T20 and then I
can go and output all of those so we'll do console right line and no exactly
like that and whoops where do I have a bug at oh I know I did call this so there we
go and uh I'm going to also generate a couple other little examples here and
also I have to return an integer that's the reason why we have an error okay so
what do we want to do let's say I'd like to get the type for this if I
could come in and say console and right line and then I'm going to say that
I'm going to get this type so get type like that gt20 get type and anything
else oh yes I want to close off those quotes uh let's say that I'd like to
convert it into a list or array I could say VAR list greater than 20 is equal to GT
20 dot two list and I have to designate this as an integer and then like this and
like that and I could say VAR array greater than 20 is equal to and I'll say
gt20 and convert this into an array like that there we go um if you change the data
in the query just understand that it's going to automatically update which is
also good so we could say Noms 0 equal to 40. whoops I forgot to hit the equal sign
equal to 40. and we could go and cycle through those values once again so let's
get this and paste that right there there we are and then finally we can return our
array which is what we designated we would return after this was done so we'll
come down here and we'll just say return and array gt20 exactly like that and
there we go and now what we can do is just simply go and call this function and
then we can maybe cycle through some things or something like those ideas so
I'm going to come up here and select this so I don't have to type it again
go down inside of Main and then I can say int like this and let's call this int
array is equal to Quarry into array and there we go and then I could go and cycle
through those values again so let's just copy this guy right here and paste
that inside of there and this is going to be intera so we'll say it array like
that and then this will be number so we're gonna like that I don't think I
have any errors no I don't and you can remember greater than 20 and what did I
have let's close this so we can look at it first whoops 5 through 35 and we
said we want everything greater than 20. let's go and run it again all right
and if we cycle through indeed greater than 20 didn't say greater than or equal
to it said greater than 20 we get all of those different values and you can see
that this is represented as an innumeral and also we were able to return all of
those additional values that were passed inside of there and you can also see where
the 40 is where we inserted the value and how that also automatically was updated
which was also very useful let's do another example I went and again I used my
basic animal class that I've been using over and over again you could pause
your screen if you'd like to go and type all that and of course all the codes
on GitHub can I get everything in one place well you can pause your screen there
and then pause your screen yeah that's everything so there's that and then
also I went and created another class and it's just simply called owner so
there's that you can pause your screen and type that in if you'd like leave
your curly bracket
in there though don't get rid of it like I just did okay so you have all those
set up hopefully you got that all right so now let's go in and do another
example now what I'd like to do here is go and create an array list family and
lay of eggs or something like that is equal to new array list and then what I'm
going to do inside of this is I am going to throw in my curly brackets and I'm
going to go and generate a whole bunch of animals and store them in this array list
so I'm going to say animal and I'll Define name is equal to Heidi and
whoops ID there we go and I will designate that the height is going to be equal to
0.8 and the weight is going to be equal to 18. and there we go I'm just going
to plug in some additional animals here just so that we have everything maybe put
some equal spacing there and then what you have to do of course is put a semicolon
here at the end now what we're going to have to do is come in and convert this
into an innumerable so we're going to say VAR man and numb like that is equal
to Bam animals dot of type and this is going to be animal then I can go and query
this so I can say small animals so let's say that I want to get animals who
have a weight that is less than or equal to 90. so I'm going to say from animal
in bam animals enum this guy right there I'm gonna say where animal dot weight
is less than or equal to 90. and order by I'll say animal name because it's
more specific and select animal someone it's almost exactly like SQL it's
just basically the main difference is that you are flipping the select at the end
versus the beginning so we'll say four each and VAR and cycle through all these
different animals in small animals and there we go and then what I can do is go and
get the name and how many pounds each of these animals weighs so let's go right
line and then I'll say oh like this zero ways and go and get the number or the
actual weight and I'm going to use pounds in this situation and then I can say
that I want to get the animal name so there's the name and I want to get the
animal weight which is right there and there we go and there's another example
of how we can cycle through and uh do more complex little ideas and you can see
here that our animals whoops the wrong button Congo weighs 90 pounds and Heidi
weighs 18 pounds okay so there we go and another example I want to do this time um
let's change these up a little bit let's make this a um let's just make
this a animal list animal list and I'm mainly changing the name only so that I
can keep everything consistent in my code that people download so I'm going to
say shepherd German Shepherd and height I'm going to set this for 25 and the
weight for 77 and I will make Chihuahua I think that's spelled right oh boy
what'd I do let's get rid of that there we are okay Chihuahua there yeah
don't need that eye inside of there and uh little dog so make this seven and
make the weight on it be 4.4 and then we'll do a Saint Bernard so Saint Bernard
Bernard I think that's right and uh make this 30 and make this weight of 200.
there we go now what I want to do is I want to find animals where the weight is
greater than a you know large number as well as the height how do I stack those
let's call this big dogs so big dogs and I am going to go and cycle through all
of these so I can get rid of this I'm going to be cycling through them so leave
the for each inside of there even though it's giving me an error right now no
big deal so I'm going to say from dog in animal list and where and to put in
multiple conditions you say dog dot weight greater than 70 and then you just say
and and I'm going to say dog dot height is greater than 25 like that and then I
can do order by and I'm going to order by the dog name and then I can just do
select dog like that all right and then whenever I go and output this I'm going
to say I'll just leave that as animal and just call this big dogs so big dogs
and then I'm going to say a and it's going to give me a name ways and there
is the number or the wait for it then this is fine and the weight I could also
print out the height if I would like to but I'm not going to and can go and run
whoops okay what do we have an error out here animal list animal list says animal
list right there I forgot to turn this into an innumerable that's exactly how
we do that and let's go and run it and you can see here that the Saint Bernard
is the only only animal that qualifies for our condition all right let's do
another example all right so this time what I want to do is I want to go and create
another list which is going to be owners and I want to match those up with dogs so
let's come in here and let's create a couple more animals so I'm going
to go and copy this let's go and copy this whole thing like this and paste this
right here and also I'm going to say that I want this to be called pug POG and
a Pog is going to be 12. and 16. and then let's throw one more animal in so
we'll throw this in and I will say that this is going to be a Beagle dog and
we'll say 15 and we'll say 23. all right so there is 23 and then on top of
that I want to add some additional information so that I can match up owners which
I'm going to create here in a minute with all of these different animals so
I'm going to give all the animals an additional piece of information which is
going to be their animal ID so animal ID and we'll say that this is id1 and
let's go and just copy that right there so we'll copy this and come down
inside of this area and paste this here and this animals Animal ID is going to be 2
this and also we'll add another animal let's have this be owned by owner
three and let's just get these consistent and then we will also come in here
and for this animal ID let's have this be owner who owns the Pug let's just
say one and let's get this out of there and then for the Beagle I am going to
say that the owner of the beagle dog is going to be whoever owner two ends up being
all right good enough all right so now what I want to do is I want to go in and
create all of these owners and if you don't remember I went and created this
class right here for owner and then here is the animal class also so let's go
create some owners and then let's tie them up with the dogs using an inner join
so I'll say owners is equal to new like this and I can Define all of my owners
so I'm going to say that I want a new owner and the owners oh curly brackets
name is going to be equal to Doug parks like this and yeah this is an important
part you have to put in the owner ID otherwise you can't match them up with the
dogs so there we are and then we will do pretty much the same thing that we did
previously all this is going to be owner not owners and 1.0 and copy this and paste
this inside of here and then this is going to be Sally Smith so Sally Smith like
that and she's going to be owner two and then we'll come down here and
we'll also add in another one and this is going to be Paul Brooks and he'll
be owner three all right and don't forget your semicolon down here all right so
let's say that I would like to come in and remove the name and height I could
say Vore name and height is equal to from a in animals and I could just designate
select new and then a DOT name and a DOT height just as you can experiment and do
test all kinds of other different things what did I do wrong here um uh combine
this owner's list with our animals list but I also want to show how to convert
to an object array so you can also say array array name and height is going to be
equal to name and height dot two array there we go and I could cycle through this
so just to show you what it looks like so I can come in let's just call this
VAR I and then we're going to cycle through this guy right here so let's
get it and paste that right there and I'm going to say for each and we will
output just let's just keep it very simple and just output all of that
information so let's get this and convert this to a string string like that and
I need another parenthesis there we go we can cycle through whoops I've got
another error let's figure out what that is oh that's right I meant to
actually have this be set up as a animal array so let's just change this to
animal and array there we are and then this will just be new and array like this
like that and oh I forgot to put animals okay now that should work and let's
come down here and this needs to be uppercase because that's the way they are
in a class and I think I got rid of all the errors okay so let's run that and
there you can see we're able to come in and get all of that information in
regards to the name and the height for said animals all right so cool stuff all
right let's do what we were originally planning on doing however which is
actually going in and combining the owners with the animals and let's just come
right here and we'll use the for each this time to cycle through all of our
additional information now if I want to be able to join multiple different tables
what I'm going to have to use is an inner join and the two that I want to join
are the or I guess their arrays is the owner array and the animal array so to do so
I'm going to say VAR and I'm just going to call this inner join because
that's what it is and I'm going to say from animal in animals join whoops
join owner in owners on animal ID because those those are going to be the two
fields that both of them share where the owner Dot owner ID and then we will
say oops there we are select new owner name equal to the owner dot name and animal
name equal to we can just yeah we can just throw it here equal to the animal's
name all right and that's going to be how we're going to be able to join
those now what I can do is say 4 h i n and I'll just call this inner join then
what I want to do is output the information in regards to which owners own which
dogs or yeah they're all dogs and let's just have this be like that okay so
console and right line and I can come in and this is going to be the owner's
name and I'll say owns and then this will be whatever the dog's name is and
then under here I'll say I Dot and I want the owner name and also I dot I want
the animal name there we go and now we can run that and we can see that we're
able to get both the owner name if I can get rid of that one irritating thing
let's just get out of this all together oops all right let's run it again
and okay this time it works so there you can see Doug parkzone's a German
shepherd and blah blah blah blah blah all right so there's all the information
that we were able to do or pull together by combining those multiple different
tables now you can also create a group inner join so let's say I wanted to get
all animals and put them in a newly created owner group if their IDs match with our
owner's ID so I'm just going to put this right here I'll just ignore
the inner join here in this situation and I'll call this group and join and
that's going to be equal to whoops that's fine I can say from owner in and
I want to order by owner dot owner ID and join animal and animals on the owner dot
owner ID where we have an equality so equals animal Dot animal ID into and I'll
call this owner group and then we're going to say select and stack these by
saying select new oops spell select correctly select new and then I can command and
go owner equal to the owner name and animals equal to from owner two owner two and
owner group and order by owner to name and select owner two this is where
that's designated and in this for instance put a semicolon down there and
let's say that I go and I want to track total animals so I'll go and total
animals equals zero and then I want to cycle through these different owner groups
so I'm gonna I'm Gonna Leave This for each block here also and then
let's do like a console right line oops like this just to put a space and then
I'm going to do another four each but in this situation I have to say four each
and let's move this up here whoops accidentally put a space in there for each
VAR owner group like that in group join and down here say console dot right line
and let's output the uh owner that's stored in the owner group so I'll
say owner group there it is Dot owner and then for each I can cycle through all of
the animals owned by that owner so I can say for each VAR animal and owner group
animals there we go and I'll put that and I could do something like total
animals and increment that and then on top of that this is just getting a little
crazy just for fun I can say oops absolutely cap lock and I can do right line and
let's say that I want to Output I'll say star and go and get the animal
name on top of that so you can see we can do rather complex examples here all right
looks good and I don't see any errors let's run that see what that looks
like so this is pretty cool so let's cycle through here you can see our
original information in which we see ownership for each of the individual dogs but
there's some duplication there with the owners in the second situation we are
outputting the owner's name and then the dogs that each of those owners owned
all with little bullets next to them so pretty cool much better example than what
we did previously so there's a rundown of a whole bunch of different things we
can do with link and I might come up later on in the tutorial as well and up next I
want to talk about threads all right so with threads you can basically execute
multiple pieces of code that are going to share your resources and the data also
and uh what we're going to do is I'm going to take you through a whole
bunch of examples to demonstrate them so what I want to do here is I'm going to
randomly just print either a one or a zero okay very very simple so I'm going
to say thread and I'll call this T is equal to new thread and I'm going to
call a function some sort of function all right so let's create a function here
come up here and all it is going to do is call a print one and then what is it
going to do what is our amazing function print one going to do it's going to
have a for Loop inside of it and I is equal to zero and uh let's let's
print a thousand oops not ten thousand one thousand and there we are and all
it's simply going to print is the number one all right so there we are pretty
simple function so what are we going to do now well inside of Maine I am going to
also let's call print one so we're a secret one and and we're straight
out of the box whenever you're working with threads it's important to
understand that you can't guarantee when your thread is going to execute and
that's basically the whole point of this whole entire demonstration so I'm
going to start the thread that's going to try to print a thousand um ones on
the screen and then what I'm going to do is I'm going to start another for
Loop there's I might as well just copy this so let's just copy this and
paste this inside of here and what this is going to do is it's going to print
zero that's it all right there's your thread example and we can go and run
this and there we are whoops maybe let's do um let's get rid of the right
line and let's just do right I think that'll look better so that and save
it and run it again okay so here you can see all right so what did I do I said that
I wanted to call print one first right and I wanted to print a Thousand Ones well
you can see that that is not necessarily what happens because basically when
you're working with threads in this circumstance they are sharing resources so
we start off print ones and then the zeros click in we printed all of them and then
we continued trying to print the rest of our ones thereafter and you can just see
we're sharing resources here pretty simplistic example now we'll take it up
to a Next Level and I'll demonstrate how sleep works so basically what sleep is
going to do is it's going to suspend execution for a certain period of time so
let's create an integer just call it num equal to one and then let's create
another I didn't save that for Loop did I yes I did all right let's just do
this to 10 and then I'm going to do console and right line and I'm going to
Output my number and then I'm going to come down here and I'm going to say
that I want to pause for one second look it did it for me then I'm going to
increment my value for I and then maybe after this I want to say that the thread is
it ended execution so we'll just come in here and we'll just say thread
ends exactly like that and we don't need to do anything else we can just save
it and run it and you're going to see that even though I'm slow that it is
slowly every one second printing out an additional video or an additional value
might stop because of my thing that I'm using but there are all of the values
printed out with a one second interval now what I'm going to do is take this up
to another notch and I'm going to simulate using another keyword called lock or
another command called lock which is going to whenever a thread is executing lock
is going to block another thread from executing until the other thread is done but
we don't need these guys anymore owner and animal let's get rid of those
yes I don't want that and owner delete that's from previous example and I
am going to create another class and we'll come up here and I'm going to
say I'm going to simulate transactions at a bank so I'm going to create a
new class and let's call this just Bank like that all right so there's bank
account what are we gonna do with our bank account well I have to put my um lock
inside of here so I'm going to come in and I'm going to say private object
count lock and then we're going to withdraw money from our account from this
guy all right so we have that and what do we need for a bank account well a balance
would be something that would be useful and uh just generate my Getters and Setters
for that and also have a name and I'll generate Getters and Setters for that
whoops there it is okay got that and then we're gonna need a Constructor so
we'll say public and Bank account and it will be passed a starting balance
that's good and we do not need anything else here what is it it's trying to
give me all the other stuff don't want all that other stuff I just want my
balance set inside of here and then whenever this is created it's going to be
assigned to the balance and then what else do we need well we need an ability to be
able to withdraw money from our bank account so this is going to return a value and
we'll just call this withdraw and then we'll say how much do we want to
withdraw from this account to have an amount and this is going to be a little bit
more complicated I'm going to say if and balance minus amount is less than zero
then what am I going to do with it well I'm going to say that I want to stop
that and put an error message on it so I'll say something like let's return
balance
after this is done well let's just do this and then return balance to the user
but inside of here I want to do it like an error message and it is going to say
something like sorry and another dollar sign balance in account so it's going
to Output our balance that we have in our account and say like hey you can't
take that much money out whoops let's get this cut that out of there throw that
there in account let's get that extra space out of there all right and uh oh
there's the excess let's show that there okay so there you are there's
our error message and then it's going to return a balance now if we don't
have that problem that means we can take money out of the count so what we're
going to do is we're going to say lock account lock and then go and perform the
operation so I'm going to say if and balance is greater than or equal to amount
then let's go like this and I'll have another message print out here so
let's do let's do right line and I'll say how much money was removed so
I'll say removed and how much money and then I will say how much money they
have left which is their balance left in account like this and then we can come in
and we can go and get the amount and then after that we go and get the balance
minus the amount that was withdrawn just like that then we have to go and decrement
this so I'll take that and I'll just throw a minus sign in there to
decrement that and then after we do this once again we are going to return our
balance amount so there is our balance what else are we going to do well we're
going to want to allow the user to easily withdraw a dollar from the account so
I'm just going to create another method here let's get outside of
withdrawal and all it's going to do is whenever you call it it's going to
automatically withdraw one dollar so I'll say this is a poor person here
we're dealing with uh issue withdraw and like this so it's not going to
return anything and we're going to just say withdraw will say we want to
withdraw one dollar all right that's all we need for our bank account so now
over inside the program is where we're going to do the more complex stuff so
very first thing I want to do is I want to create a bank account and I'll just
call this account equal to new and bank account and what we're going to pass
inside of it is ten dollars that's all we have in our account we're very
poor okay so what we can do here is we can actually go thread and I'm going to
show you a whole bunch of side things you can do so you can go current thread dot
name is equal to and Main like that and current thread is going to provide a way
for us to get the current executing thread then I'm going to create a whole
bunch of other threads so say in I is equal to zero and let's create 15 of them
so they're going to try 15 different threads they're going to try to
withdraw one dollar and we only have ten dollars in our account now it's very
important to understand you can only point at methods without arguments that return
nothing so will say thread T is equal to new thread and new and then what we're
going to do is say thread start and call account and issue withdrawal you know what
that does because you just saw it a minute ago and there we are okay so then
we're going to say t dot name is equal to and convert this into a string and
get this like that there we are and then I'm going to say threads this array is
going to be equal to T right like that but I'm going to have to create this so
we'll say thread array is equal to threads because that won't make sense
and there we are and we said we wanted to have 15 of these all right so 15 of them
good all right so go to all that set up and this is going to be threads not thread
start threads what was I thinking okay all right threads there we go now I got it
all set all right so now that I have these threads what I want to try to do is to
execute them so I'm going to do another for Loop and we're gonna do
basically the same type of thing here so let's just copy this guy right here
and paste that inside of there and what we're going to do is we're going to
check if a thread has started so how you do that is let's just output this so
we'll say console and we will say whoops right line and I'm going to say
thread and this is going to be the thread's name so we'll say alive and
this could get completely crazy if we weren't dealing with smaller dollar
amounts so to get the thread's name I'm just going to go threads I and name
like that and then I can command and find out if the thread is alive by saying
threads and I like that and calling is alive like that and there we go that's
going to provide us information in our output in regards to what's going on now
we need to start the thread so we'll say threads and let's just keep that
and that and just change this to start like that and then we can also come in and
check if the thread has started again so let's go I'm just trying to get a
lot of output on the screen here just so this right there so there we are again and
those are just going to execute on their own and then what we can do let's get
rid of this excess line we can come in here and what we can do is we can get our
thread priority now normal is what is called the fault um basically changing the
priority however isn't guaranteed to even work but uh it's really best not
to really mess with this but what the heck I'm just gonna output this just so
you can see so I'm going to say current priority like this and change this to
zero and then to get it you just come in and all that and we just say thread dot
current thread dot priority like that and there we are and then what we can do is
also output I have that saved whenever our thread is ending so we'll say this
and we'll say blank and this is ending like that and then we can go and get our
current thread then I type that out here somewhere where did I type that um yeah so
it's going to be thread current thread and then I just want the name is what I
want to output and so we'll get rid of all this put that right there and then
I'll say that I want the current threads name I think I did everything right
there so let's get that there save that if we go and we run it whoops
what'd I do I have an error I think I have an error do I um do I have an extra
or my missing one yes missing a curly bracket save that and let's run it and
you can see how the threads are going to come alive here so we'll come down
here and then we gotta start getting crazy errors but basically we can see here
thread zero alive and it's cycling through and turning those threads on so we
go zero one two three and then we have the uh the first act so we created a whole
bunch of threads here but then it thread one then goes and takes one dollar out of
the account you can see we started creating more threads then we removed one dollar
eight dollars left and continued moving and moving and deleting and taking money
out of the account until we get to a point where there's no money that is even
left in the account let's zoom out of here so we can come down a little bit
further and then what you're going to see is let's go back in here is uh us
trying to take money out of the account whenever there's no money left and then
we just continually get that there is no money in our account okay so they're
taking their turns jumping in there and trying to take money out of the account
even though there is none left all right and that is sort of the process of going
through and allowing each thread to take its time executing now you can come in and
actually set it up so that you can pass arguments to a thread and we're going
to create a function that's going to allow us to do this so we're going to
go static and void and I'm going to call this count two and it's going to
be past a maximum number we want to count to and let's go put this inside of
there let's go and create a for Loop and say int I is equal to zero cycle as
long as I is less than or maximum number go like this and then we can come in and
console and right line and then in this situation we're just going to convert
this we're just going to print that value out because there's nothing else
really to do there okay so we got that little guy up there and it is just simply
counting to a maximum value what we're going to do now inside of main is go
thread and I'll just call this t equal to new thread and I'm going to use a
Lambda expression to pass it's actually pass arguments to this thread so
I'm going to say new thread and then inside of it like this and we will do
nothing there so let's just close that off and have this be like this and then
we can count to we can call our count to function and then we can pass a value of
10 as our maximum number into it and then we can say hey let's get going start
that thread up rid of that and there we go and another thing that's neat is you
can use a multi-line lambdas so we can say new thread and throw another one inside
of here is this and then we will just simply go like that like that and make sure
that you close this part off actually it's going to be start at the end of this
so we'll say start like that and there that is and then we can command and we
can count uh to multiple different maximum numbers so I'm going to say count
two and we'll try five and then we can do count to six also out two whoops
let's mess that up there we go count two one six that looks good all right and
then we can come in and we
can execute this and you can see how we were able to print these out so I said
count to ten count to five count to six that was what I did right and oops
let's go and Escape out of that get this over here all right okay so count to
ten did it count to five did it count two six did it so there we are we're able
to go in there pass those values inside of there and mess around do some neat stuff
with threads and of course just like anything um the more you practice with threads
the better you will get at them now what I want to do is I want to come in here and
show you how to mess around with directories and files you might get an error
however unless uh you might want to start visual studio with Administration rights
if you do get any type of error where it says you you can't read or write to
your file that's going to be completely up to you so let's say we want to
get access to the current directory how would we do that well we could say
directory info equal to current directory is equal to new directory info like that
and then we're going to just put a dot in there for our current directory now
what would you like to do well we can get access to a directory also with a path
and we could say directory info again and we'll call this Derek's directory
this clearly is more than likely not going to be on your system Because unless you
have a direct directory in your users uh directory um but you know hey that's
possible I guess so let's go see colon backslash and users and backslash there
like that and that's how to get to a specific directory now you can go and get
the directory path if you would like so let's go and console and right line and
I'm just going to say Eric's directory and full name look at that just
print it out exactly what I was looking for let's get a couple more of these so
I'm gonna go and uh copy this and just change what exactly I'm getting out
of it so let's say that I wanted to get the directory's name that's the
very first thing that seemed to pop up I can get this as his full name there is the
name I can then get the Parent Directory by just going parent like this there we
are I can go and um find out what type it is by going attributes attributes
there's attributes you can go and find out when this was actually created you
can find out when exactly this was created by saying creation time like that and
you can also come in if you have Administration rights and create a directory so
we're going to say directory info data directory is equal to new and this is
going to be directory info and let's say that we want to create this let's
create it roughly at the same place so I'm going like this and users and Eric
and C sharp Atta and there we go and then I say data PR directory create so I
created all those let's go let's run those and there we have all of our
information so we're able to get that directory we're able to get the
specific directory name The Parent Directory what type it is well of course
it's a directory and also whenever it was created all right so a lot of
information there and also you can see over in my directory if I zoom in here where
is it there it is C Sharp dot it's right under Apple I don't even know what
these things are but okay so that is this is stuff in my users directory so we did
create that directory now if you'd like to delete that directory well we can
just highlight this part right here and copy and you can just say directory like
that and delete and then pass in all of that information so same exact data that we
had before and like that and let's see if it gives me an error message because
the directory already exists um okay because I tried to create it again it
doesn't it was smart enough so let's go in here and where is it oh open the
wrong directory Eric there we are and we can see here that the C sharp directory
doesn't exist anymore all right so good let's close that what else do we
like to do well let's go and start uh reading and writing data from our our
files here well I'm going to get rid of this part I want it next time I execute
to actually create this data directory because that's where we're going to
be saving everything to I'm going to create a string array right here and
let's call this customers equal to and I'm gonna throw some people in so
let's throw my buddy Bob Smith in here and Sally Smith and also Robert Smith I
have a lot of my all my customers are named Smith okay so there we go got all of
those now what I want to do is I'm going to Define my the path to this actual
directory or the extra file name that I want to create so I'm call this text
file path equal to and did I save that yes I did so there's that and that's
the directory where I want to save this to except I have to also put my file name
inside of it so I'm going to call this test file one dot text all right good
now I can come in here and if I want to write the array to that file I can just
simply say file dot right and I'll say write all lines is what I want write all
lines and I'm going to put my text file path in first and then it was smart
enough to know that I wanted to go and Print in the customers now what I want to do
is that just wrote it to the file and what whenever I execute it then if I want to
read strings from the file I can say for each and let's say string and customer
in and here I'm going to say file dot read all lines good got it and this is
going to be text file path again and then inside of this boy what I want to do is I
want to just output that information so I'm going to say console right line and
I do want the customer's name but let's make it a little bit nicer than
this just so you can see what's going on in our output so I'm just going to
say customer like this and like that and close that off and there that goes any
other things I'd like to do well let's say that I'd like to be able to
get some file data out of here also so let's come in oh I can just run this
just so you guys can see it so oops there's a build error what's wrong here
um looks like I accidentally left off curly bracket there we are save it and I
think that was it oops hit the wrong button here we are I'll do it again all
right so you can see here that we were able to well it's all the same
information the only thing that's different is right here we have customer we
wrote that data and then we pulled it out and we used it and displayed it on our
screen and we can come down here and actually go in and look so C sharp data there
it is and what is going to open up when I click this oh the notepad all right and
you can see there it is it's written in there so very very very simplistic
process of going in and actually accessing that information now let's go and
get some additional file information so again I'm going to create directory and
I'm going to use directory info and I'm going to call this my data
directory equal to new and directory info like this why isn't it taking there
we are directory info doesn't like that let's see I don't know all
right so let's just go directory info like that and then did I save it see and
C sharp all right good so we got that guy right there now let's say we would
like to go and find out all the text files that exist inside of there so I'm
going to say file info like this and text files and and equal to my data directory
there are my data and then I'm going to say get files and then my conditional
is quite simply just going to be anything that's that ends with text Dot txt
then come out of here and go to the next line and I'm going to use the search
option of all directories so let's get every single thing that exists out of
there now what I can do is I can go and see how many of these I actually have just
by saying that I want to get my text files length and let's go and put a little
bit more information we'll say matches for the number of strings that we have
inside of there that just to keep that simple all right and on top of that
let's say that I would like to go and get the file names and output them I can
say for each and I'll say file info file in the text files and there we go and
what I want to do here is I want to Output um out oops output the file name
there's only going to be one in there we'll do file name like that and I
would also like to get the the number of bytes in the file let's just copy this
right here paste that right there and to do that you say file and length I'm
going to wait on executing all this because I have so many different things going
on ah why not let's just execute it let's run it and here you can see that
we're able to get our customers there's only one text file it outputs the
text file and the number of bytes that are in the file all right it's a whole
bunch of other additional information now there are numerous different ways to
access your file data one another way is through the use of what are called file
streams and they're used to read a byte or an array of bytes so let's go
string and let's call this text file path to equal to and inside of here I am
just going to call this test file 2. like that and close that off now what I want
to do is create and open a file stream so we'll say file stream let's just
let that be FS that's perfectly fine but then instead say file dot open you
have to close these after you're done with them and I'm going to go text
file path to there it is and then we're going to say that we want to create it
so we're going to say file mode and create like
that now what it can do is I'm going to go and create a string that I'm
going to write so I'll say random string is equal to this is a random strength
like this and then I need to convert it to a byte array so I'm going to say
this and let's call this RS byte array for random string byte array and equal
to and we need to say encoding dot default like that git bytes and random string
all right good now to write to the file I'm going to say fs.right and rs byte
array and this is going to be zero and the byte array length is what needs to be
passed inside of it now another thing that's interesting is we can move back to
the beginning of our file if we would like by just calling position like this and
zero and then we can create a byte array to hold the file data so we'll say
byte and like this and let's call this file byte array equal to new and this is
going to be byte RS byte array and we have to pass in the length of that byte array
then we can put the bytes into our array by saying 4 int I is equal to zero well I
is less than RS byte all right length and everything was automatically thrown in
good and then we can say file and byte array and each of those individual pieces
however in this circumstance we're going to get rid of this and instead convert
this to byte f s dot and we will call read byte like that and there we go now to
convert oops go outside of this for Loop now to convert from bytes to a string and
then output that information we can do right line then we say encoding dot default
Dot and we call get string and file byte array there we go and then like I said
it's important to close the file stream whenever you are finished and we can go
and run that and you can see here is our output this is a random string right there
and if you open up the file you can also see that there is our string once again
all right so good stuff let's continue here I don't need that open
let's close that now in actuality if you want to work with strings and read and
write them to your file system it's better to use what are called stream
writers and stream readers let's create another one and we'll just call
this three works fine change this to three also all right so we have both of those
set then we want to create a stream writer and I'm just let that be the default
streamwriter and pass in my path and then what I need to do is if you want to write
to a file you can just say SW and this is not going to create a new line if you
just simply say right say this is a random and whatever okay and then if you want
to write with a new line you can say SW Dot and right line just like we're very
very used to doing and inside of here we're going to say sentence sentence like
this and this will create a new line uh you can go and write another one so just
keep writing as long as you would like so right line again and we're going to
say um I don't know this is boring but this is another sentence there we are
and there we go and then after we're done we can close our string writer
exactly like that now if you want to open the file for reading data from it we can
go and we have to call stream reader and should I just keep everything here exactly
the same why not all right then what I can do is there's a bunch of different
ways to access we can use Peak which is going to return the next character as a
Unicode if we would like to convert that and we can convert it to actually show the
characters so let's say I wanted to go console and right line oops there it is
and there we are and let's get rid of this part and this time what we want to
do is Peak so we're going to say Peak like that and get this information out of
here come down here and I'm going to say convert dot to character Sr dot Peak
and pull that information out of there and I think yeah that's how many I need
you can also read to a new line so we can say console do right line like that and
we could say first string and get that information out of there and let's come
up here a little bit so you can see this and I'm going to say Sr Dot read line
and close that off and we are you can also read to the end of the file starting
where you left off reading so if you want to get everything else you say console
right line and everything else like that and then we can just say Sr dot read two
and of course you could do that from the very beginning if you would like and then
after you're done of course you need to close it so close there we go don't
think of any errors let's run it and here you can see our output Peak just
grabs you the very first character but of course it gives you a Unicode that needs
to be converted into a character then you see up to the First new line and then you
see everything else that was stored in the file and I think you can trust me that
everything is in that file now the last thing I'm going to demonstrate is what
are called binary writers or readers and they're used to read and write data
types very useful so we're going to say four and then change this to four like
that and then we'll come down inside of here and the very first thing we want
to do is we want to go and get our file so I'll call this that file is equal to
new file info and we will pass in our path if you want to open the file you say
binary writer and VW is equal to new and binary writer there it is and uh yeah we
have to pass inside of it the dot file and we want to open it for writing so open
right boom and boom good and now what we want to do is we want to create some data
to save to the file so I'm going to go string random text is equal to and
random text and then we're going to create an age so in my age is equal to um
47 and then double height is equal to six point two five and then we can write our
data so we can say BW dot right and random text like that and there we are and
let's just copy this I'm going to print all the other stuff also so paste
that in there paste that in there and I'm going to put in my age so my age and
also the height oops there we are height there we go all right so we have that now
I want to close this so I'm just going to say VW Dot close and there that is
and then I'm going to open the binary reader for reading and writing or reading
reading data so I'm going to say binary reader is I'll just leave this be
like that and leave that be like that everything else is exactly the same and to
Output the data that I just put inside of there I can come in and just say right
line and then what I want to pull out and print onto the screen is going to be BR
Dot and read these strings of information that we have inside of there so I'm
going to say read string like this and then you're going to use different
functions to read different types of data so I'm going to read the string and I
am going to read the integer which is an age this is int 32 and then I'm also
going to read what's the last thing the height well that's a double so
there is a double there we go and save that and then just don't forget to close
it after you are done there we are and we run it and you can see that we got our
information just as we put it into our file all right so cool stuff there's a
whole bunch of different ways of messing around with files and up next I'm
going to talk about serialization all right so now let's talk about
serialization now with serialization what you're going to be able to do is
actually store the state of an object in a file stream and you can pass it to a
remote Network whatever you'd like to do so what we're going to do
let's go in and create our old buddy here animal all right so this is going to
be a good one though all right so I'm going to create my animal class inside of
here animal class there we are whoops I spelled it wrong I was trying to be fast
and then I spelled animal incorrectly let's not do that all right let's get
rid of that and let's create it right here all right so class and I'm going
to take my time this time I'm going to say animal there we are all right so
here we are and let's create it all right so what we want to do here is we want
to Define what we want to serialize with this class so we're going to come in
here right after the namespace and put serialize a mole there we are okay good
stuff so we got that set and then this is going to be a public class animal and
we'll say I serializable let's see if it'll pop up here so I don't
have to type that all out serializable there we go all right good stuff now what we
can do is we can come here and click show potential fix is and Implement our
interface all right so we got some stuff done for us that's always good and
we'll come in here and I'm going to Define my Constructor so I'm going
to say public class and oops now what I'm going to do is Define everything I
want to store in this class so I'm going to there's name that works and
what else will be like let's get rid of this extra line we have here no
it's not my next one is not description let's just copy this line here just
to save a little time paste this inside of here and we're going to have four
different pieces of data I'm also going to have a double and it's going to
be my weight that I want to store so I'll say double and wait and I'll have
double and I'll have height like this did I get it all height there we go and
then I'm gonna have an animal ID which is going to be an integer so we'll
have this be animal ID animal ID all right have that all set let's create our
Constructor so we'll say public animal and just keep it simple there and then
I'm also going
to have another Constructor inside of here let's have this be public animal
and we have string name is equal to and I'll have this be no name down to
another line I'm gonna have double weight equal to zero and double height also
be equal to zero and then we can come in and Define everything so I'll say name
is equal to name and weight is equal to weight and height is equal to height all
right good all right so now what we need to do is I'd like to override tostring
for this class so I'm going to say public over we did this in the past so
anytime they call to string I'm gonna Define here what I want to occur and what
I'm looking for here is just to say return and string Dot format and go and get
some information so I'll say whatever the animal's name is and I'll say
ways and go and get the weight I'm going to have this be in pounds and is and
go and get the height and I'll just have this be inches tall like that and then
I can go and of course get all this information let's just keep it on the same
line so I'll say name and wait and height good now we get to our serialization
function which is what was automatically placed inside of this for us we don't
need to change anything in regards to the parameters but what I want to do here is
assign key value pairs for all of the data so come in here and get rid of this and
I'm going to say info dot add value and assign this with name and of course
it'll just be name and then we're going to do this for our other additional
information so let's take all this copy that and let's throw that inside of
there then we're going to have our weight so weight and of course this is
weight whoops wait and our height and height and then we're going to have our
animal ID and this is animal ID all right so we have all that set up for us so now
what we want to do is create the deserialization function what it does is it
removes object data from the file so we'll say public animal and we're
going to say serialization info and streaming context also so let's grab both
of these paste those inside of there and come down here and here what we'll do
is we'll say name is equal to and this is a string and we're going to get
the values from info and assign them to the properties in this situation so
I'll say info dot get value and name and this is going to be type of and
we'll have this be string then we have to do this for everything else what do I
need an extra parenthesis what did I do wrong here string info get value name type
of and this is going to be string [Music] um hmm I'm not sure what I did maybe
it's what is the error message here info is not in all here oh okay don't
worry about that okay so we're just gonna go name and go and do this for our
other different data points okay so we'll have this be weight this and this is
going to be a double so we'll say double and this is going to also have to be
converted into double and then we'll have our height and this of course becomes
a double say double and um yeah don't forget to change this also this is going
to wait and this is going to be height and this is double then we have animal ID so
animal ID which is going to be an integer as we defined it and animal ID and then
this is going to be an integer as well all right good stuff all right so now that
we have all of that set up let's go over into program and we can start using it
do I have system i o inside of here I don't think I do so let's go and say
using and system dot IO and this is going to be used for writing to a file and also
I need an additional library to serialize an object and I'm going to serialize
in binary format specifically so I'm going to say system dot runtime dot
serialization so your realization Dot and this is going to be formatters Dot and
binary there we are and uh then we also can serialize into XML so we can say using
system Dot XML Dot and serialization which is right there serialization good and
also uh that is all that I can think of all right so let's go and try this out
so we got this and inside of our main function I am going to create a new animal so
animal and let's call him Bowser equal to new animal and Bowser Bowser there we
are and 45 and 25. now what I want to do is serialize the object data to a file to
do that you say stream and stream is equal to file dot open and I'm going to
say animal data dot dot and you're gonna it doesn't exist so I'm gonna
mark this as create and whoops accidentally hit that all right good and then
I'm gonna go and create a binary formatter so binary formatter and there
that's fine but let's just shrink this down to BF like that butter and
there that goes all right so we can move this up here so we can see more on the
screen um another thing that I'd like to do is send the object data to the file
so I'm going to say BF Dot serialize and stream and Bowser good and then say
stream close right like that now what we can do um if we wanted to delete our data
we could say Bowser like that is equal to null that's one way to do it if we
want to read our object data from a file we go and open it again let's go and
let's just copy this guy right here so grab this or actually and I can just
copy this so I can go stream copy that and um let's grab the binary formatter
also so let's go here and here this paste that inside of there and what I need
to do is change this to open let's grab this and then I don't need the
binary formatter part here it's already been set up so there's that and
then I can come down here and I can go Bowser is equal to and animal and binary
formatter dot d serialize and stream like that and then I can go stream Dot close
like this now what we can do is I can go and call tostring to see this information
so let's do right line like that and then just say Bowser dot to string and
it's going to Output everything I want in a nice easy way one thing I just
figured out is I'm passing the height and weight name and all that but I
didn't over in the Constructor I don't think I assigned animal ID which I
did not so let's come in here and let's get this and then put animal ID
inside of here and this is going to be an integer and let's go and give that a
value of I don't know let's just make it zero also and then I can come down
here and go animal ID is equal to and let's have it be animal ID like that
animal ID there we are and there we go all right good so let's come back over
here and Bowser and let's give him a one for his value and let's go and run
this guy I closed it right yes I did all right so let's run it and you can see
here is the data that we expect Bowser weighs 45 pounds 25 inches tall all right so
good stuff um let's go and close that now you're also going to be able to
write object data as XML and to do that you go XML serializer serializer [Music]
serializer where are you at there it is XML serializer serializer there we are and
this is going to be equal to new XML serializer there we are and type of and this
will be animal all right then we're going to say using and you go text writer
where's text writer there it is and let's just call this TW is going to be
equal to new and this is going to be whoops I should have just taken that new and
stream writer and here inside of stream writer I'm going to get rid of our
stream part and I'm going to replace that with the path where I want to store
it now this path has to exist so see if you just use my code it won't exist
like I said before unless you have in your users directory hey Derrick directory
which I find highly unlikely okay and data and let's go and call this Bowser
Dot XML and close that close that and then what we can do is go serializer like
this and serialize like that okay and if you want to come in and delete your Bowser
data let's come down here we can just say Bowser is equal to null like that now
if you want to deserialize from XML into an object of course you wouldn't
normally put all this stuff in your main file I'm just doing it to keep it
simple we say XML serializer and I don't like the name of some of this stuff
but I'm just going to keep it this way I'm going to call this D serializer
like this and get rid of that part and this part is fine and the animal part is
also fun but what you're going to do now is say text reader greater is equal to
new and then this is going to be a stream reader there that is and what are we
going to do here well we're just going to reference this directory again
sometimes we'll just copy this copy that and stream reader throw this right
here and paste this inside of here throw a new semicolon then we're going to go
object as equal to D serializer there it is deserialize and our reader then we can
say Bowser is equal to and animal and object like that and then we can close our
reader by saying reader close and then we can call tostring again just to prove
that uh it worked so right line and let's throw Bowser and call to string like
this and then I I just realized since I am already doing this I want this to show
that something has changed so up here when I called Bowser to string I want to
change something about Bowser just so you can see in the output that something
changed between the first and the second so let's just change his weight to 50
I don't know just just to do something different all right so now what I need
to do is um let's say this let's say that I wanted to well let's go and
actually just output this so we'll run it and you can see up here that in this
situation we change the weight
for Bowser and we're able to pull both of those files and all of that data
from said files and if we come up here and go into my c-sharp data area and open up
Bowser when I want to open it in Why didn't it just open it in some normal here
let's just open it in notepad all right you can see here that it is actually
XML all right and that's how it was stored so let's get out of that and get
out of that and get out of that all right so now what do we want to do well
let's say we wanted to save a collection of animals so let's go create a
list and these are going to be of animal type oops forgot to put my bracket inside
of here so let's go like that and and then I'm going to say the animals is
equal to new and list animal all right now that I have that set what I can do is
just throw in animals so I'll say animal and I'm going to throw this here
say this is Mario and 60 and 30 and um two like that and let's go and copy this
so I have to type it all so go copy face and paste and we'll type in Luigi so
Luigi and let's make him 55 of just to make him different I'm just so that
that is different just off and then at the end I'm going to say Beach and
let's say 40 and 20 or something I don't know 40 and 20 and make this four
okay don't forget semicolon all right now that we have this set up let's
see if I can keep this all in one little window here so you can see it all at once
then I'm going to say using and stream FS is equal to new and we're going
to be using file stream didn't pop up yet there's file stream file stream
and I need to get the location for my data so I'm going to go and grab all of
this stuff right here and this is going to be I'm just going to change it
slightly so I'm going to say file stream and come down inside of here and then
I'm going to paste in the location where I'm saving this information
I'm going to change this to animals XML though animals XML and I need quotes
around this there that is and then what am I doing here I'm creating a brand
new file so I'm going to say file mode dot create and I also want the ability
to write so I'm going to say file access Dot right and file share I'm going
to Mark as none all right and now what I can do is do I need yes I need another
closing parentheses there and then after this I'm going to say XML serializer
serializer is and I'm going to call this serializer let's not call it one
let's call it two this is going to be equal to new and XML serializer type of
this is not going to be a type of animal it's going to be why don't I put
this down on the next line so serializer this is going to be type of list animal
we're working with a list in this situation like that and then what I can do is
say serializer did I get it right yeah two serialized and for my animals now if I
want to delete this data I can say the animals is equal to null and if I want to
read the data from XML I can say XML serializer we'll call this serializer 3
equal to new XML serializer now I just type it in again type of and what type is it
it is a list of moles and everything else there is fine I guess I can't get
everything all in one one window at one time I need more space and what I want to
do here is I'm going to say using and again file stream it's basically the
same as what we did in our previous example example why not and what we want to do
here is open this file for reading do I have yes good okay so I am going to say
equal to file dot open read paste that in there and of course this is animals XML
so animals XML and then we can go and get the animals and I did all right yeah it
looks good and I'm going to say the anomals [Music] is equal to and I go list
and the animal type is what we're working with close that off and then say
serializer three and call deserialize serialize like this pass FS2 and close that
off I have a probably a quote problem here that's exactly what I have all right
and now what we can do is we can go and cycle through all of these think I forgot
to put a closing curly bracket here yes I did okay so I got all those and then what
I can do is just cycle through all of them so I'm going to say four each animal
and I'll just say a in the animals and then I can just call two string on them
to print all of that information out I call this the animals right the animals yes
the animals and then I can just say boom like that all good stuff and that is all I
need to do to do all that so cool cool all right then we can cycle through here and
look at what we got so we have our first two lines which we did before we have
Mario weighs 60 pounds and it outputs all of that information and then just to
verify that it actually does still exist we can come in here and go into animals
and that it exists as XML um ask me later I don't care okay and you can ah
where'd it go there you go so you can see all of this information has been
stored as XML alright so good stuff learning a whole bunches of stuff and up next I
am going to start talking about databases and how to use those with c-sharp all
right so now what I want to do before we need a database to be able to start
working with databases so I'm just going to use SQL Server if you go and type
in SQL Server a developer you're going to get to this page you might want to
put download on top of that so we're going to use the free version 100 free so
you're just going to click on this and then you're going to see something
like this pop-up when what we want to use in this circumstance is the basic option
and then you're going to have to accept the Privacy agreement and then you
decide where you want this to be installed and click on install and I messed my
system up as much as humanly possible so that I could potentially catch any error
that you could ever come across all right and then this is going to install and
it's going to take a while and then whenever it's done you're going to
see something that looks like this now what you're going to want to do is click
on customize and whenever you do you're going to want to click on Microsoft
update and use Microsoft update to check for updates recommended click on next
whenever you do you're going to see something along these lines just click on
next and then what we're going to need to do is go and look specifically for
SQL Server 2019 installation center it's been installed in your apps and you
want to click on that and then you want to click on new SQL Server Standalone
installation which is at the top here whenever you do that you can go and
you're going to need to find your installation media where that's located
at if you want a Hint it is right here it says installation media folder so this is
where you're going to have to locate that and so that's exactly what I did
right here and then I click on OK now this is going to pop up just leave this
selected perform a new installation click on next and then specify free edition
which is going to be developer click on next gonna have to accept these agreements
click on next then make sure you put a check mark here where it says database
Engine Services and everything else will be perfectly fine click on next then I
change this to a named instance and I just called this Derek tutorial you can call
it whatever you'd like click on next and then this is going to pop up you
don't need to check this that's all fine click on next and then here
you're going to want to click on mixed mode and you're going to type in a
password and the username for this is going to be systemad man which is just
abbreviated as sa so just so you know that later on but here's where you're
going to type your password click on next here this is going to pop up click on
install and then that's going to take a while to install whenever it's done
you're going to see that this has succeeded so click on close then what
you're going to want to do is go and get the ssms installation so this is SQL
Server management studio and here you can click on this to open that webpage
install SQL Server management tools and this is what you're going to get
you're going to click on this guy up here and then you're going to want to
open this and execute that of course and whenever you do you're going to see
this so you can go and change your location do whatever you'd like I'd
likely you want to keep it where it is click on install and it's going to take
a while to install that and whenever it's done you're going to see this now
what you want to do is you want to go and look for this newly installed SQL Server
management tool click on that and then it's going to load up and then here is
where see I said login the user ID is essay then you're going to type in
whatever password you set up previously and you can click on remember password if
you'd like and then click on connect and then you can see everything's set
up here now what we need to do is basically get this to work with Visual Studio so
I'm going to open up visual studio and it looks weird because I enlarged
everything so it's a little bit easier to see on your screen you're going
to say you want to create a new project and then C sharp and next and then whatever
you want to name it wherever you want to store it click on next and Dot net 6 is
fine click on create then what you want to do inside a visual studio is go to view
and server Explorer we're going to have a little bit of a hiccup here but
I'll show you how to fix it and you can see here are our data connections what
we want to do is right click on that and say add connection and then
you're going to say the SQL Server click on continue and it's going to say
that it's missing some packages potentially and so you want to click on OK to
install those packages and you're going to see this click on install and then
it's going to install I'll go back to view and server Explorer again add
connection SQL Server continue and you're going to see this pop up and
you're gonna be like what's my server name I don't know what to put
inside of there and nothing's going to show up more than likely so what you
want to do is you want to go back into SQL Server management Studio you want to
right click on this and you want to go to properties whatever you do this is
what's going to pop up and you're going to select whatever it says here
next to name in my situation it says Scarlett Derek tutorial remember whenever we
named that well that's where that went and you're going to want to copy
that and then paste it directly inside of here then you're going to type in
your username essay and whatever password you have save my password and then you
can see here I selected my database name and I'm going to click on OK it's
going to say that it doesn't exist and or I might not have permission to use it
would I like to attempt to create it yes I do so click on yes and then you can see
it shows up right here and that is basically the steps you need for installing the
database and connecting it and all the database software and then finally
connecting Visual Studio to your database and in the code that follows I'm
going to show you how to work with databases all right so now I have the database
set up so what I want to do is set it up so that we'll actually be able to work
with it inside of visual studio and start writing some code so what I'm going
to do here is I'm going to say file new and project and again this is a I made
this extra big so that's the reason why it looks different than probably what
you're seeing and what I want to make here is a WPF app.net application not a
WPF application they're different so if we look here so Windows forms app so if
we go through this WPF application don't want to use that in I'm going to
make a gigantic video on dot net Maui the next video I make but for now I'm
just going to stick with WPF because we have so many other things going on this is
what you want wpfapp.net framework click on next and then we have to give this a
name so let's just call it WPF app one that sounds perfectly good and
everything else here is fine let's go create and I'm going to say uh
don't save for that something else I was working on okay so now we have this
all set up here so what we want to do is we actually want to go and create our
database now you may not see these things even though I showed them before if you
want to find them just go to view and then you can look for whatever Tabs are not
showing up so here's server Explorer and so forth so this is what I have right
now and this is where we were at whenever I left the last video so what I want to
do is I want to go into our database and I want to right click right here where it
says tables and I always say I want to add a new table and this is going to pop up
and there's going to be all types of information here so what I want to do is I
want to well basically we already have an ID here this is going to represent a
primary key but it's currently not in a primary key format so what we want to
do is we want to select it and then under properties over here so let's just go
properties all right so whenever we create this table it is going to have an ID
inside of here and as you can see it's going to represent our primary key but
one thing we want to make a change to is we want to make sure we select properties
down here this tab in the lower right hand corner of my situation and then select
this and then we want to go to where it has identity specification let's move
this out here so I can actually see it and we want to change this to true so set
this for true oops I have to open this up first is identity this is going to be
true so just click this tab right here and then click on true and then that's
all set up for so that's good we can move this over here now what I want to
design with these databases is basically just a pretty simple system I'm going
to have some stores I'm going to have a list of potential products that I have
in those stores and then I'm going to have the actual inventory in each store
and then as we program this I'm going to allow you to add products delete
products change products names and so forth what we're going to do with this
guy is to start off this is just quite simply going to be called store so we're
going to say its name is store and then we're going to go in and we're
going to add additional Fields so I'm going to say okay I want to know the name
of the store and then we come to this part now when we have a character here what
this means is this is going to be a fixed length of 10. what I want is a variable
length so I'm going to set this to 50 and then on top of that I'm going to
come in you can go and work down here or at the top or whatever you want I want to
set this to 100 length because I think that's going to work and I'm also
going to say that I do not want to allow allow null values which is the same as
saying no value I'm going to also come in and throw in a street and likewise
I'm going to change this to a variable number of characters also so let's
throw that there all right and you can see it updates up here I'm going to also
have a city and the variable number of characters again so like this 50 I think
that sounds perfectly fine I'm also going to throw in a state now in this
situation I think it should be a fixed number of characters and I'm gonna mark
this as two this isn't a database tutorial this is just you know the basics of
allowing you to set this up maybe we'll have a zip code and in this
circumstance a uh int value is going to work pretty good for me so let's just
go and Mark this as an integer and everything else is fine so we have a primary key
we have name Street city state ZIP code and we change the name of our table to
store now what we can do is click on update and it's going to go and update our
database so it's just preparing that right now all right and then after it
comes up with this message that everything's ready to update just click on
update database and by the way if it crashes and it gets stuck on that screen just
restart Visual Studio that's really the only thing you can do to fix that now
what we want to do is to put some store data inside of there and we can go into our
tables here and we see that there's nothing showing click on update now you can
see that store is showing then you're going to go to store and you're going
to right click on it and you're going to go show table data and there's not
going to be any table data so what we want to do here is we want to go in and we
want to fill in some names and streets all fake stuff clearly and states and zip
code information all right so there's some fake information I threw inside of
there then you're going to want to go over here where it says data sources and
if you get an error there are no data sources to show for the selected project
I'll show you how to fix that you want to make sure that you have your solution
Explorer open over here so just go solution Explorer where is it there it is right
at the top solution Explorer make sure you have that open and then you're going
to go over here where you have your project WPF app one you're going to want to
right click on it and then you want to go to add new data source so add all you
need to do is come over here where you have your app name WPF app one and click on
it and whenever you click on that go to data sources and now it allows you to add a
new data source so just click on that and let's open this up a little bit so
you can see it a little bit better you want database you don't want these other
two things click on know how it's open the whole window up so you can see
everything that's written on here click on next data set click on next and then
you're going to see which data connection should your application use to
connect yes this is the correct one you're going to want to come down here
where it says yes include sensitive data in the connection string and then you also
want to check right here where it says show the connection string that you will
save in the application so here is that guy then you're going to click on next
and this right here you want to copy this and save this because you're going to
need it so I'm just going to copy it make sure you don't copy something
else and lose it but it's just you know whatever this is so you I'll show
you in a minute what we're going to use it for so we're going to copy that
and click on next and then this is going to pop up here you're going to want to
make sure that you put a check mark where it says tables and then click on finish
and there you go everything's set up then what you can do is over here in the
data source tab you can open this up and you can open this up and you can go to
store if you want to see what this looks like in a prettier sort of vision you can
right click on it and then you can click edit data set with designer like this and
it'll show you your store and all of your additional information but we
don't need that right now so I'm just going to close that now you're
going to open up this code window right here
and we need to import some information to be able to work with this but I'm
going to go and paste in this is going to give me an error so what I need to do is
take what we just copied and save it inside of here which is our connection string
so connection string is equal to and what we want is configuration and manager give
me an error I'm gonna I'm gonna fix it don't worry about that and then
connection string and then inside of this connection strings like that and then
what we're going to do is throw this bracket in here not parentheses and
you're going to say w p f app one which is the name of your application and
then properties dot set things Dot and here is what we copied from before so you
just paste that inside of here and then at the end you mark this as connection
string right like that and in the videos that are and in um the code that follows
I'll show you how all of this is going to work but we need to go and get this
guy so what we want to do is we want to go over here under your search solution
Explorer and under references right here you're going to want to right click on
references so there that is and then go add reference and then you want to make
sure that you have assemblies selected up inside of here and then what we're
specifically looking for is system configuration so let's open this up so make
sure we get the right thing so system configuration this is what we want just put a
little check mark inside of there and then click on OK and there we go all right so
now what we need to do is go and import that so right here we're going to come
up here and say using system configuration so configuration there it is there it is
and there it is and your error went away and there you go and up next we're
going to start playing with our database all right so now I'm going to add some
more tables so let's just go over to tables over here in server Explorer say
add new table now I actually kind of prefer to just type everything in that's
normally what I do because I'm just used to doing it this way so what I want is
remember we're going to have a store we're going to have products and then
we're going to have a relationship table that's going to lie between them
and sort of merge what's in the product and the store tables so I'm going
to call this product and this is all fine and primary key except I want identity at
the end here and then what I'm going to do is I'm going to have
manufacturer these are going to be shoe stores if I didn't mention that so
we're going to have the manufacturer and Bar character just like we did before
100 and not null and why don't I just copy this copy this and throw in one more
column and this is going to be brand so manufacturer will be something like Nike
and um the brand is going to be something like Air Force One or something like that
all right not in all and I don't need this here and now I can click on update
and then whenever it takes update databases like I said if that crashes just go and
restart Visual Studio all right so now that we have that set we can close this and
we're going to create another table so let's just come in and add new table
and for this what we're going to be doing actually I should have why don't
I do that first I'm going to go into here and I'm going to go into tables
and product and I'm going to add some new products here so I'm going to say
show table data and I'll just go and add a couple products so I'm going to
say Nike is what I want to have here and let's go whoops like this so we'll
have some room so we can actually read what we're writing here and then here
I'm going to say error Force one and I'm going to say Nike and I don't
know it's Nike or Nike I don't I really don't care I grew up thinking
it was night and if it isn't well I don't Okay so if you're actually
hearing me talk about this I'm just glad that you stayed this long for the
video and uh if you did uh why don't you take a second and tell me because I
will be absolutely amazed okay so I'm gonna say Brooks and ghost and [Music] I
don't know I'm trying to just throw some random different types of shoes
inside of here uh zero brand I think is what those are called and New Balance 327 I
think that's a shoe okay so we have all of those inside of there now let's
go and create another table so we're gonna go to tables add new table and then
I'm just going to type everything in just like I did before and this is going
to be the individual store inventory for each store you know what shoes do do each
of the stores carry so in not in all primary key and I want this identity again and
what I'm going to just simply store inside of here is the store ID and this is
going to be an integer not null and I'm going to do the same for say this is
going to relate both those tables together through a the same type of ID okay so
this will be product ID get rid of this there we go click on update and then update
database again all right so good stuff now what we need to do is Select our
application over here and then you're going to go alt shift and D like that and
here are our data sources and we want to right click our database so right click on
this and then we want to say that we want to configure our data source with Wizards
what we need to do is to basically set up our foreign keys so we have our tables
right here and we want to make sure we have product and store inventory selected
and then click on finish and there we are now what we want to do is let's come
over here to this guy and let's refresh this okay there's store inventory
and we want to right click on store inventory and click on Open Table definition
and we're going to set up our foreign keys for this store inventory so how you
do that is let's move this up here so you can see everything all at one time
you just come in and say constraint print and let's call this store um uh
foreign key something like that and this is foreign key which is going to reference
our store ID and then we say references and store this is the table name store and
the ID inside of it and make sure you put commas everywhere and we're going to
copy this let's set our comma there also and copy this right here because
we're going to basically do the same thing so we're going to go here and
we're going to say product foreign key product or in K and then this is going
to reference this the product ID here's the product ID here's the store ID
so this is going to be product ID oops product ID and references and in this
situation it's going to be the table name product and the column named ID
inside of that and then of course we come up here and click on update again and
update database and there we are now what we want to do is to populate the store
inventory table that we have so I want to go and get this there's store
inventory I want to right click on it and I want to show table data because there
is non-currently and I have no idea what I have um I just realized okay so we got
product and you can see there are our different product types actually what I want
to do let's just close that I want to go over into my server Explorer and I
want to right click on this and I want to say new query and inside of here I'm
doing this only to see what I put in there because I totally forget someone to say
select everything from product like that and then you hit this little run thing
execute and it shows I have six items inside of here now what I want to do is so
I'll just remember product six and from this I'm going to say how many
stores do I have because I don't remember all right so let's do this and I
have five too bad I didn't have six but I have five stores all right good
enough so what I want to do is I want to populate this guy which is going to be my
store ID so I have five stores and six products so I'm gonna say that this
store here let me open this up so you can see what exactly I'm doing I'm
going to use the identification number for the store so like this and I have six
products let's say store one has the first item and store one also has the
second item and I'm going to do two and oops can't do that whoops whoops
all right let's say it has one and it has three and there you go I went and
updated everything that was inside of there I didn't think there was any reason
for you to have to sit there and watch me type a bunch of random numbers but
basically I have stores one through five and each of them has different products so
that's what the products reference here so store two has product one and three
and so forth and so on I think that's understandable now let's go back over
where we were creating our quarries and what do I want to do I want to do an inner
join just to make sure that that took with the uh the store inventory table that I
created so I'm going to say I want um let's say product and this is going
to be a product and I'm going to say that I want the brand return so that's
not uh the manufacturer like Nike it's something like Air Force One instead and
I'm going to say that I want to get this information from my product table and
I'm going to do an inner join sorry this is just a way of joining tables uh
this really can't become a SQL uh an SQL Server tutorial also and I'm
saying SI I'm just going to type out everything and then I'll explain what
every single thing means so p i d is equal to s i dot product ID and I'll say
where s i I'm going to explain this just give me a second to put it all inside
here okay so
I have all of this here and we can go and run it as long as I didn't do
anything wrong whoops what I do wrong oh I see I forgot to reference this okay make
this P I think everything else there is fine let's run it and uh yeah so it
worked okay so what I did here is I said I want you to select all brands from the
product table and an inner join what it does is it merges the store inventory table
with the product table so it's like there this same sort of thing but on what
it was this is an abbreviation so I don't have to keep typing out or an alias
so I don't have to keep typing out product and this is an alias so I don't
have to keep typing out store inventory okay so that's what's going on
there so what I'm saying is I want every product in our store inventory where
the product ID is equal to the product ID for the store inventory so in essence
I'm saying I want everything that store 1 has and where we're going and
this is the condition where the store inventory store ID is equal to one okay so
hopefully that's understandable like I said I can't get you can leave
questions in the comments and I'll I'll expand upon that but I'm
basically just saying I want all the brands that are that have a store ID for store
ID one all right hopefully that's understandable got a lot of this SQL out of
the way at least for now and now I'm going to start coding all right so now
let's create our interface this is going to be pretty simple uh one thing I did
here is I went and put store inventory manager in uh the bottom part I type that in
so that's the only thing that's changed since okay so what are we gonna do
here now this isn't going to be the prettiest interface ever but it doesn't
matter by the end of this you're going to know how to be able to use databases
and that's the ultimate goal so what we're going to do is click on common
WPF controls right here and then just start dragging some stuff over all right so
what do I need I need a label so let's bring this over and I'm just going
to stick it inside of there and then let's see if I can double click on this
yes I can and I'm going to say that this is going to be stores that's what
we're representing there don't hit enter at the end just go like this what
else do I need toolbox I am going to need a list box so I'm going to grab a
list box drop it over here don't drop it on top your stores and let's just
move it up so that it looks that looks okay it's way too big so let's go
and Shrink this down dramatically and place that and then down here or I don't
know how it got down there so let's move it up here so that it is exactly it
fits the uh the different samples we have here one through five all right so now
what I'm going to do is I'm just going to select both of these and I'm
going to copy them and paste them and drag this over here and let's just put
some distance between the two of them and come in here select stores and we're
going to call this store inventory after you see this interface I think it's
going to help a lot for understanding what's going on here all right and then
let's do another paste and we'll drag this over oops whoops grab this over
what happened I thought I did it let's undo that all right let's undo that
all together and let's go let's check down here to see if we went and
created it or not um no we did not all right so you see we have one label and one
list box so we know that we've created two of them okay so I'm gonna go and
let's move this down a little bit more all right so let's select this and
this copy paste and not sure why it is not pasting that in there okay it wants to
be difficult with me so I'm just going to say on two and uh yeah I can't do
that all right no big deal so let's just go and create another one so we'll
go and label drop that there there it lines up so you don't have to worry if
it's being testy with you it doesn't matter and we're going to select
this so I'm going to call this all products all right so what we're going
to have is we're going to have this store names we're going to have the
inventory for each individual store and then we're going to have a list of
products that can be carried in any of the stores so hopefully that's
understandable and let's go and after I get the whole interface done you'll
be like wow this is so simple for some reason it insists on having my list boxes
take up the whole screen I just realized all right so let's bring this over
here sometimes Visual Studio can act a little bit weird and let's go down here
let's shrink this down to write about here which is roughly the same as before
and we'll drag it over here and throw it in right there all right that looks
good and it looks like it lines up pretty pretty well okay so good so we might want
to move that around because I'm gonna have to put all kinds of other things
what else are we going to need here well I'm going to need the ability to add
stores and delete stores same with inventory and products how am I gonna do that
button sounds good so let's go get a button drag a button over here and
let's just bring it so that it's under here a little bit but up butt up
against that and inside of here oops you see what it did it actually created a
function that is going to allow me to Define what happens when that button is
clicked well I'm not going to name it button click I'm going to give it a
better name than that so what I want to do is let's say content Button as you
can see I can change the text that's inside of here with the content area and
I'm going to call this add store like that and also I think that it let's
make it a little bit wider so let's go like that all right good copy paste and
let's see if it messed it up no it didn't all right good so let's go
like this and then for the content on this one this is going to be called delete
store so we'll say delete store all right so we got all our buttons there
they're looking fantastic let's see if we can copy and paste them save
ourselves a little bit of time and there we go and over here what we're going
to do is we're going to have ADD inventory so we're going to be able to go
and get inventory from here from all these lists of potential products in store and
just pop them over there and we'll go to delete store this is going to be
called delete inventory and we will be able to do that as well and let's go and
select these copy paste and drag them over here and see here that our list box is
not the total perfect size so let's go and try to make it just a little bit
nicer take that extra minute to fix that no problem and here we're going to say
add product and delete product and then we're going to have the ability for the
users to go in and um add new products and manufacturers and Brands and all that
other additional information so it says delete inventory this is where we are right
and yep see this is slightly highlighted I'm not sure if you can see that or
not and let's come in here and go though products so we're going to add and
delete products so we need the fields to be able to facilitate that can't do it
just with buttons so we're going to need another label I'm going to go
label and this label right here is going to be called Product manufacturer [Music]
there it is and we'll just drag that and put it over here and we're going
to be labeling giving these all names so that we will be able to uh yeah I mean I
mean the fields the buttons the list boxes and such so that we'll be able to
reference them inside of our code come over here and for this we want a text box
not a text block a text box so let's drag that right there and um let's
let's put it in the center I think that looks the best and how much space do we
want to give it let's stretch it out a little bit more so it lines up with that
and it looks a little classier copy paste and we will also allow them to enter a
product brand and this is all coming from the different fields that we have inside
of our database if you don't remember server Explorer and what are we doing
here well this is going to work for inventory as well as products so let's just
look at products manufacturer and brand that's where they show up and
that's good um if we go over here and look at the stores what do we have well
we have a name we have a street a city state ad zip so we're gonna have to put
all that information here also because we want to give them the ability to add new
stores so let's go over here and get a label throw that there and this is going
to be store name and how should I line this up maybe line it up with the top here
uh store name and maybe spread this out a little bit yeah let's try that that
looks good enough and then we're gonna go and get another text box so text box
and let's drop that there and throw it there and we are just I really do want
to stretch it across so that it sort of fills the area something like that looks
pretty good all right so we got both of these let's copy and paste and then
let's come down here and for this one this is going to be store Street so just
highlight this and Street and that's good let's grab this and this copy
paste drag this down and the next thing I believe I had was the store City yes
that's normally how I do it I won't let me select that hello all right you
want to be difficult let's just go City now you can do it either please and
City and normally we would line
this up much better but like I said I'm just trying to keep everything is
completely understandable and simplistic over the prettiest interface you ever did
see all right and here's store City and let's highlight this and store and
after the city I had the state and let's highlight this and this copy paste and
then I'll go give them names after I finish this so there's that and for
this one I am going to have the zip code and we'll just call this zip code
because that looks good enough all right so there is our complete interface and now
I think you have a pretty good idea what's going on here and what we're
going to have to utilize to make this guy work in our database now what we need to
do is name all these parts so this gets a little bit more complicated so let's
go and highlight the store and then decide what we want to do with the label I am a
big believer in going and labeling everything so let's come in and I don't
believe this has a name I'm looking across no it does not this guy's the
one I'm talking about and I could come in here and I could name this a lot of
people don't name everything you know it's a label if you don't plan on
changing it then why name it but whatever I'm just going to do it here because
why not okay so we have a name for our label up here what would I like to do now
for our list box well the list box absolutely has to have a name so we're going
to come in here and what I want to do for this is go and give it a name that is
equal to and I'm going to call this store list and that's good and am I
going to have to click on anything I would think there is a chance and what I'm
referring to is if they click on a list box item am I going to have something
happen well yes if they click on a list box item I'm going to or at one of the
stores that's listed here it's going to show the products in that store
inside of our Center area this is never going to change unless something's
deleted or added this is never going to change unless something is deleted or added
this will change every time they click on a different store so yes let's go and
look and see what that's called so does it have a click event assigned to it it
does not all right why don't we just put a click event on it so I'm going
to say that selection changed this is what you use when you're using list boxes
and I'm going to call this store list and I normally have some consistency here
but in this situation I'm going to call this selection changed and that is just
going to point out that it is a list and it's going to make it stand out
it's just something that I do it to make the list selection Stand Out versus
the um clicks on buttons the way which are going to have less convoluted names all
right then we have store inventory I'm just not I'm in this circumstance uh
do I want to have it out why not why don't we just take the time and do it okay
we might want to dynamically change the names or something at some point so
let's just come in and let's just do that also so I'm going to paste
that inside of there and what should I call this let's just call this store
inventory whoops what happened did something incorrect um store maybe just simply
because that's the same name I bet you that's what it is inventory and
let's just keep it brief inventory label I bet you it's mad because yep I
gave it the same name that's what got it all upset all right so up next we have
this guy right here and I'm going to give that a name of course and do I need
to give it an in a uh clickable event uh or a changeable event uh no I don't
need any of that so I'm just going to call this name is equal to and let's
just keep it simple and call this store inventory inventory oops no no space all
right then we have our label again name is equal to and for this one I'll call
this all products label so all maybe a lowercase all products label and like that
and then we have another list box which is going to be this guy and I'm going
to call this product list name is equal to and product list like that all right so
we have all of those all set up now we get to the button part and for this one I am
going to go and first off I'm selecting add store and I want to see if there is
a click assigned to it it's third up from the bottom and the generic name it
gave it was button click that's not a very good name so I'm going to go and
change that and what should we call it let's call this add add um store click
much better all right and then the one under it is delete store so let's go and
get rid of this and well let's move that over there I can leave the click part
on there and for this one I'm going to call this delete store click delete
store click very understandable what's going on whenever you click on those all
right now I have it add inventory so I'm going to call this and I'm going
to keep its consistent even though it's getting a little bit long in the name
add inventory click and whoops I already have click there so let's get that out
of there and let's continue so for the next one we have delete inventory so
I'll call this delete inventory click delete inventory click and for the next
one we have ADD product guess what this is going to be called add frauduct click
and then we have delete product click so delete product click anything else well
whenever they select these boxes do I want anything to change no not really one
thing that is very irritating though is I don't like the fact that it says text
box and all these so I'm going to get rid of all those so I'm going to
select this and here it is and it looks like I have to scroll to get rid of the
text box part so the text is just going to be nothing so you'll see that go
away whenever I save here I'll just save it see I wonder why and um let's
move this up so that I can see this a little bit better all right um there's
another one that has text box inside of it let's get rid of that let's get
rid of this one let's get rid of this one and this one and this one and the
last one all right so I think I got rid of all the text boxes looks like I did and
I think that is probably all that I have to do yep all the text boxes are gone the
interface in general looks pretty good so let's save it and if we have to make
any changes as we go no problem and I'm going to generate all the functions
largely just by double clicking on these boxes which will be really cool all right
so what are some things we're going to have to think about whenever this loads
what's the very first sign well one thing is I want a list of stores to show up
here and that is going to come from the database so let's jump over into our
code and let's just start making that happen so I'm going to come over
right outside of the main window method we have inside of here and I'm going to
go private and what this is going to do is just update the store names in our
little interface so I'm going to call this display stores and there we are and
anytime we're accessing a database you are going to want to surround everything
with try catch blocks so let's just go and do that right now now the very first
thing I want to do here is I want to define a query that I will be using to pull
the store names out of the database that's going to be select everything from
and the name of my table in my database that has my store information is store so
there it is so I'm just defining a quarry inside of here and if you go
there's say store uppercase all right select everything from the store well
what do I need to do now well I'm going to show you mall I'm going to show
you at least three maybe four different ways of pulling information from a database
clearly in your code when you're writing this you're going to use the same
style of polling information you're not going to use an adapter sometimes and
not an adapter another time I just want to make you aware that there are different
ways of connecting and pulling information from a database so I'm going to show
you all of them but I'm going to show you like four of them okay so SQL uh data
adapter and what this will allow us to do is both connect to the database run
queries and also close the database command connection and I'll just call this
whatever they named it SQL data adapter sounds good and what you need to do for
this is you need to pass it the query that you we just created and then also an SQL
connection which is that guy right there all right so now what I'm going to do
is I'm going to say using SQL data adapter I am going to go and use our queries
and the connection to populate the list with the different store names so what am I
going to need inside of here well I'm going to need a data table so I'll
say data table and let's just call this store table which is equal to new and
data table there it is and there we go all right so there is our data table and
this basically is just going to act as an interface between the database and the
code that we're going to be using then I'm going to go SQL data adapter and
fill and we will be using our store we will be putting information into our store
table and then I need to define the column we are going to display in our list box
well that is our store name I want to display the name inside of there so what
we're going to do is I'm just going to say um store list Dot and display
member up member half and that's going to be equal to the name that we have
here and what else in store list is the name for our interface so store
list if we click on this look store list so we're referencing these names and
we're both pulling information from these as well as later on we will be
changing the information that shows up inside of there what else would I need to do
well I need to find what is unique about each item in our list well the names
potentially could be but I know one thing that will always be unique and that's
going to be the name so we'll go store list and we'll say select it or the
the ID is that what I said I don't know so I've been making this tutorial
for a while so I apologize if I said something incorrect the ID is unique okay good
then I need to come in and say store list item source and the content we will use
to populate the list is going to be defined here which is going to be our store
table which is right there and we call default you like that all right so now that
we have that set up what do we need to do well we have this inside of a try block
you might not notice it there's a little red line here and it says hey I
don't like what you got going on there that's because I didn't put the
catch part in here so I'm going to say catch exception and we'll just have
this BX and I'm just going to make this extremely simple I'm just going to
say message box show ex and maybe we'll do like two string or something so two
string like that and close that off with the parentheses and um yeah that looks
like that's going to work out so what we can do now is we can come in here and
test it actually it is going to crash I know it's going to crash because I
defined the buttons and gave them names and things like that and then I did not go
and create functions for them that is a requirement here you want me to prove it I
will run it so we'll run it and it's going to give me all kinds of Errors
say that's the error that it doesn't like it if I Define a button and I
give it a name and things like that it insists on those functions existing which is
fine so let's just go and write some more code just to think about the
simplistic things we need well one of the things I had up here was for this guy
right here the one that I actually have selected remember I had this this is
highlighted it and it says selection change store list selection let's go and
double click on this and look at that it went and created the function for me so
what am I going to have this do so anytime one of these items is um in the list is
selected what I want to do is I want to display my store inventory okay well that
means I need to create a function for that um so let's do it do I let's do
it here okay so what we're going to do is we're going to say private void
and display store inventory like that here we are and there we go all right so what
I want to do and this what this is going to do is just display the shoes the store
has in inventory and again we're accessing a database what should we do we need
to go try and we also need to do the catch block so let's go and copy this and
let's paste this down inside of here now what are we going to do here well I
need to go and find the shoes that match for a specific um a specific store and um
that means I'm going to be merging tables and such and what does that mean that
probably means we need to create an inner join I don't think I even need any of
this I'm just going to do this okay so I need to create a query that is going
to give me all the products that are held in a specific store so I'm going to
go string Corey I apologize that I can't also teach SQL Server I have many
videos on databases and such and if you guys decide you want me to go and make a
SQL Server tutorial I will just leave an information in the comments so what
I'm going to say is I want to select everything from the table name product and
I'm giving it an alias of P so I don't have to type product over and over
again and then I'm going to say how I want this data to be joined together so
I'm just type inner join and then I'm going to say I want to look in store
inventory the store inventory table that I'm going to call SI just for
convenience and on and in this situation I'm looking for situations in which
the product ID is equal to the store inventory product ID product ID like this and
also I'm going to say where s i store inventory dot store ID is equal to and
this is going to be the past store ID that is going to be provided and how it's
going to be provided is whenever they select the store in this list so whenever
they select one of these stores that's how we're going to be able to get
that additional information so um now to be able to to use past variables you have
to use something called SQL command so I'm going to go SQL command and the the
past variable is going to be the store that was clicked in this list box so
that's where that's coming from so SQL commands let's just call it SQL
command because I think that's what it wanted it to be called anyway so
that's good is equal to new and this is going to be SQL command query and SQL
connections passed inside it wrote all the code for me I don't even have to do
okay so now what I need to do is go in here and connect to the database run the
query then close it use a DOT adapter again probably going to do like some data
adapter stuff and then I'll just give it this name right there and then
I'll show you how to do it without a data adapter and SQL data adapter right
like that and with this guy we need to pass inside of it the SQL command like that
and there we go now what I'm going to do is I'm going to go and populate
the list box with the store name so I'm going to say using SQL data adapter and
we need the ID click to be able to perform this query SQL and this is why we use
SQL commands parameters and and this is going to be called add with value and the
value what I'm doing is see it has store ID here well I need to get the store
ID that was selected and then put it here so the query can actually be executed and
how I do that is I just say store ID like that and then I Define where it comes
from well the name of the list box is store list and what I want is the selected
value so I'm just going to go allow that and get rid of this part and the value
that they selected inside of that okay so now what do I need to do well I'm
going to get go and create a data table again and I'm going to call this
inventory table equal to new data table is and like I said before this acts as an
interface between the database and the actual code that we're writing and
we'll go SQL data adapter dot fill fill that inventory table that and I can
move this up here and then after that I need to define the column we want to
display in the list box so this is going to display let's let's say this
instead of the manufacturer because Nike obviously manufactures numerous Brands as
does every other shoe company so we're going to say store inventory um display
member half and what I want to display is the brand um I don't even know if
that's the right term but like if you have a Nike uh Air Jordans what I want to
display is the Air Jordan part of it um what is unique about each of these remember
we talked about this last time the unique thing is more than likely always going to
be the ID in this table so ID and what else do we have um the content we will use
to populate this list so we'll say store inventory Dot and items source and
that's going to be equal to the inventory table and default view just like
before now down inside of here we're going to have our little message box and
everything else there is probably going to be fine so what other things am I going
to have to do with interface well another thing I'd like to do I don't
think I need to do anything else here inside of the code part of this okay so what
else would I like to do well I'd like to display all the different products
that are here and then after I do that part then I'm just going to double click
on all these to create those functions and then I'll I'll make them work
but I want to make sure that what I'm what I've done so far is working
properly because if not well then that's a problem isn't it um for the last
one oh for store list selection changed what do I want to do with that well I said
that I want to go and call this function so I will call display store inventory
copy paste it down inside of here and there we go all right so we have all those
set up and now I just want to list all of the different products that we have and
it is largely going to be exactly the same as this so I'm just going to copy
this and I'm going to come down here right after this method ends paste that
inside of there and then this one's going to be called display why don't we
call it display all products products display all products and to do so I need to
say from and this all needs to go away don't need all that complication stuff
what I'm going to say is I just want everything from the products table that we
have created and this line stays exactly the same and the adapter stays exactly the
same to differentiate this I am I don't need this this part right here so we
just get rid of that all together this is going to be a product table so instead of
just leaving it in the inventory which wouldn't make any sense I'm going to
call this the product table and of course if that changes all these other ones have
to change so we'll say product table and product table Actually I don't
even know what parts I need here okay so I have this I don't need this fill
part so I can get rid of that and what
oh no I do need that oh I need that so SQL adapter fill product table of course
yes of course I need that and this is going to be called the product product list
let's get this out of here and product list is the name that I gave for the
list inside of my interface again I'm talking about this guy right here
it's a product list is the name of that list box let's paste this inside of
here product list and product list all right display member path what are we going
to display we'll just we'll display the brand that works ID's fine
product table all of that is fine so that all looks good all right good stuff so
what else am I going to need to do well now it gets to the point where I'm
gonna go and double click on all of these buttons inside of here so that the
functions were automatically generated so I can go test everything there's that
add store and after my store is added let's do add inventory let's do those
first and go back here and add product good and then we'll do the delete part
so delete delete inventory and then we will go and create those all right have all
that set now I think I have every uh function or method for every single one of
these let's run it and see if it works and it is not it's not grabbing the
data from my database and displaying it why is that I probably forgot to type
something in and of course I did um I need to call these these methods that I
created inside of the main window otherwise guess what they don't do anything
so I'm going to say display stores and I'm going to say the other one is
called display um um what I call it all products there it is there it is and close
that close that all right now it should work and I'm gonna go start and
hopefully it is going to yes it did look at that look how awesome this is look we
have made progress so I am trying to get this to work let's go like this and
hopefully you can see that it there's the names of my stores there's the
names of my products and I can go through them and now that I'm looking at this
maybe I want to give myself a little bit more space so let's go and select all
of this information and let's move it down I got all kinds of room here to work
so I might as well go and actually have these all be a little bit longer can I do
that without breaking the whole interface think that I can all right good so I
think that looks a little bit better for now so let's save that nothing broke
all right so great we got a couple things working here we're pulling
information from our database we're displaying that information and now what we
need to do is to make all our buttons work and then allow the allow us to add new
products and new stores and also change the inventory that we have in our stores
all right so let's do add store click first now what I like to do when I first
create a button is verify that it actually is working so I'm going to go
message box dot show and I'm just going to say add store clipped and that's
going to display if I click on that button so let's come up here and run it and
add store hopefully a message box pops up and there it did add store clicked shows
up right there so good stuff we know that our button works and I and I could do
that with every single one of my buttons which I normally would all right so what I
want to do now is I want to add a store well I have to add the name the street the
city the state and the zip code just to get the store name to show up over here so
there's a lot of information that we're going to need so what I want to do
is I'm going to list I'm going to actually go and create an SQL parameter
list and I'm going to store everything inside of it so I'm going to say
list and this is an SQL parameter and why don't we just call that parameters
that's good SQL parameters except this is going to have curly brackets and then
we're going to no I don't want that all right let's just do this and
let's go and get this and put it on the next line and go new SQL parameter that
part's good and then I need to Define all of the individual pieces now just
like we did up here um where did we do it we did it right here see at store ID we
are going to use this app symbol but then tie it to a place where we're going
to pull the information like the um it somewhere else where did I do it um where
did I do it sorority data table oh maybe I didn't do it I thought I did yeah I
didn't okay so this is the first time I'm gonna do it so I need to pull
this information from the text box that is right here did I give these texts yes I
did give these text boxes names let's just verify that so store name here it is
and text box and did I give it a name or did I forget to do that I did okay so for
each one of these I'm now going to have to give them a name so I'll be able
to pull information from them actually while we're at it while we do it for
this also so let's select this there's the text box and it needs a name
equal to and let's call this product Man U just to give it a little bit shorter
name and here this is going to be the name is equal to product and this is going to
be let's just call this brand it's a pretty short name after all then
we're going to do the same for here so we'll come in here and this is going
to be name is equal to and I want this to be lowercase I think I do I'm going
to make this store name like that and this I'm actually not going to change
much let's just copy this and go to the street area that's selected so
let's paste that in there and change this from name to Street Street and then
go and select whoops what did you not like store Street oh didn't like it
because that's there okay so store Street store City and paste make sure I put
a space there it will get upset as we see and and then I need the state so score
state that and that and then finally the store ZIP code I know I could put the
telephone number but I think this is enough information I think you get the point
and what should I call this let's just call it the zip and like this and like
this all right so now all those have information that I can pull from and also add
to more importantly so new parameter I'm going to reference the name so name
like this and then where I am well then you have to Define what type of data it is
it is a uh a variable number of characters long look at that it automatically
populated it for me the only thing is it forgot to allow me to put my value in and
where I'm getting it from so the value is going to be equal to store name that
is the name we just gave to our text box and if I want the text out of the store
name I have to type in text and now I got it so I'm going to be able to pull
that out of there and then use it to be able to add new stores every single time
they click on that so that's cool and the next thing I have is the street and
put these in the same order that your database is set up so names Street city state
ZIP all of them in exactly that same order so we have Street also variable number
of characters but this is called store street so let's change that to Street
and then on to the next one okay and grab this and this is going to be the city and
this is okay this is going to be called store City and come down to the next line
and this is going to be the state state like that fine oh state is different
isn't it state is not States let's go into our store area and table
definition and our city is our state is n characters okay so there we are so
we'll change that and this is going to be just get rid of the variable part and
that's cool and this is going to be store state and yep it found it and then on
to the next one and this is going to be the ZIP code so store zip and the zip code
is an integer so let's change this into an integer like that and the name of
this is zip like that and text is cool we don't need this I think you don't
it doesn't bother you if it's in there but I got rid of it anyway okay so
now what I need to do is go and create a quarry to access this and this is going to
allow us to insert values into our database so I'm going to say the query is
going to be equal to and this just simply says insert into the table name store the
values that we pulled from our text box and we're going to put them in order so
remember in name we're getting these from the text boxes that we have at Street
and at the city and at whoops at the state and finally at the ZIP code and there
that is and there that is all right so now what let's uh go SQL command and SQL
command there just let it do it for me there we are and here we're gonna go SQL
connection and open and then what I'm going to do is go SQL commands and
parameters Dot and we have a range of parameters to pass so we're going to go
add range only problem is we want to put our parameters inside of there and
we're going to have to convert that into an array so all right like that and
there we go all right then again data table just like we did previously multiple
times I'll call this store table is equal to new data table and then
there's an abbreviated way to use the adapter why don't I just show you
that right now so I'm going to say using SQL and this is going to be data
adapter like that and let's call this adapter is equal to new and SQL data
adapter SQL commands and then at the end of this I can just say adapter fill with
our store table there we go all right anything else we need to do no that's it
all right so uh actually the whole thing needs to be put inside of a try block
forgot to do that uh let's go try like that and then
let's go and copy this all this stuff and cut that out of there and paste this
inside of here and there that looks good and it's complaining because it wants
me to catch an error I'm just going to use this default and I'm also going
to show you finally which actually finally finally has a use and its use is that it
is going to close the database we always want to close our database so we're
say finally and inside of here we'll go SQL connection and close look at that
and then I want to call my display scores so that we are able to go and see the uh
the stores all show up inside there obviously all right let's run it and this
is supposedly going to allow us to add new stores so will it we'll find out
let's just call this um a b c and one two three I don't know South and city
and state and there's one add store up it gave me an error why was it uh
incorrect syntax near zip doesn't like the zip all right let's figure out
what's going on with that I know what the problem I forgot to put a parenthesis
there we are that's it just that one little simple problem let's run it
again and let's go ABC it doesn't matter what this is so I'm just gonna
throw anything inside of it and like that and add store and there it is ABC say it
shows off nothing else is going to work yet because we haven't implemented
anything else yet but we will get to it so what's up next well I'd like to
add inventory so let's go and set it up so that we can do that so test your
button I'm not going to do it this time but it's always a good idea to do
it all right so except one I don't want to all right so this time I'm going
to be inserting um uh both a store ID as well as a product ID so I'll say
insert into the table called store inventory the values and those values are going
to be store ID and the product ID and don't forget the parentheses and there it
is all right now what am I going to do basically the same stuff so I'm going to
need all of this and I'm going to need open that's good add range I do not
need um I'm also not going to need okay so I just need these two things so
let's just copy that just so I don't have to type two things and paste this
down inside here there it is okay so got the command I open the database now what
I'm going to do is I'm going to say let's do a different so we'll
say SQL command dot you can also do it this way so we'll say parameters and add
with value see I said I was going to show you multiple different ways of doing
things well here's another way at store ID and store list Dot selected value
and that's the value they selected in the list the uh list box so that's
where that comes from and down here paste this here and this is going to be our
product ID so product ID and this is going to be the product list that's the
name of it in our interface we'd have signed over here I don't think I need
to show you that again and then you can just simply come in and say SQL command dot
execute scalar like that good and we can go and copy this verbatim this is exactly
the same so copy and paste that inside of there there we go and now we will be able
to add inventory quite easily as well um up next I'd also like to be able to
add products so how do I want to do this is there code that I can copy it's
always nice to copy code um this I think I'm going to copy like all of this one
because it has multiple different parts and I like that so let's just select
that and copy this because it's almost identical and I could almost definitely
put this into a separate method to save code duplication but you know this tutorial
is very long and I'm just gonna do it this way all right so I just pasted it in
there what do I need to do this is going to be a list of parameters that we're
going to be defining again so what are they well then we're going to have a
manufacturer to factor sure and this is for the name it has in the table in the
database and is going to be a number of characters and where are we going to get
this from well we have a text box that's called Product manufacturer that's
the text box name and I want the text from it and that's good and I do not need
this we're only getting the brand and the manufacturer nothing else and I this
is called brand and this is also a variable number of characters and the value it
is going to be coming from is going to be called Product brand product Brands
product brands and just so you're 100 aware the name that this is where
it's coming from the name of that is product manufacturer name of this is
product brand I know sometimes things don't click and uh so I want to make sure
that you understand exactly where all of this information is coming from okay so
now we're going to have a query what is the query well I'm going to be
inserting into and I'm going to be inserting product this time and values and
the specific thing I'm inserting is manufacturer that's where that goes and
brand s here brand all right so that's where they're going to go and then
after I have that I do need the SQL command I obviously need the ability to open
this range yes I'm going to be adding a range of products data table um for
this one let's go and call this product table just so that there's
consistency because this has absolutely nothing to do with a store so product table
this is fine this is fine do I need to do anything else using I do not think I do I
think everything else here is perfect except for this part right here this is going
to be called display all products all products and then that is going to be called
just so that it updates the information that is in the product area all right so
we're able to add products we're able to add inventory we are able to add
stores so we have all of that awesomeness inside of there now what I'd like to
do is be able to delete all those things so inside of here what do I need to change
um and and can I copy more importantly can I copy what I already created up here uh
yeah let's try this one so let's copy this through this copy and even
though we're deleting a lot of the things we're going to be doing are going
to be very similar to what we did before now what's going to change here well
this is going to completely change so I'm just going to get rid of that I'm
going to say delete from the table called store and then I have to Define how to
delete I'm going to say where the ID is equal to the store ID that I'm
going to pass it and I'll show you how to get the store ID and I have this
command I have open I am not going to be I'll add with value I don't need
two of these I just need one of them so let's get rid of that add with value
and this is going to be store ID just like before and it's going to be store
list selected value so it's going to delete whatever is selected in the list
box and execute scalar all good this is good that's good the close is good and
and guess what I'm deleting from stores so I want to update the stores the
store list so that's also very very good all right anything else I'd like
to do I don't think so I think that's pretty good well one problem however
is if I delete a store then this is not it's gonna mess up my other table that
I have my store inventory because if I have store IDs inside of here and I delete
stores well that's going to cause all kinds of problems so what I want to do is
I want this to update and uh how you can do that is by right clicking on the store
inventory you can you can have the database basically do it for you we're going
to say open table definition and let me come up here and let's see did I
already add it on delete Cascade I added it I guess I added it previously and I
didn't know that go and verify but yeah just type in on delete Cascade and what
it's going to do is it's going to automatically delete all of that
information that doesn't pertain anymore because stores have been deleted all
right good well that saved time all right anything else I need to do well we got
that all set up and I need to go in and delete inventory so I'm just going to
select this and see I think most of it's the same so delete inventory paste
this inside of here now this time we're going to say that we want to delete
from and this is going to be not store but it's from a table called store
inventory where the ID is equal to and in this situation we're going to be
deleting product IDs and what else okay SQL command that stays exactly the same we
open just like we did before and here what I need to do is add value and what
we're going to be passing over are product IDs and what else the selected value
that we have here also good and execute look at that so good and the thing
that's going to display here is I need to say that I want to display store
inventory because that changed so the store inventory changed and I want to update
that list view also and did I I don't know if I copied anything else let's
just copy this and let's continue so let's say I want to delete a product
how can I do that well this is largely going to be the same also so I'm going
to say delete from in this circumstance I'm going to be deleting from product
so product like that and where the product ID is equal to the product ID of course
the Esco command the open add value the value I want to add is the product ID and
this is going to be product list not store list so product list and execute and
everything else is good there now this is going to be called display all products
because that's what
it's called and that's going to update the view and there we go all right
so if I don't have anything wrong here everything should work the whole old
database should be working now which is awesome okay so we added some new stores so
let's just go here to this store and let's say we would like to add this
shoe to that store both were selected at the same time and I am going to say add
inventory uh oh okay so we got a problem here it says that parameterized query
store ID select from expects parameters store ID which was not supplied okay so
inside of that for adding inventory so let's go and let's take a look at
what's up with that well in reviewing the code I actually saw a couple problems
okay so this right here the data adapter let's go and this is not SQL command
is not passed to that what is passed to that as you saw with every other
circumstance is the query and the SQL connection like that okay so that is going to
fix that problem that came up another thing that came up this is um down in add
inventory click I don't need to display the stores what I need to do is display
the store inventory that is what changed see that's what happens whenever you
rush through typing code you sometimes call the wrong thing no problem let's
continue down inside of delete inventory click what I'm looking for is I had
where ID that is not correct what this is supposed to be is product ID is equal to
the product ID that was provided and I think I saw another problem here um also
like my parameters add with value product ID this is going to be hmm store
inventory that's what's wrong this is store inventory say that I got that
messed up so this is store inventory is where I need to update that obviously
because I'm working with the inventory and now let's go and see if
everything that was the bug that it threw was the last one that I did there I just
noticed those other two bugs as I was looking at other things all right what else I
wanted to do oh I wanted to go to my new store it's called XYZ oh look it did
update so it updated but then something else messed up so we're gonna go there
327 add inventory hey look at that it works and now what I'd like to do is add
a new product so I'm going to add product I'm gonna have to go manufacturer
Nike and we'll say Air Jordan's one I don't know add product is it
gonna show I think it did all right so that works and let's say I don't
want the Air Jordans I'm going to select them delete product also works also
awesome I already showed you that I can go and work with a store and I guess the
only thing I haven't done is go in here and delete inventory is this gonna work
we're able to add new and from add new products to new inventory new Pro
everything and the database automatically updates thank you to any human being that
has sat through this entire tutorial I meet I'm just testing new things and
trying to provide the absolute greatest content that I can possibly provide if you
are one of the probably five people that watch this entire course please take one
moment and put a comment at the bottom saying you watched it I will be shocked and
if you want to see more gigantic all-encompassing courses like this tell me and
I'll do it and like always please leave your questions and comments down below
otherwise till next time