Wednesday, November 10, 2010

Detatching Files

To detatch a database
Before you can detatch a database you must make sure that all windows that connect to the database are closed.
In Management Studio, in the Object Explorer
Right click on the database
Choose "Tasks"
Choose "Detach"
Just click OK on the following dialog. Don't check any boxes.
Use the operating system file manager to navigate to the database files.
Usually they are under C:\Program Files\Microsoft SQL Server\..\mssql1\Data\
The dots are for a variable folder name. What it is depends on your installation
Copy both the .mdf and the .log file. You will need them both.
To Reattach
In Management Studio, right click on "Databases" in the Object Explorer
Choose "Attach"
In the Dialog box click "Add"
use the next dialog box to navigate to where your files are
(to attach them they must be in a "root" level folder. That means they can't be on the desktop or in my documents.)
Once you have located the files click OK.
It should reattch the database for use

Tuesday, November 9, 2010

More array stuff

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

namespace ArrayExamples
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.CreateArray();
Console.ReadKey();
}

void CreateArray()
{
int[] myArray = { 1, 3, 5, 20, 51, 3, 2 };
Console.WriteLine(myArray[3].ToString());

//double[,,] myArrayTwo = new double[5,2,1];
//myArrayTwo[0,0, 0] = 2.3;
//myArrayTwo[0,0 ,1] = 2;
//myArrayTwo[0,1, 0] = 4;
//myArrayTwo[0,1, 1] = 4.5;

//string[,] books = new string[3, 2];
//books[0, 0] = "Lord of the Rings";
//books[0, 1] = "Tolkein";
Console.WriteLine("How many scores do you want to enter?");
int number = int.Parse(Console.ReadLine());
int[] scores =new int[number];
FillArray(scores);


}

void FillArray(int[] myScores)
{
for (int i = 0; i < myScores.Length; i++)
{
Console.WriteLine("enter a score");
myScores[i]=int.Parse(Console.ReadLine());
}
DisplayArray(myScores);
}

void DisplayArray(int[] allScores)
{
int sum = 0;
int counter = 0;
foreach (int i in allScores)
{
counter++;
Console.WriteLine("the score for hole {0} is {1}",counter,i.ToString());
sum += i; //sum = sum + i
}

double average = (double)sum / allScores.Length;

Console.WriteLine("The sum of the scores is {0}", sum);
Console.WriteLine("the average of the scores is {0}", average);
Array.Sort(allScores);
Console.WriteLine("the highest score is {0}", allScores.Max());
Console.WriteLine("The Second highest score is {0}", allScores[allScores.Length-2]);
}

void ArraylistExample()
{
ArrayList myList = new ArrayList();
myList.Add("Don't Panic");

List<string> genericArray = new List<string>();

}
}
}

Monday, November 8, 2010

Kilometers Conversion

Conversion.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace ConvertToKilometers
{
class Conversion
{
//private fields
private double miles;
private const double CONVERTFACTOR = 1.6;

//default constructor
public Conversion()
{
Miles = 0;
}

//overloaded constructor
public Conversion(double totalMiles)
{
Miles = totalMiles;
}

//public property
public double Miles
{
get { return miles; }
set { miles = value; }
}

//public method

public double Convert()
{
return Miles * CONVERTFACTOR;
}


}
}


Display.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace ConvertToKilometers
{
class Display
{
private double totMiles;

public void GetMiles()
{
bool isNumber;
Console.WriteLine("Enter the Miles");
isNumber = double.TryParse(Console.ReadLine(), out totMiles);
if (isNumber==false )
{
Console.WriteLine("Must be a number");
return;
}
}//end getmiles

public void DisplayKilometers()
{
Conversion c = new Conversion(totMiles);
Console.WriteLine("{0} is equal to {1} Kilometers", totMiles.ToString(), c.Convert().ToString());
}


}
}


Program.cs

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

namespace ConvertToKilometers
{
class Program
{
static void Main(string[] args)
{
Display d = new Display();
d.GetMiles();
d.DisplayKilometers();
Console.ReadKey();
}
}
}

Thursday, November 4, 2010

Code for Tip calculator


Xaml

<Window x:Class="TipCalculatorMark2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="417" Width="525">
<Grid Height="346">
<Label Content="Enter the total meal amount" Height="28" HorizontalAlignment="Left" Margin="50,30,0,0" Name="label1" VerticalAlignment="Top" FontSize="16" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="309,36,0,0" Name="txtMeal" VerticalAlignment="Top" Width="120" Background="#FF1CE9BB" FontSize="16" />
<RadioButton Content="10%" Height="16" HorizontalAlignment="Left" Margin="72,93,0,0" Name="rdoTenPercent" VerticalAlignment="Top" FontSize="16" />
<RadioButton Content="15%" Height="16" HorizontalAlignment="Left" Margin="72,129,0,0" Name="rdoFifteen" VerticalAlignment="Top" FontSize="16" />
<RadioButton Content="20%" Height="16" HorizontalAlignment="Left" Margin="72,167,0,0" Name="rdoTwenty" VerticalAlignment="Top" FontSize="16" />
<RadioButton Content="Other" Height="16" HorizontalAlignment="Left" Margin="72,207,0,0" Name="rdoOther" VerticalAlignment="Top" FontSize="16" />
<TextBox Background="#FF1CE9BB" FontSize="16" Height="23" HorizontalAlignment="Left" Margin="163,207,0,0" Name="txtOther" VerticalAlignment="Top" Width="120" />
<Button Content="GetTip" Height="23" HorizontalAlignment="Left" Margin="63,0,0,41" Name="button1" VerticalAlignment="Bottom" Width="75" Click="button1_Click" />
<Label Content="Label" Height="103" HorizontalAlignment="Left" Margin="163,243,0,0" Name="lblResults" VerticalAlignment="Top" Width="276" FontSize="16"/>
</Grid>
</Window>

