Difference between revisions of "Programmering II Januar 2017"
(→Begynder hold) |
(→Expert hold) |
||
Line 89: | Line 89: | ||
Console.ReadKey(); | Console.ReadKey(); | ||
</source> | </source> | ||
+ | |||
+ | |||
+ | '''Serializering:''' | ||
+ | |||
+ | <source lang="Csharp"> | ||
+ | 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(); | ||
+ | </source> | ||
+ | |||
+ | Opgave: | ||
+ | |||
+ | Send et objekt fra et program til et andet program igennem UDP eller TCP. |
Revision as of 08:38, 10 January 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
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 |
Middel hold
Opgave 1:
Lav en BMI beregner som denne:
http://www.beregnbmi.dk/Default.aspx
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
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.