Deciding when to use a Property verseus a function isnt always clear, but it will become clear over time. A good book will help explain their syntax (which is a good start), but only practice will let you know when and how best to use them. If you have a class to define a person, you might have an Age property and a Name property. You *could* define a method called GetAge() but its generally bad practice. What makes it bad? Nothing but the fact that "professional" programmers call it bad - its a standard to make code more readable and easier to understand (from your coding perspective and the from the perspective of the person using your class, even if youre both).
A delegate is completely different. Its a way for you to define what a function will look like. Ive used them in two ways (though more exist - depending on what you need). First, an event is a delegate. Meaning, a click event is basically a function that gets called when a click is made. The function is not known when the user created the control. So a delegate is used to say "I want to call a function that takes two parameters". The control can then raise this event (basically, just call a function) and call your code.
Normally, you have code that calls a function that you know about. You call that function by name (if the function is named Test and it takes a string, you can use Test(string1) or Test(string1)
. But in the case of events, the function being called isnt known but as long as its known at runtime, ANY function can be called (as long as its hooked). To get hooked, it MUST follow the exact syntax specified by the delegate definition.
The other main use is to define your own function - or rather, what a function will
look like. You can decide later what function to call.
Suppose you have a function that takes a string and a DataSet and saves the DataSet. Before it can do the save, it needs to run a bunch of conversions on the data in the DataSet and the types of conversions are based on what the string contains. You could do this:
C#:
private void DoAction(string code, DataSet ds)
{
bool result = false;
if(code=="A")
result =DoActionA(ds);
else if(code=="B")
result =DoActionB(ds);
else if(code=="C")
result =DoActionC(ds);
if(result==true)
{
// Do something on success
}
else
{
// Do something on error
}
}
private bool DoActionA(DataSet ds)
{
// Do some checking specific to action A
}
private bool DoActionB(DataSet ds)
{
// Do some checking specific to action B
}
private bool DoActionC(DataSet ds)
{
// Do some checking specific to action C
}
The above works fine. But, if you need to have an error handler in DoAction(), you might want/need to wrap it around the big IF. Or, you could write 3 error handlers, one for each call to DoActionX.
You could also simplify it (simplify is a relative term
) by using delegates. Take this example:
C#:
private delegate bool DoAnAction(DataSet ds);
private void DoAction(string code, DataSet ds)
{
// Create the delegate like any other variable
DoAnAction action = null;
if(code=="A")
action = new DoAnAction(DoActionA);
else if(code=="B")
action = new DoAnAction(DoActionB);
else if(code=="C")
action = new DoAnAction(DoActionC);
// Use the delegate as if you were calling any other function:
try
{
bool result = action(ds);
}
catch{/* Do something */}
if(result==true)
{
// Do something on success
}
else
{
// Do something on error
}
}
private bool DoActionA(DataSet ds)
{
// Do some checking specific to action A
}
private bool DoActionB(DataSet ds)
{
// Do some checking specific to action B
}
private bool DoActionC(DataSet ds)
{
// Do some checking specific to action C
}
The above shows that you can create a delegate just like any other variable. Its constructor always takes a function name. Once created, you can call the delegate like any function only it will call the function you pointed to in the constructor. You can also create
multicast delegates that automatically call multiple functions in ONE call. Events work this way since you might have multiple event handlers (for instance, when you see C# code that does "control1.Click += new ...").
Id strongly suggest looking over a book and playing around with everything you read (dont just read through 3 chapters in one sitting). Playing around and tinkering with code is the best way to get familiar with (and remember) a language.
-Nerseus