mainwindow code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TipCalculatorMark2
{
///
/// Interaction logic for MainWindow.xaml
///

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
//Get input
double tipChoice = 0;
double totalMeal = 0;
bool test = double.TryParse(txtMeal.Text, out totalMeal);

if (test == false)
{
MessageBox.Show("Enter a valid number. No $ sign.");
txtMeal.Clear();
return;

}

if (rdoTenPercent.IsChecked==true)
{
tipChoice = .10;
}
if (rdoFifteen.IsChecked == true)
{
tipChoice = .15;
}
if (rdoTwenty.IsChecked == true)
{
tipChoice = .2;
}

if (rdoOther.IsChecked==true)
{
bool test2 = double.TryParse(txtOther.Text, out tipChoice);

if (test2 == false)
{
MessageBox.Show("Enter a valid percentage, no % sign.");
txtOther.Clear();
return;
}
}
Tip t = new Tip(totalMeal, tipChoice);
lblResults.Content = "The Tax on the meal is : "
+ t.CalculateTax().ToString("c") + "\n" +
"the Tip amount is: " + t.CalculateTip().ToString("c")
+ "\nThe total due is: " + t.CalculateTotal().ToString("c");


}
}
}

Tip Class

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

namespace TipCalculatorMark2
{
class Tip
{
//private fields
private double mealAmount;
private double tipPercent;
private const double TAXRATE = .09;

//constructors
public Tip()
{
MealAmount = 0;
TipPercent = 0;
}

public Tip(double total, double percent)
{
MealAmount = total;
TipPercent = percent;

}

//public properties
public double MealAmount
{
get { return mealAmount; }
set { mealAmount = value; }

}

public double TipPercent
{
get { return tipPercent; }
set
{
if (value >= 1)
{
tipPercent = value / 100;
}
else
{
tipPercent = value;
}
}
}

//public methods
public double CalculateTax()
{
return MealAmount * TAXRATE;
}

public double CalculateTip()
{
return MealAmount * TipPercent;
}

public double CalculateTotal()
{
return MealAmount + CalculateTip() + CalculateTax();
}

}
}

Wednesday, November 3, 2010

Arrays

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

namespace ArrayExamples
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.CollectScores();
Console.ReadKey();
}

void SimpleArray()
{
//one way to declare and initilize

int[] myArray = { 3, 45, 3, 14, 50 };
//always start counting at 0;
Console.WriteLine(myArray[3].ToString());
myArray[3] = 100;


// Console.WriteLine(myArray.Length);

int[] myArray2 = new int[5];
myArray2[0] = 23;
myArray2[1] = 22;

}

void CollectScores()
{
Console.WriteLine("how many scores do you want enter");
int number = int.Parse(Console.ReadLine());

double[] scores = new double[number];

for (int i = 0; i < number; i++)
{
Console.WriteLine("Enter a score");
scores[i] = double.Parse(Console.ReadLine());
}

CalculateAverages(scores);


}

void CalculateAverages(double[] rawScores)
{
double sum = 0;
//for every double value in the array
//scores which stores doubles
foreach (double score in rawScores)
{
Console.WriteLine(score.ToString());
sum += score;
}
Console.WriteLine("**********************");
Console.WriteLine("The sum is {0} ", sum);
double average = sum / rawScores.Length;
Console.WriteLine("The average is {0}", average.ToString("c"));
}
}
}

Monday, November 1, 2010

Package Class



Window1.xaml
<Window x:Class="PackageCalculator.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="377">
<Grid Height="268">
<Label Height="28" HorizontalAlignment="Left" Margin="20,31,0,0" Name="label1" VerticalAlignment="Top" Width="120">Enter the Weight</Label>
<TextBox Height="23" Margin="0,31,99,0" Name="txtWeight" VerticalAlignment="Top" HorizontalAlignment="Right" Width="120" />
<RadioButton Height="16" HorizontalAlignment="Left" Margin="48,87,0,0" Name="rdoOvernight" VerticalAlignment="Top" Width="120" >Overnight</RadioButton>
<RadioButton HorizontalAlignment="Left" Margin="48,112,0,0" Name="rdoTwoDay" Width="120" Height="16" VerticalAlignment="Top">Two Day</RadioButton>
<RadioButton Height="16" HorizontalAlignment="Left" Margin="50,0,0,106" Name="rdoGround" VerticalAlignment="Bottom" Width="120">Ground</RadioButton>
<Button Height="23" Margin="136,0,0,71" Name="btnShipping" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="75" Click="btnShipping_Click">Get Shipping</Button>
<Label Height="28" Margin="42,0,73,18" Name="lblShippingPrice" VerticalAlignment="Bottom">Label</Label>
</Grid>
</Window>

