Difference between revisions of "Programmering II Januar 2017"
m (→Generalt) |
m (→Generalt) |
||
Line 10: | Line 10: | ||
*Link til C# WPF (film materiale): [https://drive.google.com/file/d/0B5oDizcmFQ1YNTZwNGZ4XzREWFU/view?usp=sharing C# WPF] | *Link til C# WPF (film materiale): [https://drive.google.com/file/d/0B5oDizcmFQ1YNTZwNGZ4XzREWFU/view?usp=sharing C# WPF] | ||
+ | ==Telnet fra Csharp= | ||
+ | *[[Telnet C-Sharp]] (Switch på 192.168.138.222 - Don't f... it up :-) | ||
'''Bold spil:''' https://drive.google.com/file/d/0B5oDizcmFQ1YRUdIdG1lMEtWbTA/view?usp=sharing | '''Bold spil:''' https://drive.google.com/file/d/0B5oDizcmFQ1YRUdIdG1lMEtWbTA/view?usp=sharing |
Revision as of 10:40, 18 January 2017
Contents
Generalt
- Link til C# bog: C# 6 Programming with Visual Studio 2015
- Link til Bob Tabor: C# Fundamentals for Absolute Beginners (Video)
- Link til danske videoer: Introduktion til C# programmering
- God WPF-Tutorial: The complete WPF tutorial
- Link til C# fundamentals (film materiale): C# Fundamentals
- Link til C# WPF (film materiale): C# WPF
=Telnet fra Csharp
- Telnet C-Sharp (Switch på 192.168.138.222 - Don't f... it up :-)
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’
Link til SQLite tutorials:
http://blog.tigrangasparian.com/2012/02/09/getting-started-with-sqlite-in-c-part-one/
https://nickcharlton.net/posts/sqlite-with-csharp.html
opgave
Lav et program i konsollen (eller en webForm), hvor brugeren kan indtaste en helt (navn, alias, superkraft ect.) som bliver gemt i databasen.
Programmet skal kunne udskrive en liste over alle helte der ligger i databasen.
Brugeren skal have mulighed for at slette en helt fra databasen.
SQLite eksempel 2:
SQLiteConnection connection = new SQLiteConnection(@"Data Source=C:\Users\MaKN\Desktop\asdf.db");
connection.Open();
string query = "CREATE TABLE IF NOT EXISTS heroes (name TEXT, age INTEGER, powerLevel REAL)";
SQLiteCommand command = new SQLiteCommand(query, connection);
command.ExecuteNonQuery();
//Console.WriteLine("Indtast en superhelt:");
//string heroName = Console.ReadLine();
//Console.WriteLine("Indtast heltens alder:");
//int heroAge = Convert.ToInt32(Console.ReadLine());
//float herolevel = 0;
//bool success = false;
//while (success == false)
//{
// Console.WriteLine("Indtast heltens powerlevel:");
// success = float.TryParse(Console.ReadLine(), out herolevel);
// Console.WriteLine("Brug kun numeriske værdier");
// Console.WriteLine("Prøv igen");
//}
//string newQuery = $"INSERT INTO heroes (name, age, powerLevel) VALUES ('{heroName}', {heroAge}, {herolevel})";
//SQLiteCommand newCommand = new SQLiteCommand(newQuery, connection);
//newCommand.ExecuteNonQuery();
string updateQuery = "UPDATE heroes set powerLevel = 1 WHERE name = 'spiderman'";
SQLiteCommand updateCommand = new SQLiteCommand(updateQuery, connection);
updateCommand.ExecuteNonQuery();
connection.Close();
Console.ReadKey();