Chris
Chris

Reputation: 23

C# object (2 numbers) performing 2 calculations

I have a couple questions about creating a object (2 values) and how to "call" it.

Initializing the object with:

Tweetal t1, t2, t3, t4, t5, t6;
t1 = new Tweetal();      //a: 0 , b = 0
t2 = new Tweetal(-2);    //a: -2, b = -2
t3 = new Tweetal(5, 17); //a: 5, b = 17
t4 = new Tweetal(t3);    //a:5, b = 17

Console.Write("t1 = " + t1);
Console.Write("\tt2 = " + t2);
Console.Write("\tt3 = " + t3);
Console.Write("\tt4 = " + t4);
Console.WriteLine("\n");
t1 = t1.Som(t2);
t4 = t2.Som(t2);
//......

Now the 2 things i want to do with this object are taking the SUM and the SUMNumber: Sum: t4 = t2.sum(t3); (this would result in t4: a:3 (-2+5), b: 15(-2+17) SumNumber: t1 = t3.sum(8) (this would result in t1: a:13 , b:25)

Sum -> parameter for example t3, t2 etc... SumNumber -> parameter 7, 5,... or 2 numbers (5,7) ...

Next is my code for the object (in a separate class), but how exactly do i perform the simple sum calculation when i call up for example t2 etc...

public class Tweetal: Object
{
    private int a;
    private int b;

    public Tweetal()
    {
        //???
        //Sum(...,...)
    }
    public Tweetal(int a)
    {
        //???
        //Sum(...,...)
    }
    public Tweetal(int a, int b)
    {
        //???
    }
    public Tweetal(Tweetal //....) // to call upton the object if i request t1, t2, t3,... insteed of a direct number value)
    {
       // ????
    }


    public void Sum(int aValue, int bValue)
    {
        //a = ???
        //b = ???
        //Sum(...,...)
    }

    public void SumNumber(int aValue, int bValue)
    {

    }


    public override string ToString()
    {
        return string.Format("({0}, {1})", a, b);
    }/*ToString*/
}

Upvotes: 1

Views: 168

Answers (4)

Asher
Asher

Reputation: 1026

I was planning to add to Jon Skeet's answer by mentioning that he did not give an answer for

public Tweetal(Tweetal t) 
{ 
    a = t.a; 
    b = t.b; 
} 

and warn not to implement ICloneable like this


class Tweetal: ICloneable
{
...
  object ICloneable.Clone() 
  { 
    return this.Clone(); 
  }
  public virtual Tweetal Clone()
  {
    return (Tweetal) this.MemberwiseClone();
  }
}

because of this reason and rather suggest creating

Copy(Tweetal objectToCopy)
method cause it's meaning is clearer than the param in the constructor.

I was gonna do all that but thought better of it as Jon's answer is good enough and all that is just to much typing.

Upvotes: 0

Matt Ellen
Matt Ellen

Reputation: 11612

First create a constructor that initialises the a and b members:

public Tweetal(int a, int b)
{
    this.a = a;
    this.b = b;
}

Then set up A and B accessors, e.g.:

public int A { 
                 get {return a;} 
                 set {a = value;}
             }

public int B { 
                 get {return b;} 
                 set {b = value;}
             }

Then make the sum method return a new Tweetal.

public Tweetal sum(Tweetal rhs)
{
    int a = this.a + rhs.A;
    int b = this.b + rhs.B;
    return new Tweetal(a, b);
}

Finally your sumNumber method

public Tweetal sumNumber(int newVal)
{
    int a = newVal + this.a;
    int b = newVal + this.b;
    return new Tweetal(a,b);
}

Upvotes: 1

James Curran
James Curran

Reputation: 103605

public TweetalSum(int aValue, int bValue) 
{ 
    return new Tweetal(this.a + aValue, this.b + bValue);      
} 

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1504062

Well, the construction part is a matter of having three overloads (assuming you don't want to use optional parameters from C# 4):

public Tweetal() : this(0, 0)
{
}

public Tweetal(int a) : this(a, a)
{
}

public Tweetal(int a, int b)
{
    this.a = a;
    this.b = b;
}

Then you need to change your Sum method to return another `Tweetal``:

public Tweetal Sum(Tweetal other)
{
    return new Tweetal(a + other.a, b + other.b);
}

Personally I'd call it Plus rather than Sum though, and possibly add an operator overload:

public static Tweetal operator +(Tweetal first, Tweetal second)
{
    return first.Plus(b);
}

Then you could write:

t1 = t1 + t2;

etc. You should add checks for null arguments in the above code, however - and probably throw an ArgumentNullException where appropriate.

Upvotes: 6

Related Questions