Tip:
Highlight text to annotate it
X
Hi, I m Bob Tabor with LearnVisualStudio.NET
In this lesson, you will show you how to create methods that allow us to split our C# code up into
multiple code blocks
you might say why would I want to do that
Well, one connect to that and just a moment explain in a high level what a
method is
simply put methods of blocking code that's given in name
so they can execute that blocked the code
by just simply calling its name some other block of code
it's one of the basic building blocks of building larger applications
along with classes which will learn about
in just a little bit
so why would we ever want to split up our code
in the multiple methods
actually there's a lot of differences why I give you just one for now
it's always a good idea
Do never write the same code twice
Cut and paste is the sworn enemy of the software developer If you find yourself
copying code from one spot in pasting it into respond to a little bit lower you
should stop yourself getting your track to say wait a second what we're doing
here
uh... actually create a method and then call that block of code
from every place i needed
within my code so this way you're limiting the amount of code that you
need to change
should there be a change your requirements
it will limit the amount of code that
you would need to look at if above work pop up in your application
so we work with methods up to this point we called methods like on the console class
we've also routing code within the main method many times in the main method is
just a special
uh... method
that is triggered execute whenever application begins
so what i've done here just to sort of illustrates some more ideas about
methods
is that created a new project called Helpermethod you can already see i've
taken the liberty to do that so positive record you need to catch up
uh... so i have a project called hopper methods
and the first thing i want to do inside of our static void main is
start out here
Now I want to do is go to the closing curvely break for static void main
Need a little space
I am creating new method
private string
Super secret formula again noticed the naming convention I use here snubbed
forced it's just a way of creating names inox plane that naming convention a
little bit later
alike a promise before
Last thing I want to do then
is go back
here
remove the double quotes and set my values equal to super
secret
formula
open in close parentheses if you recall
that is our
uh... method invocation operator
so what are we doing here were first of all we're creating a variable called my
value and initializing it too
the return value of this block of code name super secret formula
we defined that block of code here private string super secret formula
let's not worry about the were private just now will come back to that a little
bit later
i think that we needed note for the moment is that we should just expect
back
from this method of value it should return its value of type string
and that's exactly what this the body of this method
does it returns ustream valued which i've heard committed to the world
hello world
does a very simple formula we probably want to
uh... add number lines of code to
performe a number of operations i'm keeping it very simple at the moment
So it might seem a little monday
we were to cover mouse cursor over this resolutely line will see that
an arctic references required for the non static field method or property
helper methods programme and so on
Alright, so reasons that i really don't want to explained at the moment
what we needed to use chest add the word
static
here
private static string super secret formula
again i don't want to explain why to except that
as
fact for the moment
let's go ahead and run the application
Yiu can see we simply get to word "Hello world" so very
round about the way of creating a hello world application or actually interested
in the
manner in which we were able to create that
calling a method
we were able to gain the benefit
of that method
in this case that simply return to stream but could do number of different
things
and they were able to take that value and write it
to screen or whatever we may want to do
so the example is awfully simple but the concept is powerful we could offload
operations
other methods
to organize and reuse code
Furthermore, the helper methods can in turn call other help a rapids and so on
begets a powerful step
towards building more complex applications so let's improve our helper
method to make a more flexible and useful by allowing it to accept
and input parameter
we can creator methods
to accept zero input premise what we've done here
so between are opening and closing
parentheses we define no input parameter
or we can define it so that accepts one or more input parameters
and then in the body of the method we use those values that we pass and
to do something meaningful in our case really keep it simple words to make
except a single input parameter
that will didn't used to attend to the end of that literal string hello world
so this is what we're trying to accomplish shall be comin out that one
of code
kind of redo it
super secret formula
and i'm gonna pass in the workplace
notice when i do that by hermann oscar serial say
no overloaded method for super secret formula that takes one argument that's
because currently
super secret formula
takes no work and so what i want to
it's actually
highlight this whole area using the keyboard
and then it hit control c
and then control v
to paste
and i know i just said copy paste is an enemy but there's a reason why did this
time
come back to that at the moment
so here's what i want to do
is
created input parameter of type string called name
and then what i want to do
is
so this is really just a fancy way of doing
basically this
the string dot format allows me to use that
insertions syntax that allows me to do what i did with the consul downright
literally or
so i can't pass into
opening close curly priest sub-zero so this would be the first item the listed
would get substituted inside of that insertion point okay
soon now that we were to run the application
we concede that
our method does a little something more interesting so
computers
copy-pasting unscripted
sti
snot ronnie application again
we have problem here because i'd use copy pati see how insidious that can be
so let's do this
that risk reassignment value
except there
statistic
sorry hello world bob dole the world speed we were able to reuse r_c_
seatbelt super secret formula
uh... four two different values so it
helps us by four manning our strain
the way that we need a format
let's take that line of code here emina just cut
pace and just get rid of my value altogether
so we could right like this again super secret formulas returning a string so we
can
easily in bed
the results of one method inside of another method
furthermore if i were to
select this original version of super secret formula and select un comment out
the selected lines
notice now vats
first of all i have too
to versions of the exact same method
one version
that accepts no input parameters
invent a nother version that accepts a string
called name
so
that's actually ring
move
two-word steve
okay and call
and now we have hello world bob and then just tell the world we called two
different versions of our methodist calls
overloading the method we haven't overloaded method
two versions of the same method
the only difference is the signature of that method
in this case the signature has no
uh... input parameters in the second it hasn't input parameter of type string
it's interesting that the dot at one time a smart enough to know which
version
of our overloaded method that we want to execute based on the method signature in
other words
uh... the number types of parameters
input parameters
as were calling
that mehnat
that uh... that metheny okay
dickey take away in this lesson
is the use of methods
will be using methods often the upcoming videos for various purposes and i just
want to make sure that he had some exposure
uh... and explanation to them as we get started
as i said at the outset a method is just one building block we're gonna need to
understand a little bit about classes as well
which you can think of it as means blocks of code that are containers
of our methods will talk about this at length
in an upcoming lesson we'll see you then