function passing / inheritice ?

  • Thread starter Thread starter dtdev
  • Start date Start date
D

dtdev

hi,

Im new to C# - ive have my background in c/c++ - and somethings are abit new
to me, so havent really gotten the total feel of the laguage yet.

I have a situation which i really have no idea to solve in c# - what i do,
is that i have a MAIN form, which subsequently calls several other forms.

There are very few variable things between each form, but i need to have a
"SaveData()" function for each form.

In C++ it was possible to do this by inheratice of classes - in C i could
pass a functionpointer to my local function, and obtain the thing i wish.

How do i do a similar thing i c# ? - pass some reference to a local function
(in one class) to another class ?

Hope you can help.
 
Hi,

You would typically do that sort of thing with inheritance in C# too. Create
a new baseclass that inherits from System.Windows.Forms.Form and then let
all your other forms inherit from that baseclass instead of the default
System.Windows.Forms.Form.

In the baseclass you can implement the Save() method.
 
hi guys,

Thanks for your quick answers.

Do you know anywhere where there might be an example of this on the internet
? - just to make sure im getting it right from the start.
 
public class BaseForm : System.Windows.Forms.Form
{
public virtual void SaveData()
{
// do some saving...
}
}


public class MammothForm : BaseForm
{

public void doFunkyStuff_click(object sender, EventArgs e)
{
base.SaveData();
// - or -
this.SaveData();
// same, same but different ;)
}
}

public class MammothEatingForm : BaseForm
{

public override void SaveDate()
{
// Save data elswhere
}

public void eatAMammoth_click(object sender, EventArgs e)
{
this.SaveData();
// - not equal to -
base.SaveData();
}

}
 
Check MSDN.COM for Inheritance, Object Oriented Programming (OOP) examples,
etc.

It is really not that complex :)
 
dtdev:

You can have a base class which has the SaveData() function.

All other forms can inherit the base class with the common SaveData or other
common functions.

You can also override the SaveData function in each Form depending on
whether you want to implement the same functionality or use a variation of
it.

Thanks
 
Back
Top