Content Obsolete

This content is no longer current. Our recommendation for up to date content: http://channel9.msdn.com/Series/C-Fundamentals-for-Absolute-Beginners

Creating and Calling Simple Overloaded Helper Methods - 10

Download this episode

Download Video

Download captions

Download Captions

Description

In this video we begin wading into the topic of methods by creating a helper method to break out code we may need to use in multiple places within our code. We create and call our methods to retrieve a value, create and use input parameters, learn about string formatting, and create overloaded versions of our method

Download the source code for Creating and Calling Simple...

Tags:

C#, Visual Studio

Embed

Format

Available formats for this video:

Actual format may change based on video formats available and browser capability.

    The Discussion

    • User profile image
      santi_m

      Overloading i learnt today, which i never able to understand going through other site. period.

    • User profile image
      subaddiction

      i can't download any vidoes, it says your servers are down! please put it back up  Tongue Out

    • User profile image
      subaddiction

      Thanks... The servers are back i can now download this videos  Big Smile

    • User profile image
      Kevin

      Just a quick question is there a reason why the methods were written after the program code? Can you write methods first and then call them later?

      Thanks.

    • User profile image
      BobTabor

      @Kevin: GREAT QUESTION!  First, you can always try stuff like this out on your own -- and you should!  It's a great way to learn the propensities of the environment you're in.  In this case, it does not matter -- you can put the code anywhere.  What matters, as you'll learn soon, is the NAME of the method, both it's method name AND the namespace it belongs to.  The compiler is smart enough to link up a method call -- regardless of where it's defined -- with its definition.  Again, love that question!

    • User profile image
      claudioas20​01

      Great, just a  question. Is any video where you explain ref, out and arrays params for methods? Thanks Bob, your videos and the way how u explain the topics are easy to understand. 

    • User profile image
      BobTabor

      @claudioas2001:  Out parameters, yes.  At some point we talk about TryParse and in that video, TryParse will send a converted value as an output parameter.  Unfortunately, I can't recall which video that is in ... someone recently asked for help with it, so look for a long passage / reply from me on TryParse.  It's a simple concept however ... output parameters are a second way to return values from methods (the other way being the 'return' statement).  Just prefix a parameter with out, like so:

      string value;
      
      double number;
      
      
      
      value = Double.MinValue.ToString();
      
      if (Double.TryParse(value, out number))
      
         Console.WriteLine(number);
      
      else
      
      .
      
      .
      
      .

       

      re: using arrays as input parameters.  Personally, I hate arrays for passing data around in real applications.  I much prefer using generic collections.  I believe I do talk about generic collections near the end of this series and in more depth on my website (see the C# for Absolute Beginner's series, day 5).  Hope that helps!

    • User profile image
      Kieran

      Bob,

      Regards from Ireland!

      Loved your videos, you are a fantastic teacher. Where would you recommend to go from here as far as learning materials go?

      Thanks for the great series!

      Kieran

    • User profile image
      Tesfaye

      Bob, while wandering through so many programming courses, I have long strugled to understand basics of programing. Bob, you are simply best!!

    • User profile image
      GusCodesFor​Fun

      I love the length of these videos. It's great to throw a little quick learning in every day. Thanks!

    • User profile image
      BobTabor

      @Kieran: Sorry for the delayed response.  I'm constantly creating new content / entire courses on my own site if you like my style.  www.learnvisualstudio.net ... hope I can help! -Bob

    • User profile image
      Bill

      Thank you,Bob; you are a fantastic teacher, and the content is A+++. You take a subject which is like Greek to the neophyte and make it logical and understandable. 5 Stars!!!

    • User profile image
      paekyaw

      Thank you,BoB.

    • User profile image
      raju

      all videos are ********
      thanks bob

    • User profile image
      BobTabor

      @Bill:  @paekyaw:  Thank you, much appreciated.

      @raju: I hope ******* is a good thing?  Smiley

    • User profile image
      fifi

      just got to understand the concept of overloading...thanks bob you make it seem so simple and straight forward..

    • User profile image
      jbouwens

      Thank you for the video.

    • User profile image
      shashankonl9

      thanx..BoB..

    • User profile image
      DDom23

      Why doesn't this work? I'm probably doing something stupidly wrong.

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;

      namespace TestingTesting
      {
      class Program
      {
      static void Main(string[] args)
      {
      string[] names = {"Bobby", "Steve", "Chad", "Bart"};

      foreach (string name in names)
      Console.WriteLine(name);
      Console.WriteLine("");
      Console.WriteLine("Press any key to continue.");
      Console.ReadLine();

      string numberChoice;
      Console.WriteLine("Choose a number between 0 and 3:");
      numberChoice = Console.ReadLine();

      string myValue = superSecretFormula(names[numberChoice]);
      Console.WriteLine(myValue);
      Console.ReadLine();
      }

      private static string superSecretFormula(string name)
      {
      return string.Format("Hello World, and {0}.", name);
      }
      }
      }

    • User profile image
      BobTabor

      @DDom23: What is the error message you are getting?  Which line does the error message reference?  Or are you simply getting results you did not anticipate?

    • User profile image
      DDom23

      Its ok now. I forgot that readline forms a string and had to convert it using convert.toint32 before I could use it.

    • User profile image
      Dinesh

      Hi Bob,

      Order of parameters also matters, for example
      SuperSecretFormula(string name, int age) is different from
      SuperSecretFormula(int age, string name)

      So Number, Type and Order of parameters are considered in Method overloading.

      Thanks

    • User profile image
      BobTabor

      @Dinesh: Yep, order matters.  Smiley

    • User profile image
      luiztba

      Tenho grande dificuldade no ingles... Mas quem quer consegue.

    • User profile image
      JG  Hayes

      WHOA! I just wanna say I got my start watching your videos back when I started programming in '08, it's awesome to see you again!

    • User profile image
      NIthin

      Hi bob,
      your tutorial is great. i am in need of creating an app for tablet running windows 7 OS. what all tools or SDK's will i need to set up before creating one other than visual studio?? and if a Winforms app enough to run on tablet?? i need your help in that..thanks in advance...

    • User profile image
      BobTabor

      @JG Hayes:Awesome ... are you still writing code?  Let me know what you're up to!

      @NIthin: I'm really sorry, but I have absolutely no knowledge in what you're asking.  If it's an old-style tablet it should "just work" ... the OS takes care of pen / virtual keyboard input ... I think.  Again, I'm not the guy to ask about that. Sorry!

    • User profile image
      Akram

      Hi bob

      I am just a beginner in Programming well i saw your few videos and i would like to be learn c sharp .Please if you would recommend me to go through your video which will help me to learn c sharp for future.

    • User profile image
      BobTabor

      @Akram: Hi, I may not completely understand what you are asking.  However, I would say just start here and watch them all, and be sure to follow along.  Once you finish this entire series, you'll have the basics and can move on to learn more about (1) composing classes / delegating responsibilities / designing interactions, etc.  (2) the .NET Framework APIs for things like data access, some front end client technology like WPF or ASP.NET WebForms or MVC.  Could you clarify your question?  I'd be glad to point you in the right direction if I can.

    • User profile image
      milan

      Hi Bob,

      Thanks for putting so much efforts and making these videos. The videos are awesome.

    • User profile image
      Jonagesh

      @DDom23:  Error is in the below line

      string myValue = superSecretFormula(names[numberChoice]);

      numberChoice is an string variable, You need to use int.Parse method to covert it into int. replace that code with below.

      string myValue = superSecretFormula(names[int.Parse(numberChoice)]);

    • User profile image
      BILAL

      How can i download core c# course complete videos??

    • User profile image
      Yodatech

      Hi Bob,

      I have been going through your video every chance I get, all day long when possible.

      Its very rewarding and comprehensible. Although, the "Buffering" is annoying.  What can I do, other than pausing it for the buffering to catch up?  It interferes with my train of thought, especially when you are sharing a new concept for me.

      @Bilal

      I would like to download the video as well and have been unsuccessful.

       

    • User profile image
      ElpDev1

      Great Lesson on calling the same method with different versions. I can already see how to apply this in a real world setting.

    • User profile image
      L Cat

      I love your holly videos, you are the only fantastic C# God for me as I am just a zero beginner and watched your videos for WinPhon7! I have already coded one application...

    • User profile image
      L Cat

      I love your holly videos, you are the only fantastic C# God for me as I am just a zero beginner and watched your videos for WinPhon7! I have already coded one application...

    • User profile image
      Nhery

      Guys just restart your computer is its buffering. works for me

    • User profile image
      Hsls

      Great videos and even I learn much from them! Thanks!

      However, is the camelCase naming of the methods intentional? According to Microsoft naming conventions for .NET PascalCasing should be used for methods. http://msdn.microsoft.com/en-us/library/vstudio/ms229043.aspx

    • User profile image
      carlwain74

      You mentioned in the video that you would explain why you had to define the helper methods as static. I read that you only need to define methods as static if the class is defined with static. Maybe my source of information was wrong.

      Could you correct me on the why you needed to use the static keyword on the help methods.

    • User profile image
      Clint

      @carlwain74: Since this application is a basic console application, since Main must be a static method, any method that it calls must be a static as well.

      You don't have to have a static class to have static methods as well in c# at least

    • User profile image
      carlwain74

      But the helper function needed to be static in addition to main. This is something I don't understand.

    • User profile image
      Clint

      @carlwain74: it does.  Since main is a static function, it can only access static members / functions from within its own class.  If you create a new object like StreamReader from within main, you can call non-static members on that new object.  But any method or member within the Main here since it has to be declared as a static must be static as well to be accessed.

      http://en.wikipedia.org/wiki/Static_method

      Once you go to a non-console application, this goes away as well since they are not static by nature

    • User profile image
      carlwain74

      @Clint:Thanks for the explanation.. Now I understand.

    • User profile image
      SteveHtml

      Hello Bob, First I've been following tutorials on MSDN 9. THANK YOU & CLINT & Ms.GOLNAZ !!! I can't tell you how UNDERSTANDABLE you've made C# for me ( and a million others ) !
      My 'mystery' is : simple console program runs a method , declared Outside the "Main" codeblock ( see in the screenshot 2 private methods ) Nowhere OUTSIDE of the method do I run Console.WriteLine() , BUT my method ("Print_Number()") RUNS & prints to console. I was expecting to see a blank screen. Actually started w/ "Console.WriteLine( abc + "\n" + xyz ) " & xyz was printing to console window first. I would think I'd get a "Variable Declared but Never Used" exception...
      Oh & I hit Clean Solution, Rebuild Project , Closed & Restarted Proj. & Visual Studio , moved methods below "MAIN" block, have not gone back & made methods 'static',still experimenting with constructor method
      Any thoughts greatly appreciated , Thanks
      Ben ( using VisualStudio 2012 Express on Windows 7 Home Pre. , 8 Gb Corsair memory , iCore 7 cpu )

      namespace SimpleMethodLsn
      {
          
          class Program
          {
              private string HelloWorld ( )
              {
                  return "Hello World Method is Called Here";
              }
      
              private int Print_Number ( )
              {
                  Console.WriteLine ( "This line is inside the Print_Number Method " );
                  return 101;
              }
      
              static void Main ( string [ ] args )
              {
                  Program p = new Program ( );
      
                  string abc = (p.HelloWorld() );
                  int xyz = ( p.Print_Number ( ) );
      
               // NO CONSOLE WRITE EVER CALLED !
                  
                  Console.ReadLine ( );
              }
      

      Hope this isn't being too much of a pain, don't see where to post a screenshot.Thanks again.

    • User profile image
      Clint

      @SteveHtml: first, we're trying to keep the comments to the topic of the video, here is it overloads.

      First, there are things to help debugging like breakpoints and Debug.Writeline(string) method that can help figure out what is going on.

      Next, Console.WriteLine is called, it is called in your Print_Number method.  What you're doing is allowed.

    • User profile image
      SteveHtml

      @Clint : Wow , Thanks Man for the responce, didn't really expect one @ this late date ( or so soon ).
      You said " keep the comments to the topic of the video, here is it overloads." , for that I apologize Clint, you guys just shed a lot of light on things I've studied but never really grasped, so again apologies ( will keep on topic from now on ! ) Thank you for taking the time, just seemed the craziest thing to me.... Thanks Clint!

    • User profile image
      Buchi

      Hi Bob
      Thanks a lot for these video. please do you have any video on ASP.net
      I need it.
      thanks

    • User profile image
      Firebat

      You are a truly excellent tutor. I was starting to think that programmers make bad teachers until I found this video series. Thank you very much, sir.

    • User profile image
      Edwin j

      Hi, what is an "input parameter"?

    • User profile image
      Clint

      @Edwin j: been a while since I watched the videos so my context may be slightly off.  An input parameter is a variable that takes in data.  INput versus OUTput.  This terminology would typically be used with method calls where we would expect to pass in data, like if I had "public int CalculateCoffeeCupSize(int ouncesOfCoffee)", ouncesOfCoffee is an input parameter and it returns an int as an output parameter

    • User profile image
      Guust

      Great video

    • User profile image
      Mmiz

      {
      string myValue = superSecretFormula("Mery");
      Console.WriteLine(myValue);
      
      myValue = superSecretFormula("Philip");
      Console.WriteLine(myValue);
      
      Console.ReadLine();
      }
      private static string superSecretFormula(string name)
      {
      return String.Format("Hello, {1}", name);
      }
      }


      This gives me a popup when I try to debug it:

      An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

      Additional information: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

      figured out I had to change change this line:

      //return String.Format("Hello, {1}", name);
      //to
      return String.Format("Hello, {0}", name);

      But why?

    • User profile image
      sadiq

      i want to learn c# any body help me?

    • User profile image
      Ryan

      How come the 2nd call:

      myValue = superSecretFormula("Steve");

      only works without "string" preceding it?

    • User profile image
      NeoChing

      @BobTabor:

      Dear Bob,First thanks for your video!

      there have some question:

      Why the "stantic viod Main()"have parameters "string" named "args"?

      Have some place to use this?

      Is them result to we must convert some variable?

      thanks argain.:)

    • User profile image
      Size3

    Comments closed

    Comments have been closed since this content was published more than 30 days ago, but if you'd like to send us feedback you can Contact Us.