6237 Programmering II (Csharp) Agenda/ToString

From Teknologisk videncenter
< 6237 Programmering II (Csharp) Agenda
Revision as of 14:34, 29 September 2015 by Orso (talk | contribs) (ToString)
Jump to: navigation, search

ToString

Hvis du gerne vil bestemme hvordan en klasse ser ud når den udskrives gøres det ved at lave en ToString metode. Det fungerer ved at vi f.eks. på klassen Log laver en ToString der ser sådan ud.


class Log
{
	......
	
	.......
	
	public override string ToString()
	{
		String returnStr = String.Format("Source:{0}, Destination:{1}, Protocol:{2}",this.source,this.destination,this.protocol);
		return returnStr;
	}
}


I main kan den derefter anvendes således


static void Main(string[] args)
{
	LogCtrl lCtrl = new LogCtrl();
	lCtrl.ReadFile("logfile.txt");
	List<Log> logLst = lCtrl.Search("78:da:6e:8a:82:c0", "", "ARP");

	foreach (Log l in logLst)
	{
		System.Console.WriteLine(l.ToString());
	}
}