0

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?

tequila slammer
  • 2,821
  • 1
  • 18
  • 25
  • What do you mean by "group"? Do you want both `Item1` and `Item2` to be passed to the same `CalcItems` method? – Jamiec Feb 27 '16 at 08:55
  • @Jamiec Yes, pass both the two Items through the same method. Sorry couldnt find the wording haha –  Feb 27 '16 at 08:57

5 Answers5

1

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);
Lemonseed
  • 1,644
  • 1
  • 15
  • 29
  • How could i implement this with all classes, that you can see in the question being in separate files. –  Feb 27 '16 at 09:01
  • You can implement `IItems` on your class definitions. If you do not have access to the source code, inherit from the classes you are using in order to implement the interface. (e.g., `public class MyItem1 : Item1, IItem` would be a possible class declaration). – Lemonseed Feb 27 '16 at 09:04
  • Perfect! Do you have any recommendations on any reading materials which I could go through for programming in C#, As my knowledge in it is not as proficient as other languages. Thanks –  Feb 27 '16 at 09:39
  • Hands-on practice is always best; search for your answers with resources like SO. Also, [dotNetPearls](http://www.dotnetperls.com/) is a good resource giving some good explanations for frequently encountered situations. – Lemonseed Feb 28 '16 at 01:52
1

Add an IItem interface to your items, and replace Item1 in Calcitems with iitem. Then you dont need both calcItems

andreasnico
  • 1,478
  • 14
  • 23
1

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);
Dan
  • 968
  • 1
  • 8
  • 21
0

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
Community
  • 1
  • 1
ekostadinov
  • 6,880
  • 3
  • 29
  • 47
0

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);
Fagun
  • 79
  • 3
  • 3