I'm trying to make a Default Constructor using this code. This is the original without the constructors, can someone please help?:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Chapter_2_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Order_Click(object sender, EventArgs e)
{
string cakeFlavor, frostingFlavor;
int size; //This declasres an integer that represents size
cakeFlavor = Cake.Text; frostingFlavor = Frosting.Text;
size = Convert.ToInt32(Size.Text);
Display.Text = "Cake Flavor: " + cakeFlavor + Environment.NewLine + "Frosting Flavor: " + frostingFlavor + Environment.NewLine + "Cake Size: " + size + " Inches" + Environment.NewLine + "Thank you for shopping" + enter code hereEnvironment.NewLine + "at The Token Bakery!";
//This Displays all the info that the user input
}
}
}
It would be much appreciated. Thank you!
A piece of cake for me doing your homework?
class Cake
{
#region Fields
private string _cakeFlavor;
private string _frostingFlavor;
#endregion
#region Properties
public string CakeFlavor
{
get { return _cakeFlavor; }
set { _cakeFlavor = value; }
}
public string FrostingFlavor
{
get { return _frostingFlavor; }
set { _frostingFlavor = value; }
}
#endregion
#region Constructors
public Cake() : this("Cake flavor not provided.", "Frosting flavor not provided.")
{
}
public Cake(string cakeFlavor, string frostingFlavor)
{
this._cakeFlavor = cakeFlavour;
this._frostingFlavor = frostingFlavor;
}
#endregion
#region Methods
public void PrintCakeFlavors()
{
Console.WriteLine("Cake Flavor: {0}\nFrosting Flavor: {1}", this._cakeFlavor, this._frostingFlavor);
}
#endregion
}
Related
I have a program that is suppose to collect user car data in one method/class and then use it later.
In this case, user types in Year: 2001 Make: Mustang Model: GT (click Make the Car Button). The textboxes are cleared. Then click accelerate. In the DetailLabel it should increment speed by 5 every time the button is pressed. Output: The speed of 2001 Mustang GT is 5 mph. Same for the Brake Button. The speed of 2001 Mustang GT is 0 mph.
Problem: I can't access/use my collected data for brake or accelerate buttons.
Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CarMaker_Tate
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void GetCarData()
{
CarClass myCar = new CarClass();
int CYear;
string CMake = MakeTextBox.Text;
String CModel = ModelTextBox.Text;
if (int.TryParse(YearTextBox.Text, out CYear) && CYear >= 1900 && CYear <= 2022)
{
if (CMake != "")
{
if (CModel != "")
{
myCar.Year = CYear;
myCar.Make = CMake;
myCar.Model = CModel;
}
else
MessageBox.Show("Please enter a car model");
}
else
MessageBox.Show("Please enter a car make");
}
else
MessageBox.Show("Please enter a year between 1900 - 2022");
}
private void MakeButton_Click(object sender, EventArgs e)
{
GetCarData();
YearTextBox.Text = "";
MakeTextBox.Text = "";
ModelTextBox.Text = "";
}
private void AccelerateButton_Click(object sender, EventArgs e)
{
myCar.AccSpeed(5);
DetailLabel.Text = "The speed of " + myCar.Year + myCar.Make + myCar.Model + "is" + myCar.Speed + "mph";
}
private void Summarybutton_Click(object sender, EventArgs e)
{
Summary mySummaryForm = new Summary();
mySummaryForm.Show();
}
private void BrakeButton_Click(object sender, EventArgs e)
{
myCar.DecSpeed(5);
DetailLabel.Text = "The speed of " + myCar.Year + myCar.Make + myCar.Model + "is" + myCar.Speed + "mph";
}
}
}
CarClass.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarMaker_Tate
{
class CarClass
{
int CarYear;
string CarMake;
string CarModel;
public static int CarCount;
public CarClass()
{
// constructor runs everytime a car is created
CarYear = 0;
CarMake = "";
CarModel = "";
CarCount++;
}
public int Year
{
get
{
return CarYear;
}
set
{
CarYear = value;
}
}
public string Make
{
get { return CarMake; }
set { CarMake = value; }
}
public string Model
{
get { return CarModel; }
set { CarModel = value; }
}
public int Speed
{
get { return Speed; }
set { Speed = value; }
}
public void AccSpeed(int speedIncrement)
{
//Add check for speed limit ranges
Speed += speedIncrement;
}
public void DecSpeed(int speedDecrement)
{
//Add check for speed limit ranges
Speed -= speedDecrement;
}
}
}
You should declare myCar as a variable outside the GetCarData() method. Otherwise only the method GetCarData can access it. You can even think to make the variable static:
Like that:
public partial class Form1 : Form
{
// declare myCar for the entire class
private static CarClass myCar;
public Form1()
{
InitializeComponent();
}
private void GetCarData()
{
// init myCar
myCar = new CarClass();
...
}
}
Stream Reader to List, List to array and split the line valu , Make object of the split value and print it a random object in RichTextBox
Try following which is simpler :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Övning_3___Tipsmaskinen2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
FileLoader();
}
public class Bok
{
public string Titel;//Tittle
public string Författare;//Author
public string Boktyp;//TBookTYp
public string ILager;// Bool or InSToridge
public Bok(string titel, string författare, string boktyp, string ilager) // method must have a return typ , funkar enbart om jag skriver in void
{
this.Titel = titel;
this.Författare = författare;
this.Boktyp = boktyp;
this.ILager = ilager;
}
public override string ToString()
{
return "\t Titel : " + Titel + "\t Skribent : " + Författare + " \t Boktyp: " + Boktyp + " \t Finns i lager : " + ILager;
}
}
public List<Bok> FileLoader()
{
List<Bok> bokList = new List<Bok>();
if (File.Exists("texter.txt"))
{
StreamReader reader = new StreamReader("texter.txt", Encoding.Default, true);
string item = "";
while ((item = reader.ReadLine()) != null)
{
string[] vektor = item.Split(new string[] { "###" }, StringSplitOptions.None);
Bok k = new Bok(vektor[0], vektor[1], vektor[2], vektor[3]);
bokList.Add(k);
}
return bokList;
}
return null;
}
}
}
I am new to C#, and I am trying to create a step by step program that will create and display the nodes of a double linked list. I will show what I have so far:
This is the code for the form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Pruebapila2
{
public partial class Form1 : Form
{
DbLinList infoTask;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
infoTask = new DbLinList();
}
private void button1_Click(object sender, EventArgs e)
{
taskToDo vInfo = new taskToDo(int.Parse(textBox1.Text), textBox4.Text, textBox2.Text, textBox5.Text, textBox3.Text);
infoTask.insertAtTheEnd(vInfo);
listBox1.Items.Add("Data Added: "+ vInfo.id + " - " + vInfo.name + " - " + vInfo.length + " - " + vInfo.percentage + " - " + vInfo.programmer);
}
private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
Node n;
n = infoTask.firstNode;
while (n != null)
{
listBox1.Items.Add(Convert.ToString(n.info.id) + "\t" + n.info.name + "\t" + n.info.length);
n = n.Next;
}
}
private void button3_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
Node n;
n = infoTask.firstNode;
while (n != null)
{
if (n.info.id == int.Parse(textBox6.Text))
listBox1.Items.Add(Convert.ToString(n.info.id) + "\t" + n.info.name + "\t" + n.info.length);
n = n.Next;
}
}
}
}
When you click on the first button of the form, it will insert the data into a Node, the node belongs to a double linked list, therefore here is the code for the list.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Pruebapila2
{
class DbLinList
{
public Node firstNode;
public DbLinList()
{
firstNode = null;
}
public DbLinList insertAtTheEnd(taskToDo vTaskToDo)
{
Node newNode;
newNode = new Node(vTaskToDo);
newNode.Next = firstNode;
newNode.Prev = firstNode.Next;
firstNode = newNode;
return this;
}
}
}
This list uses a Node that has a link to the previous node, and a link to the next node of the list. Here is the code for the Node:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Pruebapila2
{
class Node
{
public taskToDo info;
public Node Next;
public Node Prev;
public Node(taskToDo vInfo)
{
info = vInfo;
Next = null;
Prev = null;
}
}
}
The node is reusable because it can contain any type of info, and even several parts of information, but in this case this node will contain information about a task that a programmer has to make, for that reason I created a tasks.cs file, which will contain the information that we need to store on the list. Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Pruebapila2
{
class taskToDo
{
public int id;
public string name;
public string length;
public string percentage;
public string programmer;
public taskToDo(int vID, String vName, String vLength, String vPercentage, String vProgrammer)
{
id = vID;
name = vName;
length = vLength;
percentage = vPercentage;
programmer = vProgrammer;
}
}
}
The code shows no errors and no warnings, when executed, it displays the ERROR: "An unhandled exception of type 'System.NullReferenceException' occurred in DoubleLinkedTest.exe." But I have no clue as to why is this error appearing.
The logic here is the following: The button sends the data to the list, the list creates a new node, the node creates a new task, and the info is stored in the node.
Can anyone tell me what is wrong with the code, why is not working?. The functions of the button number 2 and 3 are not in question at the moment.
This is how the form looks like:
Thank you very much for your Help on this!.
I'm learning C#, trying to get to grips with accessors at the moment.
I'm going nuts looking at this, I have no idea what I've done wrong:
class BankAccount
{
// *PROPERTIES*
private int _initialDeposit = 0;
// **ACCESSORS**
public int SavingsAccount
{
set
{
_initialDeposit = value;
}
get
{
return _initialDeposit;
}
}
}
The Form looks like this:
public partial class BankForm : Form
{
private BankAccount _myAccount;
public BankForm()
{
InitializeComponent();
_myAccount = new BankAccount();
}
private void initialDepositButton_Click(object sender, EventArgs e)
{
_myAccount.SavingsAccount = Convert.ToInt32(initialDepositTextBox.Text);
bankAccountListBox.Text = "Account opened with initial Deposit " + initialDepositTextBox.Text;
}
}
But I get this error:
Property or indexer must have at least one accessor
I'm not getting any errors. Move location of private BankAccount _myAccount;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace BankForm
{
public partial class BankForm : Form
{
public BankForm()
{
InitializeComponent();
_myAccount = new BankAccount();
}
private BankAccount _myAccount;
private void initialDepositButton_Click(object sender, EventArgs e)
{
_myAccount.SavingsAccount = Convert.ToInt32(initialDepositTextBox.Text);
bankAccountListBox.Text = "Account opened with initial Deposit " + initialDepositTextBox.Text;
}
}
class BankAccount
{
// *PROPERTIES*
private int _initialDeposit = 0;
// **ACCESSORS**
public int SavingsAccount
{
set
{
_initialDeposit = value;
}
get
{
return _initialDeposit;
}
}
}
}
I am getting an error on lines 44 and 50.
It says:
No overload for method 'GetCarData' takes 1 arguments
Alright, so my task is to create an application that displays 3 main features: year, make, and speed of a car. The year and make are inputted with textboxes and the speed starts at 0.
There is an accelerate button which is supposed to add 5 to the speed every time it is pressed and a brake button which decreases the speed by 5 every time it is pressed.
I am having trouble using the class and form together to display the results. I need to display in a messagebox the make, year, and speed. I have been sitting here for hours and I am getting nowhere.
Any and all help is much appreciated. I have never worked with classes before.
Here is the form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Car_Class_BBrantley
{
public partial class Form1 : Form
{
private Car myCar;
public Form1()
{
myCar = new Car();
InitializeComponent();
}
private void GetCarData()
{
try {
myCar.Make = txtMake.Text;
myCar.Year = int.Parse(txtModel.Text);
myCar.Speed = 0;
}
catch (Exception ex)
{
MessageBox.Show(string.Concat("Must enter a valid make and year model for the car. ", ex.Message, "\r\n", ex.StackTrace));
}
}
private void btnAcc_Click(object sender, EventArgs e)
{
GetCarData();
myCar.AccSpeed(5);
MessageBox.Show(" Your car is a " + myCar.Year + myCar.Make + " and it is traveling " + myCar.Speed + " mph. ");
}
private void btnBrake_Click(object sender, EventArgs e)
{
GetCarData();
myCar.DecSpeed(5);
MessageBox.Show(" Your car is a " + myCar.Year + myCar.Make + " and it is traveling " + myCar.Speed + " mph. ");
}
}
}
If you would like to see the class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Car_Class_BBrantley
{
class Car
{
private int year;
private string make;
private int speed;
public Car()
{
this.year = 1994;
this.make = "Ford";
this.speed = 0;
}
public Car(string make, int year, int speed)
{
this.year = year;
this.make = make;
this.speed = speed;
}
public string Make
{
get { return make; }
set { make = value; }
}
public int Year
{
get { return Year; }
set { Year = value; }
}
public int Speed
{
get { return speed; }
set { speed = value; }
}
public void AccSpeed(int speedIncrement)
{
//Add check for speed limit ranges
Speed += speedIncrement;
}
public void DecSpeed(int speedDecrement)
{
//Add check for speed limit ranges
Speed -= speedDecrement;
}
}
}
If the error says
No overload for method 'GetCarData' takes 1 arguments
then look at the calls you make to GetCarData and check that they don't try to pass a parameter (argument) to it. It's declared as
private void GetCarData()
which is good, and the two calls (lines 44 and 50) are consistent with that:
GetCarData();
so make sure that the code on your computer matches what's here, save it, and re-compile.