C# Delegate and Events - c#

I am working with delegates and events for the first time and help. When the program runs its supposed to show the text in blue if the gpa has increased from the original and red if it has decreased. I have to use two events IncreaseGpaEvent and DecreaseGpaEvent. Have to have a color variable and is assigned in the ToString(). Any help is greatly appreciated.
public delegate void UpdateGPADelegate(string ConsoleColor);
class Student
{
// Fields
private string studentName;
private double gpa;
public string ConsoleColor;
public event UpdateGPADelegate IncreaseGpaEvent;
public event UpdateGPADelegate DecreaseGpaEvent;
// Properties
public string StudentName { get; set; }
public double GPA
{
get { return gpa; }
set
{
IncreaseGpaEvent += onUpdate;
if (!(value > gpa))
{
gpa = value;
IncreaseGpaEvent("Red");
}
DecreaseGpaEvent += onUpdate;
if(!(value < gpa))
{
gpa = value;
DecreaseGpaEvent("Blue");
}
}
}
public Student(string studentName, double gpa)
{
this.studentName = studentName;
this.gpa = gpa;
}
public void onUpdate(double value)
{
if (value < gpa)
{
ConsoleColor = "Red";
}
if (value > gpa)
{
ConsoleColor = "Blue";
}
}
public override string ToString()
{
string str;
str = string.Format($"Name: {StudentName} GPA: {GPA}");
return str;
}
}

Try This..
public delegate void UpdateGPADelegate(double ConsoleColor);
class Student
{
// Fields
// private string studentName;
private double gpa;
public string ConsoleColor;
public event UpdateGPADelegate IncreaseGpaEvent;
public event UpdateGPADelegate DecreaseGpaEvent;
// Properties
public string StudentName { get; set; }
public double GPA
{
get { return gpa; }
set
{
if (!(value > gpa))
{
IncreaseGpaEvent(value);
gpa = value;
}
if(!(value < gpa))
{
DecreaseGpaEvent(value);
gpa = value;
}
}
}
public Student(string studentName, double gpa)
{
this.studentName = studentName;
this.gpa = gpa;
IncreaseGpaEvent += onUpdate;
DecreaseGpaEvent += onUpdate;
}
public void onUpdate(double value)
{
if (value < gpa)
{
ConsoleColor = "Red";
}
if (value > gpa)
{
ConsoleColor = "Blue";
}
}
public override string ToString()
{
string str;
str = string.Format($"Name: {StudentName} GPA: {GPA} ConsoleColor: {ConsoleColor}");
return str;
}
}
There were somethings wrong with the code which i have fixed like you were assigning value to gpa after that events were raised which never would be greater or smaller and signature of delegate was also wrong.

Related

Can anybody pinpoint the cause of this StackOverflowException?

Trying to learn to program and once again losing all confidence because I think I've internalised a simple concept but something seemingly extraordinary is happening or it is just flying right over my head.
When I run the program I get a StackOverFlowException if I access the property by assigning a string to FirstName or SecondName
My Customer Class:
class Customer : ICustomer
{
public string FirstName
{
get
{
return FirstName;
}
set
{
FirstName = value;
}
}
public string fName
{
get
{
return fName;
}
set
{
fName = value;
}
}
public string SecondName
{
get
{
return SecondName;
}
set
{
SecondName = value;
}
}
public string sName
{
get
{
return sName;
}
set
{
sName = value;
}
}
public int ID
{
get
{
return ID;
}
set
{
ID = value;
}
}
public int mId
{
get
{
return mId;
}
set
{
mId = value;
}
}
public int GetID()
{
return mId;
}
public void SetID(int id)
{
mId = ID;
}
public void SetName(string fName, string sName)
{
fName = FirstName;
sName = SecondName;
}
}
and the main program
class Program
{
/// <summary>
/// Create unique string code based off current date and time.
/// </summary>
/// <returns>code string</returns>
static string generateUniqueCode()
{
string characters = "abcdefghijklmnopqrstuvqxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string ticks = DateTime.UtcNow.Ticks.ToString();
var code = "";
for (var i = 0; i < characters.Length; i += 2)
{
if ((i + 2) <= ticks.Length)
{
var number = int.Parse(ticks.Substring(i, 2));
if (number > characters.Length - 1)
{
var one = double.Parse(number.ToString().Substring(0, 1));
var two = double.Parse(number.ToString().Substring(1, 1));
code += characters[Convert.ToInt32(one)];
code += characters[Convert.ToInt32(two)];
}
else
code += characters[number];
}
}
return code;
}
/// <summary>
/// Creates unique integer code based off current date and time.
/// </summary>
/// <returns>integer code</returns>
static int generateUniqueCode(int seed)
{
string characters = "0123456789";
Random randInt = new Random(seed);
var ticks = randInt.Next();
int code = 0;
for (var i = 0; i < characters.Length; i += 2)
{
if ((i + 2) <= ticks)
{
var number = ticks;
if (number > characters.Length - 1)
{
var one = double.Parse(number.ToString().Substring(0, 1));
var two = double.Parse(number.ToString().Substring(1, 1));
code += characters[Convert.ToInt32(one)];
code += characters[Convert.ToInt32(two)];
}
else
code += characters[number];
}
}
return code;
}
static void Main(string[] args)
{
Customer customer = new Customer();
int generatedIntCode = generateUniqueCode(1);
customer.FirstName = "Conor";
customer.SecondName = "MacFirbhisigh";
customer.SetID(generatedIntCode);
Console.WriteLine("{0}, {1} {2}", customer.ID, customer.FirstName, customer.SecondName);
//Console.ReadKey();
}
}
In the getter and setter of FirstName (and all others), you are calling the same property over and over again. The endless loop you created will result in a StackOverflowException.
If you don't want to add custom logic to your properties, just use auto-implemented properties:
public string FirstName
{
get;
set;
}
If you did want to implement the property on your own, creating your own backing fields, this is what it should look like (this is effectively the same as what the above code would generate):
private string firstName; // backing field
public string FirstName
{
get
{
return this.firstName; // return the backing field
}
set
{
this.firstName = value; // set the backing field
}
}
You left out a few important parts
Firstly, you always need to declare you variables,
public - for outside get and set, thus no need for the get-set methods.
private - get/set methods are necessary for data retrieval modification.
Also, see the SetName method.
Hope it helps :p
This should do the trick:
class Customer : ICustomer
{
private string firstName;
private string name;
private string secondName;
private string sName;
private int iD;
private int mId;
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string SecondName
{
get
{
return secondName;
}
set
{
secondName = value;
}
}
public string SName
{
get
{
return sName;
}
set
{
sName = value;
}
}
public int ID
{
get
{
return iD;
}
set
{
iD = value;
}
}
public int MId
{
get
{
return mId;
}
set
{
mId = value;
}
}
public void SetName(string fName, string sName)
{
FirstName = fName;
SecondName = sName ;
}
}

