Class Inheritance
-
I'm just starting to get my teeth in to C# and so far I think its going quite well. I used to do quite a bit of coding in Delphi which brings me on to my current problem. In Delphi you used to be able to create your own class using a Thread class as its base example:
-------------------------------- TMyClass = Class(TThread) .... Public FVariable : string; End; Var MyClass : TMyClass; MyClass := TMyClass.Create(True); MyClass.FVariable := 'Test'; --------------------------------
MyClass now has all the functions/procedures and variables of a normal thread class. I would like to do the same thing in C# but I can't inherit from system.threading.thread because it's sealed. I suspect there might be another approach to this but I can't find much on google. I guess I am not searching for the right things here. I guess I'm trying to do this the Delphi way when I should be doing them the C# way!!! Any ideas anyone? Thanks -
I'm just starting to get my teeth in to C# and so far I think its going quite well. I used to do quite a bit of coding in Delphi which brings me on to my current problem. In Delphi you used to be able to create your own class using a Thread class as its base example:
-------------------------------- TMyClass = Class(TThread) .... Public FVariable : string; End; Var MyClass : TMyClass; MyClass := TMyClass.Create(True); MyClass.FVariable := 'Test'; --------------------------------
MyClass now has all the functions/procedures and variables of a normal thread class. I would like to do the same thing in C# but I can't inherit from system.threading.thread because it's sealed. I suspect there might be another approach to this but I can't find much on google. I guess I am not searching for the right things here. I guess I'm trying to do this the Delphi way when I should be doing them the C# way!!! Any ideas anyone? ThanksIn C# threads are handled differently. You create an instance of System.Threading.Thread and pass ThreadStart delegate to the constructor. ThreadStart delegate represents a method that thread will run.
public static void RunMe()
{
{
//Run code here
}
}public static Main()
{
Thread myTread = new Thread(new ThreadStart(RunMe));
} -
I'm just starting to get my teeth in to C# and so far I think its going quite well. I used to do quite a bit of coding in Delphi which brings me on to my current problem. In Delphi you used to be able to create your own class using a Thread class as its base example:
-------------------------------- TMyClass = Class(TThread) .... Public FVariable : string; End; Var MyClass : TMyClass; MyClass := TMyClass.Create(True); MyClass.FVariable := 'Test'; --------------------------------
MyClass now has all the functions/procedures and variables of a normal thread class. I would like to do the same thing in C# but I can't inherit from system.threading.thread because it's sealed. I suspect there might be another approach to this but I can't find much on google. I guess I am not searching for the right things here. I guess I'm trying to do this the Delphi way when I should be doing them the C# way!!! Any ideas anyone? ThanksIf you are looking for the syntax of classes for C#, the following is a simple example:
public class TrafficLight
{
// constructor
TrafficLight(){}// private variable private Color \_color; // public property public Color LightColor { get{return \_color;} set{\_color = value;} } public virtual void ChangeColor(Color newColor) { LightColor = newColor; }
}
public class DeluxeLight : TrafficLight
{
// constructor
DeluxeLight(){}public overrides void ChangeColor(Color myColor) { //.... overriding the base class method }
}
- Nick Parker
My Blog