The Best C# Programmer In The World - Ben Perkins Member Login  | Newsletter | RSS Feeds


 
 
 
TheBestCSharpProgrammerInTheWorld.com 
 
The Best C# Programmer In The World - Ben Perkins
Conditional Methods in C#  
 
Conditional Methods
 
One of, if not the most important concepts in software programming is that of Conditional Methods. Conditional methods provide a program with the ability to make decisions based on user input, user selections or data values. The code below is a simple example of a conditional method:
 
                        
                        public static void Main(string[] args)
                        {
                            Console.Write("Please enter any value: ");

                            string enteredValue = Console.ReadLine();

                            if (enteredValue.Length > 0)
                            {
                                Console.WriteLine("You entered: {0}", enteredValue);
                            }
                            else
                            {
                                Console.WriteLine("You didn't enter any value.");
                            }

                            Console.ReadLine();
                        }
 
When run, it opens a console and requests the user to enter a value. It then checks to see if something was entered. Here is the condition... IF there was a value entered, display it ELSE notify the user that nothing was entered. In a more sophisticated software program the question or condition could be more complicated. Perhaps something like this:
 
                        
                         public static void Main(string[] args)
                        {
                            //add some logic to access a database to confirm you have
                            //ample funds change the value to TRUE if we want to deliver
                            //the dinero
                            bool amountApproved = false;  

                            Console.Write("Please enter amount to withdraw: ");

                            string enteredValue = Console.ReadLine();

                            if (enteredValue.Length > 0 && amountApproved)  
                            {
                                Console.WriteLine(
                                    "The amount entered: {0}, will be dispersed.", 
                                    enteredValue);
                            }
                            else
                            {
                                Console.WriteLine(
                                  "Sorry, the amount {0} exceeds your " + 
                                  "available credit.", 
                                  enteredValue);
                            }

                            Console.ReadLine();
                        } 
 
Although this is a basic concept, it needs to be understood, utilized and mastered before moving to more complex concepts.
 
Download the source
 
 
 
Feedback / Question
 
Your Name:Your Email:
 
Subject:
 
Feedback/Question:
 
 
 
I had to remove the capability to leave feedback due to this. Will be back soon.
 
 
page.Translate()
 
 
blog.Stats()
 
  Posts: 113
  Comments: 86
  Fundamentals: 16
 
my.Book()

 
me.About()
 
 
 
 
 
blog.Archive()
 
2012 May  (4)
2012 April  (5)
2012 March  (4)
2012 February  (4)
2012 January  (5)
2011 December (2)
2011 November (6)
2011 October (7)
2011 September (7)
2011 August (9)
2011 July (9)
2011 June (8)
2011 May (9)
2011 April (7)
2011 March (9)
2011 February (8)
2011 January (8)
2010 December (7)
2010 November (8)
2010 October (4)
 
site.Visits()
 
free counters
 
tag.Cloud()
 
code.Disclaimer()
 
The sample code on this website is provided to illustrate a concept and should not be used in applications or Web sites without proper professional consultation, as it may not illustrate the safest coding practices. I assume no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.
 
   


The Best C# Programmer In The World - Ben Perkins, © 2010, All Rights ReservedContact Ben