Programmering II Januar 2017
Generalt
Link til C# bog: https://drive.google.com/file/d/0B5oDizcmFQ1YSHUyQ0UyalNoMEk/view?usp=sharing
Link til Bob Tabor: https://mva.microsoft.com/en-us/training-courses/c-fundamentals-for-absolute-beginners-16169
Link til danske videoer: https://www.nemprogrammering.dk/Tutorials/c-sharp/1-introduktion.php
God WPF-Tutorial: http://www.wpf-tutorial.com/
Link til C# fundamentals (film materiale): https://drive.google.com/file/d/0B5oDizcmFQ1YalVYcGgzQWRrSUU/view?usp=sharing
Link til C# WPF (film materiale): https://drive.google.com/file/d/0B5oDizcmFQ1YNTZwNGZ4XzREWFU/view?usp=sharing
Bold spil: https://drive.google.com/file/d/0B5oDizcmFQ1YRUdIdG1lMEtWbTA/view?usp=sharing
Nedarvning: https://drive.google.com/file/d/0B5oDizcmFQ1YRGVzQ1NQdVhpdVk/view?usp=sharing
delegates:
delegate void ShootWeapon();
static void Main(string[] args)
{
ShootWeapon shoot = ShootRifle;
shoot();
Console.WriteLine("Changing weapon");
shoot = ShootPistol;
shoot();
Console.ReadKey();
}
static void ShootRifle()
{
Console.WriteLine("Shooting rifle. 50 damage.");
}
static void ShootPistol()
{
Console.WriteLine("Shooting pistol. 10 damage.");
}
enumerations:
enum weapons
{
rifle,
pistol,
knife
}
static weapons currentWeapon;
static void Main(string[] args)
{
currentWeapon = weapons.rifle;
Shoot();
Console.WriteLine("Switching weapon");
currentWeapon = weapons.pistol;
Shoot();
Console.ReadKey();
}
static void Shoot()
{
if(currentWeapon == weapons.rifle)
{
Console.WriteLine("Shooting with rifle. 50 damage");
}
else if (currentWeapon == weapons.pistol)
{
Console.WriteLine("Shooting with pistol. 10 damage");
}
}
Statiske klasser En statisk klasse kan til gås alle steder fra vores kode. Husk at lave variablerne sattiske også:
static void Main(string[] args)
{
//Statiske klasser skal ikke instancieres.
//De kan tilgåes alle steder fra.
Console.WriteLine(StaticData.staticString);
Console.ReadKey();
}
}
//Bemærk at jeg har indsat nøgleordet 'static' før 'class' ordet på linjen her under:
static class StaticData
{
public static int staticInt = 100;
public static string staticString = "Mads";
}
Begynder hold
Variabler
En variabel er noget som indeholder en værdi. Det kunne eksempelvis være brugerens navn der bliver indtastet i programmet, og gemt i en værdi.
Værdityper er som følgende:
Type | værdi |
---|---|
string | "Dette er tekst" |
int | 9001 |
float | 123.45 |
bool | true/false |
Færdig gør denne lommeregner:
https://drive.google.com/file/d/0B5oDizcmFQ1YZTV4Vm96Z282cTQ/view?usp=sharing
Færdiggør dette spil (Tic Tac Toe):
https://drive.google.com/file/d/0B5oDizcmFQ1YcER5ZF9HU21oQnM/view?usp=sharing
Spiludvikling i WPF
Snapshot (klokken 13): https://drive.google.com/file/d/0B5oDizcmFQ1YRHhmTFE4WHFMQ1k/view?usp=sharing
Snapshot (klokken 14:30): https://drive.google.com/file/d/0B5oDizcmFQ1YdUNrd3dsS1FiMzA/view?usp=sharing
Snapshot (dag 2 klokken 9:10) : https://drive.google.com/file/d/0B5oDizcmFQ1YWUd2am9BTE53cmc/view?usp=sharing
Snapshot (dag 2 klokken 10:10 FINAL) : https://drive.google.com/file/d/0B5oDizcmFQ1YOFk1SkstRkI2OEU/view?usp=sharing
Mads' RTS spil: https://drive.google.com/file/d/0B5oDizcmFQ1YaklBQ3phdGNBT3c/view?usp=sharing
Middel hold
Opgave 1:
Lav en BMI beregner som denne:
http://www.beregnbmi.dk/Default.aspx
Opgave 2:
Lav en lommeregner lignende den som findes i Windows.
For at konvertere en string til en decimal bruges følgende kode:
string input = textBox.Text;
decimal convertedInput = decimal.Parse(input);
Lommeregner eksempel:
https://drive.google.com/file/d/0B5oDizcmFQ1YMzBVMUk3ZUZxbzg/view?usp=sharing
Serializerings eksempel:
//Opretter et GameData objekt:
GameData gd = new GameData();
gd.playerMoney = 100;
//Hvad vi skal bruge til at serializere:
XmlSerializer serializer = new XmlSerializer(typeof(GameData));
StringWriter sWriter = new StringWriter();
//Serializering af objekt:
serializer.Serialize(sWriter, gd);
//Opretter en string med alt XML:
string myXml = sWriter.ToString();
//Udskriver XML fil til skrivebordet. BEMÆRK at du skal ændre filstien for at det virker:
//File.WriteAllText(@"C:\Users\MaKN\Desktop\save.xml", myXml);
//Indlæsning af XMLfil fra skrivebordet:
string readXML = File.ReadAllText(@"C:\Users\MaKN\Desktop\save.xml");
Console.WriteLine(readXML);
//Deserializering af xmlfil til objekt:
StringReader sReader = new StringReader(readXML);
GameData newGD = (GameData)serializer.Deserialize(sReader);
//Udskriver den nye værdi fra det nye objekt:
Console.WriteLine("New GameData money value is: "+ newGD.playerMoney);
Console.ReadKey();
GameData klasse: (husk at lave den public)
[Serializable]
public class GameData
{
public int playerMoney = 0;
public int playerUpgrades = 0;
}
Expert hold
Opgave 1:
Lav et program der kan sende data til en applikation som kan lytte. Benyt gerne UDP.
Opgave 2:
Udvid programmet så programmerne kan både sende og modtage data til hinanden. (et simpelt chat program.)
Listener
UdpClient client = new UdpClient(11000);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 11000);
byte[] recivedArray = client.Receive(ref endPoint);
string message = Encoding.ASCII.GetString(recivedArray);
Console.WriteLine("The message was:");
Console.WriteLine(message);
Console.ReadKey();
Sender
string message = "Hello!";
UdpClient client = new UdpClient();
IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);
byte[] bArray = Encoding.ASCII.GetBytes(message);
client.Send(bArray, bArray.Length, endpoint);
Console.WriteLine("Message has been sent!");
Console.ReadKey();
Serializering:
Human joshua = new Human();
joshua.name = "Joshua";
joshua.money = 20000;
XmlSerializer serializer = new XmlSerializer(typeof(Human));
StringWriter sWriter = new StringWriter();
serializer.Serialize(sWriter, joshua);
string myXml = sWriter.ToString();
Console.WriteLine(myXml);
Console.ReadKey();
StringReader sReader = new StringReader(myXml);
Human newJoshua = (Human)serializer.Deserialize(sReader);
Console.WriteLine("New Human was created!");
Console.WriteLine(newJoshua.name);
Console.WriteLine(newJoshua.money);
Console.ReadKey();
Opgave:
Send et objekt fra et program til et andet program igennem UDP eller TCP.
SQLite
SQLite eksempel i konsollen:
class Program
{
static SQLiteConnection connection;
static void Main(string[] args)
{
Console.WriteLine("Opening database:");
//SQLiteConnection.CreateFile("myDatabase.db");
connection = new SQLiteConnection(@"Data Source=C:\Users\MaKN\Desktop\myHeroeDatabase.db");
connection.Open();
Console.WriteLine("Connection successfull.");
//string sqlQuery = "INSERT INTO heroes VALUES ('Iron Man', 42)";
string sqlSelectQuery = "SELECT * FROM heroes";
SQLiteCommand command = new SQLiteCommand(sqlSelectQuery, connection);
command.ExecuteNonQuery();
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("Name: " + reader["name"]);
Console.WriteLine("Age: " + reader["age"] + "\n");
}
connection.Close();
Console.ReadKey();
}
}
SQL commands:
CRUD = Create, Read, Update, Delete.
CREATE table HEROES (name TEXT, age INT)
INSERT into HEROES (name, age) VALUES (‘Batman’, 39)
Select * from HEROES where age > 30;
Update HEROES set name = ‘Buttman’ Where name = ‘Batman’
DELETE from HEROES Where name = ‘Buttman’