Hoofdstuk 2
Hallo, C# !
Soorten programma’s Console-
applicatie
Soorten programma’s Console-
applicatie Windows-
applicatie
Soorten programma’s Console-
applicatie Windows-
applicatie Web-
applicatie
Soorten programma’s Console-
applicatie Windows-
applicatie Web-
applicatie Game
Soorten programma’s Console-
applicatie Windows-
applicatie Web-
applicatie Game
Applet
Soorten programma’s Console-
applicatie Windows-
applicatie Web-
applicatie Game
Applet
App
Opbouw broncode
Opdrachten omhet geheugente veranderen
Opdrachten zijn gegroepeerd inmethoden
Methoden zijn gegroepeerd inklassen
en dat kunnen zelf ook weer aanroepenzijn van weer andere methodes...
dus de “waar was ik gebleven” administratie is best ingewikkeld!
Soorten opdrachten
Toekennings -opdracht:verander het geheugen
Aanroep van een andere methode:voer eerst de opdrachten in die methode uit, en ga daarna verder waar je gebleven was
Voorbeeld C#-programma
één klasse
...met éénmethode
...met tweeopdrachten
accolades begrenzenklasse, resp. methode
using System;
class Hallo { static void Main ( ) { Console.WriteLine("Hallo!"); Console.ReadLine( ); }}
Klasse- en methode-header
using System;
class Hallo { static void Main ( ) { Console.WriteLine("Hallo!"); Console.ReadLine( ); }}
naam:bedacht door
de programmeur
naam:bedacht door
de programmeur
één van demethodes moet
Main hetenMain
Opdracht: methode-aanroep
opdrachten:aanroep van
anderemethoden
naam van demethode
altijdeen punt
overigedetails
klasse waaruit de methode komt
using System;
class Hallo { static void Main( ) { Console.WriteLine("Hallo!"); Console.ReadLine( ); }}
Klasse-bibliotheken
library-klassenmag je
gebruiken...
als je ze maaraangeeft in
welke libraryze staan
using System;
class Hallo { static void Main( ) { Console.WriteLine("Hallo!"); Console.ReadLine( ); }}
Methode-header en -aanroep
methode-header
methode-aanroep
using System;
class Hallo { static void Main( ) { Console.WriteLine("Hallo!"); Console.ReadLine( ); }}
Syntax en semantiek
Syntax:de vorm van het programmacompilatieeenheid
using ;
klassedeclaratie
librarynaam
Semantiek:wat het programmabetekent
Syntax van klasse-declaratie
klassedeclaratie
public private
class naam { member }
: naam
Syntax van member
member
static
public private
naam
type
void ( ) blok
,
naamtype
Syntax van blok
blok
declaratie
opdracht{ }
Syntax van opdracht
opdracht
( )
,
;expressieklassenaam
objectexpressie
.
methodenaam
= expressie ;variabele
propertynaam +=
Syntax van opdracht
opdracht
( )
,
;expressieklassenaam
objectexpressie
.
methodenaam
= expressie ;variabele
propertynaam +=
Console-applicatie
using System;
class Hallo2 { static void Main ( ) { Console.WriteLine("Hallo!"); Console.ReadLine( );
}}
Console-applicatie
using System;
class Hallo2 { static void Main ( ) { Console.WriteLine("Wat is je naam?"); naam = Console.ReadLine( );
}}
Toekennings-opdrachtgeeft een waardeaan een variabele
Console-applicatie
using System;
class Hallo2 { static void Main ( ) { string naam; Console.WriteLine("Wat is je naam?"); naam = Console.ReadLine( );
}}
Declaratieintroduceert een variabele
van een bepaald type
Console-applicatie
using System;
class Hallo2 { static void Main ( ) { string naam; Console.WriteLine("Wat is je naam?"); naam = Console.ReadLine( ); Console.WriteLine("Hallo, " + naam + "!");
}}
gebruik vande variabele
Console-applicatieusing System;
class Hallo2 { static void Main ( ) { string naam; Console.WriteLine("Wat is je naam?"); naam = Console.ReadLine( ); Console.WriteLine("Hallo, " + naam + "!"); Console.WriteLine("met " + naam.Length + " letters.");
}}
Property vaneen object
Console-applicatieusing System;
class Hallo2 { static void Main ( ) { string naam; Console.WriteLine("Wat is je naam?"); naam = Console.ReadLine( ); Console.WriteLine("Hallo, " + naam + "!"); Console.WriteLine("met " + naam.Length + " letters."); Console.ReadLine( ); }}
Syntax van opdracht
opdracht
( )
,
;expressieklassenaam
objectexpressie
.
methodenaam
= expressie ;variabele
propertynaam +=
Syntax van opdracht
opdracht
( )
,
;expressieklassenaam
objectexpressie
.
methodenaam
= expressie ;variabele
propertynaam +=
toeken
nin
g
Syntax van declaratie
declaratie
type naam ;
,
type
klassenaam
int
string
Windows-applicatie
class HalloWin1 { static void Main ( ) {
}}
Form scherm;scherm = new Form( );
Application.Run(scherm);
using System.Windows.Forms;
scherm.Text = "Hallo";scherm.BackColor = Color.Yellow;scherm.Size = new Size(200,100);
using System.Drawing;
declaratie en toekenningvan een variabele met type Form
gebruik van de variabelebij aanroep van Run
aanpassen vanattributen
Syntax van opdracht
opdracht
( )
,
;expressieklassenaam
objectexpressie
.
methodenaam
= expressie ;variabele
propertynaam +=
Syntax van opdracht
opdracht
( )
,
;expressieklassenaam
objectexpressie
.
methodenaam
= expressie ;
+=propertynaam
variabele
Windows-applicatieclass HalloWin2 { static void Main ( ) {
}}
class HalloForm{
}
Form scherm;scherm = new Form( );Application.Run(scherm);
HalloForm scherm;scherm = new HalloForm( );Application.Run(scherm);
: Form
subklasse is eengespecialiseerde versie
aanroep van deconstructor methode
public HalloForm( ){
}
definitie van deconstructor methode
this.Text = "Hallo";this.BackColor = Color.Yellow;this.Size = new Size(200,100);
Windows-applicatieclass HalloForm{
}
: Formpublic HalloForm( ){
}
this.Text = "Hallo";this.BackColor = Color.Yellow;this.Size = new Size(200,100); Label groet;groet = new Label( );
this.Controls.Add(groet);
groet.Text = "Hallo allemaal";groet.Location = new Point(30,20);
Syntax van opdracht
opdracht
( )
,
;expressieklassenaam
objectexpressie
.
methodenaam
= expressie ;variabele
propertynaam +=
Syntax van opdracht
opdracht
klassenaam
objectexpressie
.
methodenaam
propertynaam
scherm . Size groet . Textnaam . Length
propertynaam
objectexpressie
methodenaam
this.Controls . Add
(
=
klassenaam
Color . Yellow
Console . WriteLineApplication . Run
static
Staticgewone methoden static methodenstatic
Bewerken een object Bewerken geen object
Aanroep:object . methode (…)
Aanroep:klasse . methode (…)
In de body van de definitie:this is dat object
In de body van de definitie:this bestaat niet
Voorbeeld: Voorbeeld:Controls.Add(groet);
Application.Run(scherm); Main
is altijd static
Constructormethoden zijn nooit static
Windows-applicatieclass HalloForm{
}
: Formpublic HalloForm( ){
}
this.Text = "Hallo";this.BackColor = Color.Yellow;this.Size = new Size(200,100); Label groet;groet = new Label( );
this.Controls.Add(groet);
groet.Text = "Hallo allemaal";groet.Location = new Point(30,20);
Windows-applicatieclass HalloForm{
}
: Formpublic HalloForm( ){
}
this.Text = "Hallo";this.BackColor = Color.Yellow;this.Size = new Size(200,100); this.Paint += this.teken;
“Event”-property Methode-naam,maar geen aanroep!
Windows-applicatieclass HalloForm{
}
: Formpublic HalloForm( ){
}
this.Text = "Hallo";this.BackColor = Color.Yellow;this.Size = new Size(200,100); this.Paint += this.teken;}
void teken({
Object o, PaintEventArgs pea )pea
.Graphics.
DrawString(
);
"Hallo!"
, 10, 10, Brushes.Blue
, new Font("Tahoma", 30)
Windows-applicatieclass HalloWin3{ static void Main() { HalloForm scherm; scherm = new HalloForm(); Application.Run(scherm); }}class HalloForm : Form{ public HalloForm() { this.Text = "Hallo"; this.Paint += this.teken; } void teken(object o, PaintEventArgs p) { p.Graphics.DrawString( … ); }}
using System.Windows.Forms;
Game-applicatieclass HalloWin3{ static void Main() { HalloForm scherm; scherm = new HalloForm(); Application.Run(scherm); }}class HalloForm : Form{ public HalloForm() { this.Text = "Hallo"; this.Paint += this.teken; } void teken(object o, PaintEventArgs p) { p.Graphics.DrawString( … ); }}
using System.Windows.Forms;
class HalloGame4{ static void Main() { HalloGame spel; spel = new HalloGame(); }}class HalloGame : Game{ public HalloGame() {
}
}
using Microsoft.XNA.Framework;
spel.Run();
this.Window.Title = "Hallo";
override void Draw(GameTime t){}
Form vs. Gamevoid teken(object o, PaintEventArgs pea){ gr.DrawString(
);}
override void Draw(GameTime gt){
}
sb.DrawString(
);
SpriteBatch sb; sb = new SpriteBatch(this.GraphicsDevice);
sb.Begin();
sb.End();
Graphics gr; gr = pea.Graphics;"Hallo!"
"Hallo!"
, 10, 20new Point( )
, new Vector2(10, 20)
, Brushes.Blue
, Color.Blue
, new Font("Tahoma", 30)
this.Content.Load<SpriteFont>("Spelfont"),
gt.TotalRealTime.Milliseconds, 20)
Content bij een Game
Fonts, Achtergrondplaatjes, Geluiden… this.Content.Load<SpriteFont>("Spelfont")
Hulpbestanden / Spelfont.spritefont
<?xml version="1.0" encoding="utf-8"?><XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics"> <Asset Type="Graphics:FontDescription">
<Spacing>0</Spacing> <UseKerning>true</UseKerning> <Style>Regular</Style> <CharacterRegions><CharacterRegion><Start> </Start> <End>~</End></CharacterRegion></CharacterRegions> </Asset></XnaContent>
Tahoma 30
<FontName> </FontName> <Size> </Size>
this.Content.RootDirectory = "Hulpbestanden";
Top Related