Here is the Code for Windows1.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PackageCalculator
{
///
/// Interaction logic for Window1.xaml
///

public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}

private void btnShipping_Click(object sender, RoutedEventArgs e)
{
//get the weight from textbox
double weight = double.Parse(txtWeight.Text);
//initilize the shipmethod variable
string shipMethod = null;
//checking to see which shipping method
//they have selected
if (rdoOvernight.IsChecked == true)
{
shipMethod = rdoOvernight.Content.ToString();
}

if (rdoTwoDay.IsChecked == true)
{
shipMethod = rdoTwoDay.Content.ToString();
}

if (rdoGround.IsChecked == true)
{
shipMethod = rdoGround.Content.ToString();
}

//initialize the Package class with the
//second constructor, passing it the values
Package pack = new Package(weight, shipMethod);
//call the CalculateShippingPrice of the class
//and store the result returned in the variable price
double price = pack.CalculateShippingPrice();
//display the results in the label on the form

lblShippingPrice.Content = "the shipping price is " + price.ToString("c");
}


}
}
Here is the package class

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

namespace PackageCalculator
{
class Package
{

//fields are class level variables
//that describe the class
//private by default, but stating it
//makes it obvious
private double weightInOunces;
private string shippingMethod;
private double shippingPrice;

//default constructor
public Package()
{
WeightInOunces = 0;
ShippingMethod = null;
ShippingPrice = 0;
}

//second constructor
public Package(double weight, string method)
{
WeightInOunces = weight;
ShippingMethod = method;
ShippingPrice = 0;
}


//public properties expose your
//private variables
//you control how they are exposed
//properties don't have ()
//they don't take arguments

public double WeightInOunces
{
set { weightInOunces = value; }
get { return weightInOunces; }
}//end property

public string ShippingMethod
{
set { shippingMethod = value; }
get { return shippingMethod; }
}//end property

public double ShippingPrice
{
set { shippingPrice = value; }
get { return shippingPrice; }
}

//this is a method to calculate shiping price
//it must have a parenthesis
//even if it doesn't have arguments
public double CalculateShippingPrice()
{
if (WeightInOunces <= 8)
{
ShippingPrice = 1;
}
else if (WeightInOunces <= 16)
{
ShippingPrice = 2;
}
else if (WeightInOunces <= 32)
{
ShippingPrice = 5;
}
else
{
ShippingPrice = 10;
}

if (ShippingMethod == "Overnight")
{
//*= means the same as
//ShippingPrice=ShippingPrice * 3
ShippingPrice *= 3;
}

if (ShippingMethod == "Two Day")
{
ShippingPrice *= 2;
}

return ShippingPrice;
}

} //end class
}//end namespace

More about Classes

First, a class is an abstract representation of an object. . .

A class can contain:
* fields
* properties
* methods
* constructors

Fields are class level variables. They describe a class. For instance, a Student class would have fields like studentID, name, email, major, gpa, etc. Fields should be kept private, which means nothing outside the class can see them or change them. This is part of encapsulation.

Properties are a special kind of method that are used to expose the private variables to other classes. Most properties contain two other methods: a Set which allows the user to change the value of the field, and a Get method that returns the value of the field. A property can just have a Get or just a Set. Additionally the programmer can add some validation to the property.

The idea of a property is to control access to the internal fields or variables. A property is marked by having no parenthesis at the end. (All other method and class initiations must have parenthesis.)

Methods are just as you have used them in the past. They do the work of the class. Each method must have a return type even if it is void. Methods can take parameters or not as needed. Any method with a return type other than void must have a return statement that returns a value of that type. Just like in the console apps we have done, each method should do one thing, though that thing could be complex. If you want other classes to be able to use the method you should declare it public.

Constructors, like properties, are also special methods. Constructors are used for initializing a class. In a constructor you can provide initial values for the field variables. You can also call any methods that need to run before the class is used. For example, you might call a method that connects to a database, so that when the class is used it is ready to read or write from the database. You can have more than one constructor. It is possible to overload constructors (or any other method) by creating a method with the same name but a different signature. The signature consists of the number and data types of the arguments.

Here are two constructors for the Student Class:


public Student()
{
StudentID=null;
StudentName=null;
GPA=0;
}

public student(string studID)
{
StudentID=studID;
StudentName=null;
GPA=0;

}



Constructors are marked by having the same name as the class and by having no return type.

If you don't create a constructor, .Net will create a default constructor for you that will initialize all your variables to 0 or null. If you create a constructor, any constructor, .Net will not create a default constructor and you will have to do all your own initializing.