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 do
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!
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;
}
}
in class2
you need to make an object from class1
public class class2
{
class1 class1 = new class1();
public double z = class1.x + 5;
}
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