Click here to Skip to main content
14,979,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The field is never used not sure why? --> private int realValue and private int imaginaryValue

What I have tried:

using System;

class Complex
    { 

    public class ComplexNum
{
    private int realValue;
    private int imaginaryValue;

}

   public Complex(int realPart, int imaginaryPart)
        {
           this.realValue = realPart;
            this.imaginaryValue = imaginaryPart;
        
        }

    public Complex()
    {
        this.realValue = 0;
       this. imaginaryValue = 0;
}

        public int realValue
        {
            get
                { return realValue; }
            set
                { realValue = value; }

        }

        public int imaginaryValue
        {
            get
               { return imaginaryValue; }
            set
                { imaginaryValue = value; }
         }

        public override string ToString()
        {
    return string.Format(" ({0}, {1})", realValue, imaginaryValue);

            }

    public void add(Complex a, Complex b)
{
    this.realValue = a.realValue + b.realValue;
    this.imaginaryValue = a.imaginaryValue + b.imaginaryValue;

}
    public void subtract(Complex a, Complex b)
{
    this.realValue = a.realValue + b.realValue;
    this.imaginaryValue = a.imaginaryValue + b.imaginaryValue;
        Console.WriteLine("Press enter to exit.");
        Console.ReadLine();
}
   
    }

    public class ComplexTest
        {
            
    public static void Main(string[] args)
    {
        Console.WriteLine("Complex Numbers\n");

        Complex a = new Complex(6, 8);
        Complex b = new Complex(10, 18);

        Complex result1 = new Complex();
        Complex result2 = new Complex();

        Console.WriteLine("1st Number:");
        Console.WriteLine(a.ToString());
        Console.WriteLine("2nd Number:");
        Console.WriteLine(b.ToString());

        result1.add(a, b);
        Console.Write("\nTest Addition:\nAfter Adding:");
        Console.WriteLine(result1.ToString());
        Console.ReadLine();


        result2.subtract(a, b);

        Console.Write("\nTest Subtraction:\nAfter Subtracting: ");
        Console.WriteLine(result2.ToString());
        Console.ReadLine();

        Console.WriteLine("Press enter to exit.");
        Console.ReadLine();




    }
}
Posted
Updated 7-Feb-17 14:27pm
Comments
Patrice T 7-Feb-17 20:22pm
   
try to make your mind and ask a clear question.
The title is about a problem and the text about something else.
[no name] 7-Feb-17 20:30pm
   
IF we were to stumble across your posting (it's not a question and there is no description of a problem) and we were to assume that the title of your posting were supposed to be the actual question then you are getting that because you are setting your properties inside your setter over and over until you overflow your stack. Learning how to use the debugger to debug your code would have told you that.

1 solution

add and subtract are looking incredibly the same.
-----
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- You see your code behavior, you match it against your expectations.

secondary effects
- Your will be proud of finding bugs yourself.
- Your skills will improve.

You should find pretty quickly what is wrong.
   
v2
Comments
[no name] 7-Feb-17 20:39pm
   
Countered, sort of. You are answering the next question of "why my add method works but subtract not works?".
Patrice T 7-Feb-17 21:31pm
   
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900