C# - Order by an Object's property in List<T>

So i'm trying to make this console Program where you can add comments and a rating to a certain book. A certain comment can also be upvoted.
Here is my Comment.cs
class Comment
{
#region state
private readonly string name;
private readonly string commentary;
private readonly uint rating;
private uint votes;
#endregion state
#region constructor
public Comment(string name , string commentary, uint rating)
{
this.name = name;
this.commentary = commentary;
this.rating = rating;
this.votes = 0;
}
#endregion
#region properties
public string Name
{
get { return name; }
}
public string Commentary
{
get { return commentary; }
}
public uint Rating
{
get { return rating; }
}
public uint Votes
{
get { return votes; }
private set { votes = value; }
}
#endregion
#region behaviour
public void VoteHelpfull()
{
Votes++;
}
public override string ToString()
{
string[] lines ={
"{0}",
"Rating: {1} - By: {2} voterating: {3}"
};
return string.Format(
string.Join(Environment.NewLine,lines),Commentary,Rating,Name,Votes);
}
#endregion
}
You can add comments to a book by where they are stored in List<Comment> Comments
class Book
{
#region state
private readonly string bookname;
private readonly decimal price;
private List<Comment> comments;
#endregion
#region constructor
public Book(string bookname,decimal price)
{
this.bookname = bookname;
this.price = price;
comments = new List<Comment>();
}
#endregion
#region properties
private List<Comment> Comments
{
get { return comments; }
set { comments = value; }
}
public string Bookname
{
get { return bookname; }
}
public decimal Price
{
get { return price; }
}
#endregion
#region behaviours
public void AddComment(string name, string commentary, uint rating)
{
Comments.Add(new Comment(name, commentary, rating));
}
public override string ToString()
{
string s = string.Format("{0} - {1} euro - {2} comments",Bookname,Price,Comments.Count);
foreach (Comment c in Comments)
{
s += Environment.NewLine;
s += c;
}
return s;
}
I'm trying to Order the list of comments a book has by the votes property of my comment object has but I can't seem to make it work...
Try this:
foreach (Comment c in Comments.OrderBy(c=>c.Votes))
{
.....
}

operator to access other classes's objects

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);
}

How can the attributes of outer class can be accesed within the inner class in Composition Classes design?

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.

C# to Java Conversion

I'm having trouble converting especially the getter and setter.
public class CartItem : IEquatable<CartItem>
{
#region Attributes
public int Quantity { get; set; }
private int _productId;
public int ProductId
{
get { return _productId; }
set
{
_product = null;
_productId = value;
}
}
private Product _product = null;
public Product Prod
{
get
{
if (_product == null)
{
_product = new Product(ProductId);
}
return _product;
}
}
public string Name
{
get { return Prod.ProductName; }
}
public string Description
{
get { return Prod.Description; }
}
public float UnitPrice
{
get { return Prod.UnitPrice; }
}
public float TotalPrice
{
get { return UnitPrice * Quantity; }
}
#endregion
#region Methods
public CartItem(int productId)
{
this.ProductId = productId;
}
public bool Equals(CartItem item)
{
return item.ProductId == this.ProductId;
}
#endregion
}
sample of getters and setters in Java:
public class Employee {
private int empId;
private String name;
private int age;
public Employee(int empId, String name, int age) {
this.empId = empId;
this.name = name;
this.age = age;
}
// getters & setters
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
using your code:
public class Sample {
private int _productId;
public int get_productId() {
return _productId;
}
public void set_productId(int productId) {
_productId = productId;
}
private Product _product = null;
public Product get_product() {
if (_product == null) {
_product = new Product();
}
return _product;
}
public void set_product(Product product) {
_product = product;
}
}
and something more:
public class Product {
String desription;
public String getDesription() {
return desription;
}
public void setDesription(String desription) {
this.desription = desription;
}
}
//this is your hidding delegation getter only in main class (Sample in my samples)
public String getDescription(){
return _product.getDesription();
}
Java getters and setters aren't as easy to use as C#'s. In Java, every getter and setter has to be explicitly defined, rather than using the shorthand you have there.
For example, for your code "public int ProductId", you would need a line defining the variable, in addition two methods (a getter and setter) as follows:
private int _productId;
public void setProductId(int anId)
{
_productId = anId;
}
public int getProductId()
{
return _productId;
}
You'd need to define similar variable declarations and getter/setter methods for each variable you have.

Categories