Reputation:
I am working on a small application to get a grips with C# and i have written a small application that currently adds up values of items (currently predefined) Here is what i have so far:
//Defining classes
public class Item1{
public string Type{get{return "Item1";}}
}
public class Item2{
public string Type{get{return "Item2";}}
}
//Methods
public void CalcItems(Item1 item1, int val){
this.Log(item1.Type + "Val:" + val);
this.total += val;
}
public void CalcItems(Item2 item2, int val){
this.Log(item2.Type + "Val:" + val);
this.total += val;
}
//Calling these methods
Items.CalcItems(new Item1(), 30);
Items.CalcItems(new Item2(), 12);
How can I pass both Item1 and Item 2 through one calc method?
Upvotes: 0
Views: 139
Reputation: 79
You can do the refactoring using the Strategy design pattern. Declare an interface and implement that interface to your Item1 and Item2 class. Do the calculation operation using that interface.
public interface IItem {
string Type { get; }
}
public class Item1: IItem {
public string Type{get{return "Item1";}}
}
public class Item2: IItem {
public string Type{get{return "Item2";}}
}
public void CalcItems(IItem item, int val){
this.Log(item.Type + "Val:" + val);
this.total += val;
}
Items.CalcItems(new Item1(), 30);
Items.CalcItems(new Item2(), 12);
Upvotes: 0
Reputation: 1730
Use an Interface
:
public interface IItem
{
string Type { get; }
}
Then implement the interface on your class declarations:
public class Item1 : IItem
{
...
public string Type { get; }
...
}
public class Item2 : IItem
{
...
public string Type { get; }
...
}
Now we can define the method CalcItems()
as accepting an IItem
parameter:
public void CalcItems(IItem item, int val)
{
this.Log(item1.Type + "Val:" + val);
this.total += val;
}
Such that the following would now reference the same method:
Items.CalcItems(new Item1(), 30);
Items.CalcItems(new Item2(), 12);
Upvotes: 1
Reputation: 6950
You could make use of Generics. Define an interface for your Item objects and declare the method like so:
void CalcItems<T>(T item, int val) where T : IItem
Upvotes: 0
Reputation: 1018
You could define an interface for both Item1
and Item2
as they both share the common property Type
.
MSDN: Interfaces (C# Programming Guide)
public interface IMyItem
{
string Type;
}
public class Item1 : IMyItem
{
public string Type{get{return "Item1";}}
}
public class Item2: IMyItem
{
public string Type{get{return "Item2";}}
}
public void CalcItems(IMyItem item, int val){
this.Log(item.Type + "Val:" + val);
this.total += val;
}
Items.CalcItems(new Item1(), 30);
Items.CalcItems(new Item2(), 12);
Upvotes: 1
Reputation: 1488
Add an IItem interface to your items, and replace Item1 in Calcitems with iitem. Then you dont need both calcItems
Upvotes: 1