Reputation: 861
Is something like that possible?
Let's say I have the String Class. And I want to add a Method DoStringCalc()
. Now I want one Class which holds all these special strings and calls DoStringCalc()
on all of them. But every String can have a different logic in DoStringCalc()
and there can be thousands of them.
Most logical for me (coming from java) would be to override the Method DoStringCalc()
when initializing a new String. e.g:
String hello = new String() {
public override DoStringCalc() {
//do something here
}
}
How is this solved in C#?
Upvotes: 0
Views: 117
Reputation: 1
as far as I know it is not possible to change classes you do not own the sourcecode. E.g. .Net Classes.
You can extend existing classs with Extensionmethods:
public static classe StringExtension
{
public static void DoStingCalc(this string str)
{
//Do your Stuff here
}
}
If you want to Force a specific Method. You have to derive from your class an decorate it with the interface you want to ensure. Or make an abstact method declaration.
Upvotes: 0
Reputation: 2105
If you have many different calculations logic, consider passing the transformation function to the class. Wrap the value which you will work with by a class and also store an appropriate tranformation in the same class:
public class Foo
{
public string Value;
private Func<string, string> doStringCalc;
public Foo(string value, Func<string, string> doStringCalc)
{
this.Value = value;
this.doStringCalc = doStringCalc;
}
public void ExecuteTransform()
{
this.Value = this.doStringCalc(this.Value);
}
}
Then add different transforms as follows:
var foo1 = new Foo("A", (initialValue) => initialValue + initialValue);
var foo2 = new Foo("B", (initialValue) => initialValue + "bar");
var foo3 = new Foo("C", (initialValue) => " ");
foo1.ExecuteTransform();
foo2.ExecuteTransform();
foo3.ExecuteTransform();
Debug.WriteLine(foo1.Value); // prints "AA"
Debug.WriteLine(foo2.Value); // prints "Bbar"
Debug.WriteLine(foo3.Value); // prints " "
Upvotes: 3
Reputation: 118937
One option is to pass in an Action
as part of the class constructor (or a Func
if you want to return a value). For example:
public class MyThing
{
private Action<MyThing> _doCalc { get; set; }
public MyThing(Action<MyThing> doCalc)
{
_doCalc = doCalc;
}
public void DoCalc()
{
_doCalc(this);
}
public string SomeString { get; set; }
}
And call it like this:
var thing = new MyThing(t => Console.WriteLine(t.SomeString));
thing.SomeString = "Hello world";
thing.DoCalc();
Another variation of this would be to create a generic wrapper class, for example:
public class Wrapper<T>
{
private Action<T> _doCalc { get; set; }
public Wrapper(Action<T> doCalc)
{
_doCalc = doCalc;
}
public T Value { get; set; }
public void DoCalc()
{
_doCalc(this.Value);
}
}
And now you can do this:
var stringWrapper = new Wrapper<string>(t => Console.WriteLine("string wrapper"));
var intWrapper = new Wrapper<int>(t => Console.WriteLine("int wrapper"));
stringWrapper.DoCalc();
intWrapper.DoCalc();
Upvotes: 1
Reputation: 6102
If I understand correctly interface is what you are looking for.
Create an interface that requires a method DoStringCalc()
. Then have all the string classes inherit the interface and implement their DoStringCalc()
method. After that you can e.g. use a strongly typed List List<IExampleInterface>
that allows instances of any of the string classes and call their respective DoStringCalc()
from it.
Upvotes: 1