I am learning Visual Studio C#. I am having some difficulty. My background is procedural programs like Basic & Cobol. Below is some sample code for a console program in C#.
I get most of it, but I don't get this part.
Box box = new Box();
Box box2;
box2 = new Box();
The above statements are where I get confused.
Box box = new Box();
This is how you, well I don't know the technical names but it allow you to use the class along with the code(methods) and variables (properties).
These two lines do the same thing.
Box box2;
box2 = new Box();
What does each line do? I know that a copy of some sort is created from the Box Class. I know that you reference the class with the new name of box or whatever you have choosen. What does each line do? Is the first line just creating a link/reference to the original class? When is memory actually reserved? When do the variables/properties of the class get assigned values/initilized?
Like I said I have written procedural code for a number of years and each command has a function. I am used to knowing the exact function of each command. How it affects the computer and memory. Can anyone help explain what is going on in the computer? Memory?
Thanks.
The entire code is below.
using System;
namespace ClassesAndObjects
{
class Program
{
static void Main(string[] args)
{
// Create Object Class Type Box
Box box = new Box();
Box box2;
box2 = new Box();
Console.WriteLine("Enter Length: ");
double n1 = double.Parse(Console.ReadLine());
Console.WriteLine("Enter Breath: ");
double n2 = double.Parse(Console.ReadLine());
Console.WriteLine("Enter Height: ");
double n3 = double.Parse(Console.ReadLine());
//setting values
box.Length = n1;
box.Breath = n2;
box.Height = n3;
double volumn = box.getVolumn();
double area = box.getArea();
Console.WriteLine($"Box Dimensions Are: {box.Length}, {box.Breath}, {box.Height}");
Console.WriteLine($"Box Volumn is: {volumn}");
Console.WriteLine($"Box Area is: {area}");
// box 2
// you don't need to define n1, n2 & n3.
// They were already defined in box 1
// so the word double is not needed in front of
// n1, n2 & n3
Console.WriteLine("Enter Length box 2: ");
n1 = double.Parse(Console.ReadLine());
Console.WriteLine("Enter Breath box 2: ");
n2 = double.Parse(Console.ReadLine());
Console.WriteLine("Enter Height box 2: ");
n3 = double.Parse(Console.ReadLine());
//setting values
box2.Length = n1;
box2.Breath = n2;
box2.Height = n3;
//double volumn = box.getVolumn();
//double area = box.getArea();
// instead of storing the answers in variables
// we just call the method in the console writeline statement
Console.WriteLine($"Box Dimensions Are: {box2.Length}, {box2.Breath}, {box2.Height}");
Console.WriteLine($"Box Volumn is: {box2.getVolumn()}");
Console.WriteLine($"Box Area is: {box2.getArea()}");
}
}
class Box
{
public double Length { get; set; }
public double Breath { get; set; }
public double Height { get; set; }
// Here is a method
public double getVolumn()
{
return Length * Breath * Height;
}
public double getArea()
{
return Length * Breath;
}
}
}