using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Week7_A
{
class Experiment
{
public string NAME { get; set; }
private int NUM { get; set; }
private string COLOR;
private double WEIGHT;
private double VOLUME;
private string[] colors = new string[6] {"Crimson", "Azure", " Taupe", "Mauve", "Vermillion", "Chartreuse"};
public bool error;
public Experiment(string Name, int Num, string Col, double Weight)
{
NAME = Name;
NUM = Num;
COLOR = color;
WEIGHT = weightt;
}
public string color
{
get { return COLOR; }
set
{
foreach(string i in colors)
{
if (value == i)
error = false;
break;
}
}
}
public double weightt
{
get { return WEIGHT; }
set { WEIGHT = value; }
}
}
}
There is serious conceptual confusion in coding. I edited the code you want to do below and developed a test application. I recommend reading this article to get a grasp of the classes. The test application produces the following output:
Name: John, Number: 15, Color: Chartreuse, Weight: 50.75
using System;
namespace Test
{
public class Experiment
{
// "STATIC VARIABLE": You must define "static" data that takes a constant value inside the class.
private static string[] colors = new string[6] {"Crimson", "Azure", " Taupe", "Mauve", "Vermillion", "Chartreuse"};
// "FIELDS": Fields are generally defined as "private" and are closed to external setting.
private string _name;
private int _number;
private string _color;
private double _weight;
private double _volume;
public bool _errorState;
// "CONSTRUCTOR": The constructor is used to initialize an object. It is not necessary to assign directly to fields without checking the value.
public Experiment(string name, int number, string color, double weight)
{
_name = name; // Not recommended use; the value is assigned to the field without checking it.
_number = number; // Not recommended use; the value is assigned to the field without checking it.
Color = color; // Recommended Usage; in the set method of the Property, the field is checked before assigning a value. However, there is a logic error in this usage.
Weight = weight; // Not recommended use; property's set method does not check before assigning the field.
}
// "PROPERTIES": The properties provide the interface for the "private" defined fields to be accessible.
public string Color
{
get { return _color; }
set
{
foreach(string i in colors){
if (value == i) {
_errorState = false;
} else {
_color = i;
}
}
}
}
public double Weight
{
get { return _weight; }
set { _weight = value; }
}
public string Name
{
get { return _name; }
}
public int Number
{
get { return _number; }
}
}
}
public class Program
{
public static void Main()
{
Test.Experiment experiment = new Test.Experiment("John", 15, "blue", 50.75);
Console.WriteLine("Name: {0}, Number: {1}, Color: {2}, Weight: {3}", experiment.Name, experiment.Number, experiment.Color, experiment.Weight);
}
}
Related
here is my main method:
string input = Console.ReadLine(); // 2006 2000 false 1000 500 Windows
List<string> inputs = input.Split(" ").ToList();
Computer one = new Computer(int.Parse(inputs[0]), int.Parse(inputs[1]), bool.Parse(inputs[2]), double.Parse(inputs[3]), double.Parse(inputs[4]), inputs[5]);
one.Print(); // 2006, 2000, false, 1000, 500, Windows
input = Console.ReadLine(); //changeOperatingSystem
if (input == "changeOperatingSystem")
{
string newOperationSystem = Console.ReadLine(); //Linux
Computer two = new Computer(newOperationSystem);
}
one.Print(); //2006, 2000, false, 1000, 500, Linux
the "//" marks what the input and the output look like.
This is my Computer.cs:
private int years;
private int price;
private bool isNotebook;
private double hardDiskMemory;
private double freeMemory;
private string operationSystem;
private string newOperationSystem;
public Computer(int years, int price, bool isNotebook, double hardDiskMemory, double freeMemory, string operationSystem)
{
Years = years;
Price = price;
IsNotebook = isNotebook;
HardDiskMemory = hardDiskMemory;
FreeMemory = freeMemory;
OperationSystem = operationSystem;
}
public Computer(string newOperationSystem)
{
OperationSystem = newOperationSystem;
}
public int Years
{
get { return years; }
set
{
years = value;
}
}
public int Price
{
get { return price; }
set
{
price = value;
}
}
public bool IsNotebook
{
get { return isNotebook; }
set
{
isNotebook = value;
}
}
public double HardDiskMemory
{
get { return hardDiskMemory; }
set
{
hardDiskMemory = value;
}
}
public double FreeMemory
{
get { return freeMemory; }
set
{
freeMemory = value;
}
}
public string OperationSystem
{
get { return operationSystem; }
set
{
operationSystem = value;
}
}
public string NewOperationSystem
{
get { return NewOperationSystem; }
set
{
newOperationSystem = value;
}
}
public void Print()
{
Console.WriteLine($"{years}, {price}, {isNotebook}, {hardDiskMemory}, {FreeMemory}, {operationSystem}");
}
what I want this program to do is whenever the input submitted by the user is "changeOperationSystem" the program gives the user another input where they submit the new Operation system for the pc. Then with the constructor, the Computer.Cs receives the string newOperationSystem, which then should replace the value operationSystem as I tried to do in the constructor: OperationSystem = newOperationSystem, but it doesn't work. I am stil learning classes so don't go hard on me.
It seems to me you are trying to change one property of a class. All you have to do is use the property set method
one.OperationSystem = newOperationSystem;
and remove the property NewOperationSystem altogether.
Here is a bare bones skeleton code of instantiating a class with a constructor and then changing one of the properties
public class Foo
{
private int _a;
private string _b;
private readonly float _c;
public Foo(int a, string b, float c)
{
_a = a;
_b = b;
_c = c;
}
public int A { get => _a; set => _a = value; }
public string B { get => _b; set => _b = value; }
public float C => _c;
public override string ToString()
{
return $"{_a}, {_b}, {_c}";
}
}
class Program
{
static void Main(string[] args)
{
Foo one = new Foo(100, "Philip", 0.75f);
Console.WriteLine(one);
//100, Philip, 0.75
one.B = "Spencer";
Console.WriteLine(one);
//100, Spencer, 0.75
}
}
Another feature of the code above is that there is no need to create a .Print() method, if you can take advantage of the built-in .ToString() which gets called automatically during a Console.WriteLine() or any other operations that require a string input from my class.
Finally, I added a read-only property C to demonstrate that this design is possible. You will not be able to write one.C = 0.25f; since the property is read-only.
I am trying to create a class that contains a header for my list-box. I have two classes that I will be inserting in the listbox. The first class runs fine, but the second one I am making so that the array is entirely of strings is telling me that a method must have a value return type. What does this mean exactly? The error at hand is "HeaderItems."
namespace RETAILITEMSBLAKE
{
class HeaderClass
{
string HeaderDescription;
string HeaderPrice;
string HeaderUnitsonHand;
public HeaderItems(string HeaderDescription, string HeaderUnitsonHand, string HeaderPrice)
{
this.HeaderDescription = HeaderDescription;
this.HeaderUnitsonHand = HeaderUnitsonHand;
this.HeaderPrice = HeaderPrice;
}
public string HeaderDescriptions
{
get
{
return HeaderDescription;
}
set
{
HeaderDescription = value;
}
}
public string HeaderUnits
{
get
{
return HeaderUnitsonHand;
}
set
{
HeaderUnitsonHand = value;
}
}
public string HeaderPrices
{
get
{
return HeaderPrice;
}
set
{
HeaderPrice = value;
}
}
}
Here is my first class that is working correctly:
namespace RETAILitemsBLAKE
{
class ItemizedClass
{
string description;
int unitsonhand;
double price;
public ItemizedClass(string description,int unitsonhand,double price)
{
this.description = description;
this.unitsonhand = unitsonhand;
this.price = price;
}
public string Description
{
get
{
return description;
}
set
{
description = value;
}
}
public double Price
{
get
{
return price;
}
set
{
price = value;
}
}
public int Quantity
{
get
{
return unitsonhand;
}
set
{
unitsonhand = value;
}
}
}
}
So, my goal is to have the HeaderClass so that I can place them as headers in my Listbox. Is there an alternate way to do such? I want to place it on top of the code here:
namespace RETAILitemsBLAKE
{
public partial class FrmItemList : Form
{
ItemizedClass[] items;
public FrmItemList()
{
InitializeComponent();
ItemizedArray();
}
private void ItemizedArray()
{
ItemizedClass jackets = new ItemizedClass("Jackets", 12, 59.95);
ItemizedClass jeans = new ItemizedClass("Jeans", 40, 34.95);
ItemizedClass shirts = new ItemizedClass("Shirts", 20, 24.95);
items = new ItemizedClass[] { jackets, jeans, shirts };
foreach (ItemizedClass RetailData in items)
{
lstRetailitems.Items.Add(RetailData.Description + "\t\t" + RetailData.Quantity + "\t" + "$" + RetailData.Price);
}
}
}
}
Would anyone be of assistance? Thank you!
You are using construct method which needs as same as class name and it didn't need to set return data type, so the method name needs to write HeaderClass in HeaderClass class otherwise it needs to set return data type to be a normal method.
class HeaderClass
{
string HeaderDescription;
string HeaderPrice;
string HeaderUnitsonHand;
public HeaderClass(string HeaderDescription, string HeaderUnitsonHand, string HeaderPrice)
My program throws this exception:
System.StackOverflowException
when the compiler executes the set property.
The wine class:
class wine
{
public int year;
public string name;
public static int no = 5;
public wine(int x, string y)
{
year = x;
name = y;
no++;
}
public int price
{
get
{
return no * 5;
}
set
{
price = value;
}
}
}
The Program class:
class Program
{
static void Main(string[] args)
{
wine w1 = new wine(1820, "Jack Daniels");
Console.WriteLine("price is " + w1.price);
w1.price = 90;
Console.WriteLine(w1.price);
Console.ReadLine();
}
}
When setting the price property, you invoke the setter, which invokes the setter which invokes the setter, etc..
Solution:
public int _price;
public int price
{
get
{
return no * 5;
}
set
{
_price = value;
}
}
You're setting the value of the setter from within the setter. This is an infinite loop, hence the StackOverflowException.
You probably meant to use a backing field no as per your getter:
public int price
{
get
{
return no * 5;
}
set
{
no = value/5;
}
}
or perhaps use its own backing field.
private int _price;
public int price
{
get
{
return _price;
}
set
{
_price = value;;
}
}
However, if the latter is the case, you dont need the backing field at all, you can use an auto property:
public int price { get; set; } // same as above code!
(Side note: Properties should start with an uppercase - Price not price)
Your property setter calls itself when you set any value, thus it produces an stack overflow, I think what you wanted to do was:
public int price
{
get
{
return no * 5;
}
set
{
no = value / 5;
}
}
I need help with this code. If you run the code you'll get in the last 7 lines (which is for...loop for Order object) Exercise.OrderItem.The problem is that I would like to access the OrderItem objects with for...loop but all I get are the last 7 lines representing OrderItem objects.How can I access them in for...loop so that I get the same as in foreach...loop? I think it has something to do with the indexer.Thank You.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Exercise
{
interface IPricing
{
//read/write property
double Price
{
get;
set;
}
double Discount
{
get;
set;
}
}
public class Order:IPricing
{
private string name;
private double price;
private static int OrderNo;
//private ArrayList m_items = new ArrayList();
private ArrayList m_items;
//static constructor
static Order()
{
OrderNo = 0;
}
//default constructor
public Order()
{
name = null;
price = 0;
OrderNo++;
m_items = new ArrayList();
}
//constructor with parameters
public Order(string name, double price)
{
this.name = name;
this.price = price;
OrderNo++;
this.m_items = new ArrayList();
}
//copy constructor
public Order(Order order)
{
this.name = order.name;
this.price = order.price;
this.m_items = order.m_items;
}
public string Name
{
get { return name; }
set { name = value; }
}
public IEnumerable Items
{
get { return m_items; }
private set { }
}
public void AddItem(OrderItem orderItem)
{
orderItem.Order = name;
m_items.Add(orderItem);
}
public static Order operator +(Order o1, Order o2)
{
Order o3 = new Order(o1.name+", "+o2.name,o1.price+o2.price);
o3.m_items.AddRange(o1.m_items);
o3.m_items.AddRange(o2.m_items);
return o3;
}
//indexer
public object this[int index]
{
get
{
m_items[index] = this.m_items[index];
return m_items[index];
}
set { m_items[index] = value; }
}
public double ItemCount
{
get { return m_items.Count; }
private set{}
}
public virtual void Print()
{
Console.WriteLine("*********************Order No. {0}***********************", OrderNo);
Console.WriteLine("Details");
Console.WriteLine("Name: {0}", name);
Console.WriteLine("Price: {0}", price);
}
public double Price
{
get { return price - Discount; }
set { price = value; }
}
public virtual double Discount
{
get { return 0; }
set { ;}
}
public void PrintItems()
{
Console.WriteLine("Items in this order: ");
Console.WriteLine();
foreach(OrderItem itm in this.m_items)
{
Console.WriteLine("Item name: {0,4};\tPart of order: {1}", itm.Name, itm.Order);
}
}
}
public class OrderItem
{
private string m_name; //name of the item in order
private string m_order; //name of the order whose parts are items with names m_name
//default constructor
public OrderItem()
{
m_order = null;
}
//parameter constructor
public OrderItem(string name)
{
this.m_name = name;
this.m_order = null;
}
//copy constructor
public OrderItem(OrderItem orderItem)
{
this.m_name = orderItem.m_name;
this.m_order = orderItem.m_order;
}
//Name read/write property
public string Name
{
get { return m_name; }
set { m_name = value; }
}
//Order read/write property
public string Order
{
get { return m_order; }
set { m_order = value; }
}
}
public class MainProgram
{
static void Main(string[] args)
{
string order1 = "Desktop PC";
Order desktopPC = new Order(order1,25000);
desktopPC.AddItem(new OrderItem("pc mouse"));
desktopPC.AddItem(new OrderItem("keyboard"));
desktopPC.AddItem(new OrderItem("monitor"));
desktopPC.AddItem(new OrderItem("pc"));
desktopPC.Print();
desktopPC.PrintItems();
Console.WriteLine();
string order2 = "Notebook";
Order notebook = new Order(order2, 54000);
notebook.AddItem(new OrderItem("mouse"));
notebook.AddItem(new OrderItem("bag"));
notebook.AddItem(new OrderItem("notebook"));
notebook.Print();
notebook.PrintItems();
Console.WriteLine();
Order total = desktopPC + notebook;
total.Print();
total.PrintItems();
Console.WriteLine();
Console.WriteLine("Getting the items via for loop");
for (int k = 0; k < total.ItemCount; k++)
{
Console.WriteLine(total[k]);
}
Console.ReadKey();
}
}
}
for (int k = 0; k < total.ItemCount; k++)
{
var x = total[k] as OrderItem;
if (x == null) continue;
Console.WriteLine(x.Name);
Console.WriteLine(x.Order);
}
I am totally unable to access the outer class attributes inside the inner class ...
even if i make object of outer class,, in inner class*which makes no sense in composition design* .. even then i cant access them ..
is there a way by which i can access these outer class attributes ?
Scenario is that there is some sports car which is constructed only if the customers who want to buy it exists! ..
namespace composition{
public class CustomCar
{
#region Attributes
private string name;
private string plateno;
private double cost;
private CarCustomer _customer = new CarCustomer();
#endregion
#region properties
public string Name
{
get { return name; }
set { name = value; }
}
public double Cost
{
get { return cost; }
set { cost = value; }
}
public string PlateNo
{
get { return plateno; }
set { plateno = value; }
}
public CarCustomer Customer
{
get { return _customer; }
set { _customer = value; }
}
#endregion
#region methods
public CustomCar()
{
Console.WriteLine("I am in custom car");
}
public CustomCar(string s1, string pno, double c, string s2, double n, double bc)
{
this.Name = s1;
this.PlateNo = pno;
this.Cost = c;
this.Customer.Name1 = s2;
this.Customer.Nic1 = n;
this.Customer.BargainCost = bc;
}
public double finalCost()
{
if (this.Customer.BargainCost < 10000)
{
double FinalCost = (this.Cost - this.Customer.BargainCost);
return FinalCost;
}
else
{
return this.Cost;
}
}
public void show()
{
Console.WriteLine(this.name + this.PlateNo + this.Customer.Name1 + this.Customer.Nic1);
}
#endregion
public class CarCustomer
{
private string name1;
private double Nic;
private double bargainCost;
public double BargainCost
{
get { return bargainCost; }
set { bargainCost = value; }
}
public double Nic1
{
get { return Nic; }
set { Nic = value; }
}
public string Name1
{
get { return name1; }
set { name1 = value; }
}
public CarCustomer()
{
Console.WriteLine("I have a customer");
}
public CarCustomer(string n1, double i1, double bc)
{
this.Name1 = n1;
this.Nic = i1;
this.BargainCost = bc;
}
public void showCustomer()
{
Console.WriteLine("Customer name: " + Name1);
Console.WriteLine("Customer NIC: " + Nic1);
}
}
}
}
There is nothing stopping you having a reference in the CarCustomer to the CustomCar object as well. This would then give you a one to one reference between the object. Were you instaiate this object is up to you in the Constructor of the CustomCar
public CustomCar(arguments)
{
this.Customer.CustomCar = this;
}
Or you could set it in the sets on the property accessors up to you. Try this
public class CustomCar
{
private string name;
private string plateno;
private double cost;
private CarCustomer _customer = new CarCustomer();
public string Name
{
get { return name; }
set { name = value; }
}
public double Cost
{
get { return cost; }
set { cost = value; }
}
public string PlateNo
{
get { return plateno; }
set { plateno = value; }
}
public CarCustomer Customer
{
get { return _customer; }
set { _customer = value; }
}
public CustomCar()
{
Console.WriteLine("I am in custom car");
}
public CustomCar(string name, string pno, double c, string customerName, double n, double bc)
{
this.Name = name;
this.PlateNo = pno;
this.Cost = c;
this.Customer.Name1 = customerName;
this.Customer.Nic1 = n;
this.Customer.BargainCost = bc;
this.Customer.Car = this;
}
public double finalCost()
{
if (this.Customer.BargainCost < 10000)
{
double FinalCost = (this.Cost - this.Customer.BargainCost);
return FinalCost;
}
else
{
return this.Cost;
}
}
public void show()
{
Console.WriteLine(this.name + this.PlateNo + this.Customer.Name1 + this.Customer.Nic1);
}
}
public class CarCustomer
{
private string name1;
private double Nic;
private double bargainCost;
private CustomCar customer;
public double BargainCost
{
get { return bargainCost; }
set { bargainCost = value; }
}
public double Nic1
{
get { return Nic; }
set { Nic = value; }
}
public string Name1
{
get { return name1; }
set { name1 = value; }
}
public CustomCar Car
{
get{return customer;}
set{customer = value;}
}
public CarCustomer()
{
Console.WriteLine("I have a customer");
}
public CarCustomer(string n1, double i1, double bc)
{
this.Name1 = n1;
this.Nic = i1;
this.BargainCost = bc;
}
public void showCustomer()
{
Console.WriteLine("Customer name: " + Name1);
Console.WriteLine("Customer NIC: " + Nic1);
}
}
Of course you can't access them. You've set their protection level to private. In order to get at them from an external resource their protection level has to be in line with the access level needed. In this case you should be able to change the modifier to protected and be able to access them.
However, looking at your class design, I think you would be better served using the automatic getter/setter syntax. You aren't doing anything particularly special in your property definitions, so it would make sense to get rid of the private variables and change your properties to this:
public string Name { get; set; }
public double Cost { get; set; }
public string PlateNo { get; set; }
public CarCustomer Customer { get; set; }
You'll still have public access to the variables through the properties and you won't have all the messiness of the extra variables.