SQL SAMSON

everything sql

Archive for the ‘visual c# 2008’ Category

Visual c# 2008 Hello (Unicode) World (Hexidecimal)…

leave a comment »

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

namespace HelloWorld
{
   class Program
   {
      static void Main(string[] args)
      {
         ConsoleTitle();
         Greeting();
      }
      static void Greeting()
      {
         string strHello = “\u0048\u0065\u006C\u006C\u006f\n”;
         // Unicode of Hello

         string strWorld = “\x77\x6F\x72\x6c\x64\x21\b”;
         // Hexidecimal of world

         Console.Write(“{0}”, strHello);
         Console.Write(“{0}”, strWorld);
         Console.ReadLine();
      }
      static void ConsoleTitle()
      {
         string strTitle = “Hello(Unicode) World(Hexidecimal) v0.01”;
         Console.Title = strTitle;
      }
   }
}

Written by Samson Loo

September 26, 2008 at 12:17 am

Visual c# 2008 Centimeters to Inches Calc…

leave a comment »

My spouse needed to figure out what 83 cm equaled in inches. I know the formula, so I could have easily written this out by hand…but why? I also know that I could have written this using one file…but why? This is another example of how to separate methods into separate class files.

Well 1 inch = 0.393700787 centimeters, so 83 * 0.393700787 = 32.677165321 inches!

[ Program.cs ]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CM2IN
{
   class Cent2Inches
   {
      static void Main(string[] args)
      {
         SetConsoleTitle.ConsoleTitle();
         GetUserInput.UserInput();
      }
   }
}

[ SetConsoleTitle.cs ]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CM2IN
{
   class SetConsoleTitle
   {
      public static void ConsoleTitle()
      {
         string strTitle = “Centimeter to Inches Calculator v1.0”;
         // Set Console Title
         Console.Title = strTitle;
      }
   }
}

[ GetUserInput.cs ]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CM2IN
{
   class GetUserInput
   {
      public static void UserInput()
      {
         string strUserInput;
         Console.Write(“Enter Centimeters: “);
         // Prompt for user input
         strUserInput = Console.ReadLine(); // Read user input
         ConvertUserInput.ConvertInput(strUserInput);
         // Pass user input to Convert Class
      }
   }
}

[ ConvertUserInput.cs ]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CM2IN
{
   class ConvertUserInput
   {
   public static void ConvertInput(string s)
      {
         double d;
         d = Convert.ToDouble(s); // Convert string to double
         PerformCalculation.Calculate(d);
         // Pass double value to Calculation Class
      }
   }
}

[ PerformCalculation.cs ]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CM2IN
{
   class PerformCalculation
   {
      public static void Calculate(double i)
      {
         double cm = 0.393700787;
         double total;
         total = (i * cm); // Perform Calculation
         DisplayResults.Results(total);
         // Pass Results to Display results Class
      }
   }
}

[ DisplayResults.cs ]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CM2IN
{
   class DisplayResults
   {
      public static void Results(double r)
      {
         Console.WriteLine(“Total Inches: ” + r); // Print results
         Console.ReadLine();
         Console.Clear();
      }
   }
}

Written by Samson Loo

September 22, 2008 at 6:28 pm

Visual c# 2008 Hello User 1.2 Console App…

leave a comment »

I decided to split up the code into two files to illustrate how to call a separate class. It is overkill for such a simple application, but it is an example!

[ Program.cs ]

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

namespace HelloUser
{
   class Program
   {
      public static void GetUserInput()
      {
         string strInput;

         Console.Write(“Enter your first name: “);
         strInput = Console.ReadLine();
         Console.WriteLine(“Hello “ + strInput);
         Console.ReadLine();
      }
      public static void SetGreeting()
      {
         string strTitle = “Hello User 1.2”;
         Console.Title = strTitle;
      }
   }
}

[ AppTest.cs ]

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

namespace HelloUser
{
   class AppTest
   {
      static void Main(string[] args)
      {
         Program.GetUserInput();
         Program.SetGreeting();
      }
   }
}

Written by Samson Loo

September 18, 2008 at 7:27 pm

Visual c# 2008 Hello User 1.1 Console App…

leave a comment »

This is a much better approach of writing Hello User. It does require a few more lines of code, but it is better for modularity purposes. It is not a good practice to write everything in the Main method as I did in the previous examples.

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

namespace HelloUser
{
   class Program
   {
      static void Main(string[] args)
      {
         SetConsoleTitle();
         GetUserInput();
      }
      static void GetUserInput()
      {
         string strInput;
         Console.Write(“Enter your first name: “);
         strInput = Console.ReadLine();
         Console.WriteLine(“Hello “ + strInput);
         Console.ReadLine();
      }
      static void SetConsoleTitle()
      {
         string strTitle = “Hello User 1.1”;
         Console.Title = strTitle;
      }
   }
}

Prompt for input

Write to screen

Written by Samson Loo

September 15, 2008 at 4:25 pm

Visual c# 2008 Hello User Console App…

leave a comment »

The HelloWorld app showed you how to make a simple app that uses static data. Here is an app that prompts for user input. Same concept just a few more lines!

// Directive
using System;
using System.Collections.Generic;
using System.Linq; // Remove if being used in Visual C# 2005
using System.Text;

namespace HelloUser
{
   class Program
   {
      // Main Method
      static void Main(string[] args)
      {
         // Data Members
         string strTitle = “Hello User 1.0”; // Set string value
         string strInput; // Declare variable
         Console.Title = strTitle; // Set console title
         Console.Write(“Enter your first name: “); // Prompt for input
         strInput = Console.ReadLine(); // Read user input
         Console.WriteLine(“Hello “ + strInput); // Write line of text
         Console.ReadLine(); // Read line of text
      }
   }
}

Written by Samson Loo

September 15, 2008 at 4:13 am

Visual c# 2008 Hello World Console App…

leave a comment »

I figured I would post a simple Hello World Console Application built with Visual C# 2008. So here we go.

// Directive
using System;
using System.Collections.Generic;
using System.Linq; // Remove if being used in Visual C# 2005
using System.Text;

namespace HelloWorld
{
   class Program
   {
      // Main Method
      static void Main(string[] args)
      {
         // Data Members
         string strOutput = “Hello World”; // Set string value
         Console.Title = strOutput; // Set console title
         Console.WriteLine(strOutput); // Write line of text
         Console.ReadLine(); // Read line of text
      }
   }
}

Written by Samson Loo

September 15, 2008 at 3:27 am