4

I'm quite new to C# and OOP so please bear with me.

I have two classes with different namespaces:

namespace Class1

public class class1
{
 public double x;
 public double y;
}

...

namespace Class2

public class class2
{
 public double z = x + 5;
}

I have created a project called add and have a form with a button. The button will add x, y and z. My questions are:

How do I use field x in class2 and how do I use fields x, y and z under button click event?

4
  • Take a step back for a moment and define what your classes actually represent. They're not just buckets of variables. class1 and class2 naturally don't convey any meaning. What you're trying to accomplish can likely be done in any number of ways, but being "new to OOP" we should focus on determining the right way. So, outside of a contrived example, what physical or conceptual objects are your classes meant to represent?
    – David
    Aug 7, 2012 at 1:07
  • I have two classes where a variable in class1 needs to be used in class2. For example, area = PI * r^2 is a method in class1. Volume = area * depth is a method in class2. Also, I have a form with a button which will calculate the area and volume. What I cam trying to work out is how do I use r in class1 and class2 and then use these variables in the button event. I hope this is more clear. Thank you for you initial response. Aug 7, 2012 at 1:14
  • I updated my answer after your clarification, I hope it helps. :)
    – Diego
    Aug 7, 2012 at 1:37
  • @user1580591: "area = PI * r^2" and "volume = area * depth" - The point I'm getting at, though, is that these don't sound like meaningful class distinctions. In the case of geometry, each individual complete shape would probably be a class. So if there's some 3-dimensional object being modeled, it itself would be a class. Its various dimensions and measurements would be properties (static or calculated) on that one class. Being able to access classes from each other is one aspect of OOP, but being able to have meaningful class distinctions really comes first.
    – David
    Aug 7, 2012 at 11:31

4 Answers 4

2

You'd probably want to have class2 take in an instance of class1 in its constructor:

public class class2
{
 private readonly class1 _c1;
 public class2(class1 c1) { _c1 = c1; }

 public double z = _c1.x + 5;
}

As for how you'd use fields x,y and z with a button click event in a form, you would just access the public fields x, y and z on class1 and class2 instances:

protected void button_click(){
 class1 c1 = new class1();
 c1.x = 10;
 c1.y = 20;
 class2 c2 = new class2(c1);

 //do something with c1 and c2 now...
 Console.WriteLine("{0} {1} {2}", c1.x.ToString(), c1.y.ToString(), c2.z.ToString());
}

Let me know if I misunderstood what you're looking to do. Hope this helps!

2
  • Thank you for your response David. This is what I am trying to do. Aug 7, 2012 at 1:34
  • 1
    Glad I could help. If this answers your question, don't forget to mark it as an answer. It will help your accept percentage, which will attract more quality answers on future questions. Thanks! Aug 7, 2012 at 2:57
1

You don't use class fields (unless they are static, but in your case they are not), but object fields. Here's an example of how you can achieve what you want.

public class1 {
  public double Radius;

  // Function to calculate the area
  public double Area(double Rad) {
    this.Radius = Rad;
    return Math.PI * Math.Pow(this.Radius, 2);
  }

}

public class2 {
  public double Depth;

  // Function to calculate the volume of a cylinder
  public double Volume(double Rad, double Dep) {
    this.Depth = Dep;

    // Create an instance of Class1 and use it to calculate the Volume
    var Obj1 = new class1();
    return Obj1.Area(Rad) * this.Depth;
  }
}

How to use the above in a button click event

// Let's calculate an Area from a Radius
double SomeRadius = 1.234;

MyObj1 = new class1();
double Area = MyObj1.Area(SomeRadius);

double StoredRadius = MyObj1.Radius; // This will give you back the Radius stored by MyObj1, which is the same you passed to Area() function

// Now let's calculate a Volume, using the Radius we indicated earlier and a Depth
double SomeDepth = 4.567;
MyObj2 = new class2();
double Volume = MyObj2.Volume(SomeRadius, SomeDepth);

double StoredDepth = MyObj2.Depth; // This will give you back the Depth stored by MyObj2, which is the same you passed to Volume() function
4
  • What is not in the declaration of the class?
    – Diego
    Aug 7, 2012 at 1:14
  • You are declaring the variables outside of the class, he wants to access the variables inside of the class. Aug 7, 2012 at 1:17
  • I'm instantiating object to use the classes, which shows how to use object's properties. This answers to the question he was asking about using x, y and z in a method. Now he updated the question, which makes things clearer.
    – Diego
    Aug 7, 2012 at 1:19
  • Ah I understand now, I have edited to make sure it is more clear for future viewers Aug 7, 2012 at 1:21
0

in class2 you need to make an object from class1

public class class2
{
class1 class1 = new class1(); 

public double z = class1.x + 5;
}
0

Everybody here gave you the right answer for accessing the fields, but in the case of area and volume you are approaching the problem in a procedural, not OO way. This is an example showing you how to access the internal fields, and an OO way to approach this kind of problems:

public abstract class Shape
{
    public abstract double Area();
    public abstract double Perimeter();
}

public class Circle : Shape
{
    public double Radius;

    public override double Perimeter()
    {
        return 2 * Radius * Math.PI;
    }

    public override double Area()
    {
        return Radius * Radius * Math.PI;
    }
}

public class Square : Shape
{
    public double Side;

    public override double Perimeter()
    {
        return Side * 4;
    }

    public override double Area()
    {
        return Side * Side;
    }
}

public abstract class Solid
{
    public abstract double Volume();
}

public abstract class CircleBaseSolid : Solid
{
    protected Circle Base = new Circle();

    public double Radius
    {
        get { return Base.Radius; }
        set { Base.Radius = value; }
    }

    public double Height;
}

public class Cylinder : CircleBaseSolid
{
    public override double Volume()
    {
        return Base.Area() * Height;
    }
}

public class Cone : CircleBaseSolid
{
    public override double Volume()
    {
        return Base.Area() * Height / 3;
    }
}

public abstract class SquareBaseSolid : Solid
{
    protected Square Base = new Square();

    public double Side
    {
        get { return Base.Side; }
        set { Base.Side = value; }
    }

    public double Height;
}

public class SquareParallelepiped : SquareBaseSolid
{
    public override double Volume()
    {
        return Base.Area() * Height;
    }
}

public class SquarePyramid : SquareBaseSolid
{
    public override double Volume()
    {
        return Base.Area() * Height / 3;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.