Archive for the ‘visual c# 2008’ Category
Visual c# 2008 Hello (Unicode) World (Hexidecimal)…
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;
}
}
}
Visual c# 2008 Centimeters to Inches Calc…
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();
}
}
}
Visual c# 2008 Hello User 1.2 Console App…
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();
}
}
}
Visual c# 2008 Hello User 1.1 Console App…
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;
}
}
}
Visual c# 2008 Hello User Console App…
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
}
}
}
Visual c# 2008 Hello World Console App…
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
}
}
}