C
C#•2mo ago
strikeouts27

Understanding Equals() method

Is there a reason my version of Equals is wrong?
/*
Exercise 10-3A

Create an application named JobDemo that declares and uses Job objects. The Job class holds job information for a home repair service. The class has five properties that include:

JobNumber - The job number - int
Customer - The customer name - string
Description - The job description - string
Hours - The estimated hours - double
price - The price for the job

Create a constructor that requires parameters for all the data except price. Follow the order and data type above. Include auto-implemented properties for the job number, customer name, and job description, but not for hours or price; the price field value is calculated as estimated hours times $45.00 ($45.00 / hour) whenever the hours value is set.

Also create the following methods for theJob class:

An Equals() method that determines two Jobs are equal if they have the same job number
A GetHashCode() method that returns the job number
A ToString() method that returns a string containing all job information in the following format:

Job 111 Smith exterior paint 20 hours @$45.00 per hour. Total price is $900.00

The JobDemo application declares a few Job objects, sets their values, and demonstrates that all the methods work as expected.

info> In order to prepend the $ to currency values, the program will need to use the CultureInfo.GetCultureInfo method. In order to do this, include the statement using System.Globalization; at the top of your program and format the output statements as follows: WriteLine("This is an example: {0}", value.ToString("C", CultureInfo.GetCultureInfo("en-US")));

*/
using System;
using static System.Console;
using System.Globalization;

namespace JobDemo
{
class JobDemo
{
static void Main()
{
// public Job jobOne = new Job(jobNumber, customerName, jobDescription, jobHours);

// My attempt at making objects
public Job jobOne = new Job(1, "Cherry Painting", "Paint exterior of building", 12);
public Job jobTwo = new Job(1, "Apple Painting", "Paint the interior of the building", 12);
public Job jobThree = new Job(2, "Banna Painting", "Paint the Highschool Baseball Team Building", 12);
}

// Job Class
public class Job
{
public double price = hours * 45.00;
public int jobNumber { get; set; }
public string customer { get; set; }
public string description { get; set; }

private double hours;
public double Hours
{
get { return hours; }
set
{
hours = value;
price = hours * 45.0;
}
}

private double price;
public double Price => price;


// An Equals() method that determines two Jobs are equal if they have the same job number
public Equals()
{
if jobOne.jobNumber.Equals(jobTwo.jobNumber);
}
// A GetHashCode() method that returns the job number
// A ToString() method that returns a string containing all job information in the following format:



// RushJob Class s
public class RushJob : Job
{
private double premiumFee = 150.00;


// preimum calculation
}

}
}
/*
Exercise 10-3A

Create an application named JobDemo that declares and uses Job objects. The Job class holds job information for a home repair service. The class has five properties that include:

JobNumber - The job number - int
Customer - The customer name - string
Description - The job description - string
Hours - The estimated hours - double
price - The price for the job

Create a constructor that requires parameters for all the data except price. Follow the order and data type above. Include auto-implemented properties for the job number, customer name, and job description, but not for hours or price; the price field value is calculated as estimated hours times $45.00 ($45.00 / hour) whenever the hours value is set.

Also create the following methods for theJob class:

An Equals() method that determines two Jobs are equal if they have the same job number
A GetHashCode() method that returns the job number
A ToString() method that returns a string containing all job information in the following format:

Job 111 Smith exterior paint 20 hours @$45.00 per hour. Total price is $900.00

The JobDemo application declares a few Job objects, sets their values, and demonstrates that all the methods work as expected.

info> In order to prepend the $ to currency values, the program will need to use the CultureInfo.GetCultureInfo method. In order to do this, include the statement using System.Globalization; at the top of your program and format the output statements as follows: WriteLine("This is an example: {0}", value.ToString("C", CultureInfo.GetCultureInfo("en-US")));

*/
using System;
using static System.Console;
using System.Globalization;

namespace JobDemo
{
class JobDemo
{
static void Main()
{
// public Job jobOne = new Job(jobNumber, customerName, jobDescription, jobHours);

// My attempt at making objects
public Job jobOne = new Job(1, "Cherry Painting", "Paint exterior of building", 12);
public Job jobTwo = new Job(1, "Apple Painting", "Paint the interior of the building", 12);
public Job jobThree = new Job(2, "Banna Painting", "Paint the Highschool Baseball Team Building", 12);
}

// Job Class
public class Job
{
public double price = hours * 45.00;
public int jobNumber { get; set; }
public string customer { get; set; }
public string description { get; set; }

private double hours;
public double Hours
{
get { return hours; }
set
{
hours = value;
price = hours * 45.0;
}
}

private double price;
public double Price => price;


// An Equals() method that determines two Jobs are equal if they have the same job number
public Equals()
{
if jobOne.jobNumber.Equals(jobTwo.jobNumber);
}
// A GetHashCode() method that returns the job number
// A ToString() method that returns a string containing all job information in the following format:



// RushJob Class s
public class RushJob : Job
{
private double premiumFee = 150.00;


// preimum calculation
}

}
}
88 Replies
mtreit
mtreit•2mo ago
I mean, it has an obvious syntax error. So presumably this code does not compile. Revisit the syntax for an if statement.
strikeouts27
strikeouts27OP•2mo ago
Okay I fixed the obvious issue but have discovered another. (Thank you) I want my Equals method to compare job numbers of two different jobs. I recognize that in order to have a comparison the user must have a say in what jobs will be compared to what. I am believing that I will need dto utlize input methods like console. readline. What values would they need to grab for comparison? Maybe have them enter in job names?
/*
Exercise 10-3A

Create an application named JobDemo that declares and uses Job objects. The Job class holds job information for a home repair service. The class has five properties that include:

JobNumber - The job number - int
Customer - The customer name - string
Description - The job description - string
Hours - The estimated hours - double
price - The price for the job

Also create the following methods for theJob class:

An Equals() method that determines two Jobs are equal if they have the
A GetHashCode() method that returns the job number
A ToString() method that returns a string containing all job information in the following format:

Job 111 Smith exterior paint 20 hours @$45.00 per hour. Total price is $900.00

The JobDemo application declares a few Job objects, sets their values, and demonstrates that all the methods work as expected.

info> In order to prepend the $ to currency values, the program will need to use the CultureInfo.GetCultureInfo method. In order to do this, include the statement using System.Globalization; at the top of your program and format the output statements as follows: WriteLine("This is an example: {0}", value.ToString("C", CultureInfo.GetCultureInfo("en-US")));

*/
using System;
using static System.Console;
using System.Globalization;

namespace JobDemo
{
class JobDemo
{
static void Main()
{
// public Job jobOne = new Job(jobNumber, customerName, jobDescription, jobHours);

// My attempt at making objects
public Job jobOne = new Job(1, "Cherry Painting", "Paint exterior of building", 12);
public Job jobTwo = new Job(1, "Apple Painting", "Paint the interior of the building", 12);
public Job jobThree = new Job(2, "Banna Painting", "Paint the Highschool Baseball Team Building", 12);
}

// Job Class
public class Job
{
public double price = hours * 45.00;
public int jobNumber { get; set; }
public string customer { get; set; }
public string description { get; set; }

private double hours;
public double Hours
{
get { return hours; }
set
{
hours = value;
price = hours * 45.0;
}
}

private double price;
public double Price => price;


// An Equals() method that determines two Jobs are equal if they have the same job number
public string IsSameJob()
{

if (jobOne.jobNumber.Equals(jobTwo.jobNumber))
{
return "These job orders have the same job number!";
}
return "These job orders have different job numbers!";
}

// A GetHashCode() method that returns the job number

public GetHashCode()
{

}

// A ToString() method that returns a string containing all job information in the following format:



// RushJob Class s
public class RushJob : Job
{
private double premiumFee = 150.00;


// preimum calculation
}

}
}
/*
Exercise 10-3A

Create an application named JobDemo that declares and uses Job objects. The Job class holds job information for a home repair service. The class has five properties that include:

JobNumber - The job number - int
Customer - The customer name - string
Description - The job description - string
Hours - The estimated hours - double
price - The price for the job

Also create the following methods for theJob class:

An Equals() method that determines two Jobs are equal if they have the
A GetHashCode() method that returns the job number
A ToString() method that returns a string containing all job information in the following format:

Job 111 Smith exterior paint 20 hours @$45.00 per hour. Total price is $900.00

The JobDemo application declares a few Job objects, sets their values, and demonstrates that all the methods work as expected.

info> In order to prepend the $ to currency values, the program will need to use the CultureInfo.GetCultureInfo method. In order to do this, include the statement using System.Globalization; at the top of your program and format the output statements as follows: WriteLine("This is an example: {0}", value.ToString("C", CultureInfo.GetCultureInfo("en-US")));

*/
using System;
using static System.Console;
using System.Globalization;

namespace JobDemo
{
class JobDemo
{
static void Main()
{
// public Job jobOne = new Job(jobNumber, customerName, jobDescription, jobHours);

// My attempt at making objects
public Job jobOne = new Job(1, "Cherry Painting", "Paint exterior of building", 12);
public Job jobTwo = new Job(1, "Apple Painting", "Paint the interior of the building", 12);
public Job jobThree = new Job(2, "Banna Painting", "Paint the Highschool Baseball Team Building", 12);
}

// Job Class
public class Job
{
public double price = hours * 45.00;
public int jobNumber { get; set; }
public string customer { get; set; }
public string description { get; set; }

private double hours;
public double Hours
{
get { return hours; }
set
{
hours = value;
price = hours * 45.0;
}
}

private double price;
public double Price => price;


// An Equals() method that determines two Jobs are equal if they have the same job number
public string IsSameJob()
{

if (jobOne.jobNumber.Equals(jobTwo.jobNumber))
{
return "These job orders have the same job number!";
}
return "These job orders have different job numbers!";
}

// A GetHashCode() method that returns the job number

public GetHashCode()
{

}

// A ToString() method that returns a string containing all job information in the following format:



// RushJob Class s
public class RushJob : Job
{
private double premiumFee = 150.00;


// preimum calculation
}

}
}
mtreit
mtreit•2mo ago
The assignment doesn't say you need to create jobs from user input. You might be over-thinking it... Also I find this assignment's description rather cursed.
strikeouts27
strikeouts27OP•2mo ago
So maybe the job orders can be created already by the user, and if they want to know if the job has the same job number as the other than thats when they use IsSameJob
mtreit
mtreit•2mo ago
Comparing two jobs as equal when they have the same identifiers but wildly different other values is not what we do in the real world. But that seems to be what the assignment wants.
strikeouts27
strikeouts27OP•2mo ago
I think they just want me to demonstrate the Equals() method in some way. So in this case maybe have the job names be passed in and compared via input? or is there a simpler way?
mtreit
mtreit•2mo ago
Just hard-code the values in the demo program if I understand the assignment correctly.
strikeouts27
strikeouts27OP•2mo ago
Like this? public string IsSameJob() { Console.WriteLine("Please ") if (jobOne.jobNumber.Equals(jobTwo.jobNumber)) { return "These job orders have the same job number!"; } return "These job orders have different job numbers!"; }
mtreit
mtreit•2mo ago
Your code has a bunch of remaining invalid-syntax issues though.
strikeouts27
strikeouts27OP•2mo ago
sigh. agreed. one fire at a time.
mtreit
mtreit•2mo ago
No, you would not generally return strings like that. If you want to say if two things are equal, you would normally return a boolean value: true or false. That's what the Equals method does.
strikeouts27
strikeouts27OP•2mo ago
// An Equals() method that determines two Jobs are equal if they have the same job number public string IsSameJob() { if (jobOne.jobNumber.Equals(jobTwo.jobNumber)) { return True; } return False; }
mtreit
mtreit•2mo ago
Is that method called Equals? Also, you want to use the override keyword to override the default Equals method on the base object type. Hopefully they taught you that part. Otherwise this assignment is kind of garbage.
strikeouts27
strikeouts27OP•2mo ago
thats unreadable nvm
mtreit
mtreit•2mo ago
It was fine, I was reading it 😄 But looks like they are teaching you how it works which is good.
strikeouts27
strikeouts27OP•2mo ago
so I have to use equals() public override string Equals() { if (jobOne.jobNumber.Equals(jobTwo.jobNumber)) { return True; } return False; } Alright I think that is correct on to the next problem
mtreit
mtreit•2mo ago
Uh... No. That's not close to correct.
strikeouts27
strikeouts27OP•2mo ago
It has public override its named equals. it returns true and false
mtreit
mtreit•2mo ago
You have way too much broken code at once to even see all of the problems. You need to work in smaller pieces I think. And make sure the code compiles.
strikeouts27
strikeouts27OP•2mo ago
I will go work on it. but i will say this. if i knew what i was doing, i would write it the right way. i only know, what i actually know
mtreit
mtreit•2mo ago
Let me break this down into a simpler sub-problem that you can maybe use to help you solve your actual assignment. Consider the following program:
var c1 = new MyAwesomeClass
{
SomeCoolInteger = 123,
SomeCoolString = "abc"
};

var c2 = new MyAwesomeClass
{
SomeCoolInteger = 123,
SomeCoolString = "abc"
};

var c3 = new MyAwesomeClass
{
SomeCoolInteger = 39339,
SomeCoolString = "HELLO"
};

Console.WriteLine(c1.Equals(c2)); // Should print true
Console.WriteLine(c2.Equals(c3)); // Should print false

public class MyAwesomeClass
{
public string SomeCoolString
{
get;
set;
}

public int SomeCoolInteger
{
get;
set;
}

// Implement an Equals method such that this program compiles
// and prints the correct results.
}
var c1 = new MyAwesomeClass
{
SomeCoolInteger = 123,
SomeCoolString = "abc"
};

var c2 = new MyAwesomeClass
{
SomeCoolInteger = 123,
SomeCoolString = "abc"
};

var c3 = new MyAwesomeClass
{
SomeCoolInteger = 39339,
SomeCoolString = "HELLO"
};

Console.WriteLine(c1.Equals(c2)); // Should print true
Console.WriteLine(c2.Equals(c3)); // Should print false

public class MyAwesomeClass
{
public string SomeCoolString
{
get;
set;
}

public int SomeCoolInteger
{
get;
set;
}

// Implement an Equals method such that this program compiles
// and prints the correct results.
}
This is a complete program. It currently prints:
False
False
False
False
If you correctly implement an Equals method to compare two instances of the class for value-equality (meaning true if the two properties are identical and false otherwise) then it should print:
True
False
True
False
If you can make the above program (which is simpler subproblem of your assignment) I think you will be able to make some progres...
strikeouts27
strikeouts27OP•2mo ago
I appreciate your advice. I will try to do that right now.
mtreit
mtreit•2mo ago
Note that overriding Equals is kind of tricky because the input is of type object but you need to turn that generic object into something of type MyAwesomeClass (in this case.) Which hopefully your book explains and gives examples of.
strikeouts27
strikeouts27OP•2mo ago
using System;
using static System.Console;
using System.Globalization;

namespace JobDemo
{
class JobDemo
{
static void Main()
{
// public Job jobOne = new Job(jobNumber, customerName, jobDescription, jobHours);

// My attempt at making objects
var jobOne = new Job(1, "Cherry Painting", "Paint exterior of building", 12);
var2 jobTwo = new Job(1, "Apple Painting", "Paint the interior of the building", 12);
var3 jobThree = new Job(2, "Banna Painting", "Paint the Highschool Baseball Team Building", 12);
}

// Job Class
public class Job
{
public double price = hours * 45.00;
private double hours;
public int jobNumber { get; set; }
public string customer { get; set; }
public string description { get; set; }


// An Equals() method that determines two Jobs are equal if they have the same job number
public override string Equals()
{
if (jobOne.jobNumber.Equals(jobTwo.jobNumber))
{
return True;
}
return False;
}

}
}
using System;
using static System.Console;
using System.Globalization;

namespace JobDemo
{
class JobDemo
{
static void Main()
{
// public Job jobOne = new Job(jobNumber, customerName, jobDescription, jobHours);

// My attempt at making objects
var jobOne = new Job(1, "Cherry Painting", "Paint exterior of building", 12);
var2 jobTwo = new Job(1, "Apple Painting", "Paint the interior of the building", 12);
var3 jobThree = new Job(2, "Banna Painting", "Paint the Highschool Baseball Team Building", 12);
}

// Job Class
public class Job
{
public double price = hours * 45.00;
private double hours;
public int jobNumber { get; set; }
public string customer { get; set; }
public string description { get; set; }


// An Equals() method that determines two Jobs are equal if they have the same job number
public override string Equals()
{
if (jobOne.jobNumber.Equals(jobTwo.jobNumber))
{
return True;
}
return False;
}

}
}
I need help understanding. First you say, we don't need inputs for job numbers. than you say "overriding Equals is kind of tricky because the input is of type object but you need to turn that generic object into something of type MyAwesomeClass" So are we using inputs or not?
mtreit
mtreit•2mo ago
I meant you don't need to use something like ReadLine() to read input into the program when it starts up. However, talking about input parameters to a method is an entirely different thing. You understand method signatures, return values and input parameters?
strikeouts27
strikeouts27OP•2mo ago
method signatures are the first line you write when you create a method. Return values are the things that are returned by the return keyword input parameters are the things you need to activate a method or an object.
mtreit
mtreit•2mo ago
public int AddTwoNumbers(int a, int b)
{
return a + b;
}
public int AddTwoNumbers(int a, int b)
{
return a + b;
}
Yes, here the return type is int and the method takes two input parameters: a (an int) and b (also an int). For the Equals method, the signatures looks like this:
public override bool Equals(object obj)
public override bool Equals(object obj)
(or possibly this is you have nullable reference types enabled):
public override bool Equals(object? obj)
public override bool Equals(object? obj)
You know what. Re-reading your assignment, I'm not sure they want you to override the base Equals method.
strikeouts27
strikeouts27OP•2mo ago
Like This?
/*
Exercise 10-3A

Create an application named JobDemo that declares and uses Job objects. The Job class holds job information for a home repair service. The class has five properties that include:

JobNumber - The job number - int
Customer - The customer name - string
Description - The job description - string
Hours - The estimated hours - double
price - The price for the job

Create a constructor that requires parameters for all the data except price. Follow the order and data type above. Include auto-implemented properties for the job number, customer name, and job description, but not for hours or price; the price field value is calculated as estimated hours times $45.00 ($45.00 / hour) whenever the hours value is set.

Also create the following methods for theJob class:

An Equals() method that determines two Jobs are equal if they have the same job number
A GetHashCode() method that returns the job number
A ToString() method that returns a string containing all job information in the following format:

Job 111 Smith exterior paint 20 hours @$45.00 per hour. Total price is $900.00

The JobDemo application declares a few Job objects, sets their values, and demonstrates that all the methods work as expected.

info> In order to prepend the $ to currency values, the program will need to use the CultureInfo.GetCultureInfo method. In order to do this, include the statement using System.Globalization; at the top of your program and format the output statements as follows: WriteLine("This is an example: {0}", value.ToString("C", CultureInfo.GetCultureInfo("en-US")));

*/
using System;
using static System.Console;
using System.Globalization;
using System.Security.Cryptography;

namespace JobDemo
{
class JobDemo
{
static void Main()
{
// public Job jobOne = new Job(jobNumber, customerName, jobDescription, jobHours);

// My attempt at making objects
var jobOne = new Job(1, "Cherry Painting", "Paint exterior of building", 12);
var2 jobTwo = new Job(1, "Apple Painting", "Paint the interior of the building", 12);
var3 jobThree = new Job(2, "Banna Painting", "Paint the Highschool Baseball Team Building", 12);
Console.WriteLine(jobOne.Equals(jobTwo));
Console.WriteLine(jobTwo).Equals(jobThree);
}


// Job Class
public class Job
{
public double price = hours * 45.00;
private double hours;
public int jobNumber { get; set; }
public string customer { get; set; }
public string description { get; set; }


// An Equals() method that determines two Jobs are equal if they have the same job number
public override string Equals()
{
if (jobOne.jobNumber.Equals(jobTwo.jobNumber))
{
return True;
}
return False;
}

}
}
/*
Exercise 10-3A

Create an application named JobDemo that declares and uses Job objects. The Job class holds job information for a home repair service. The class has five properties that include:

JobNumber - The job number - int
Customer - The customer name - string
Description - The job description - string
Hours - The estimated hours - double
price - The price for the job

Create a constructor that requires parameters for all the data except price. Follow the order and data type above. Include auto-implemented properties for the job number, customer name, and job description, but not for hours or price; the price field value is calculated as estimated hours times $45.00 ($45.00 / hour) whenever the hours value is set.

Also create the following methods for theJob class:

An Equals() method that determines two Jobs are equal if they have the same job number
A GetHashCode() method that returns the job number
A ToString() method that returns a string containing all job information in the following format:

Job 111 Smith exterior paint 20 hours @$45.00 per hour. Total price is $900.00

The JobDemo application declares a few Job objects, sets their values, and demonstrates that all the methods work as expected.

info> In order to prepend the $ to currency values, the program will need to use the CultureInfo.GetCultureInfo method. In order to do this, include the statement using System.Globalization; at the top of your program and format the output statements as follows: WriteLine("This is an example: {0}", value.ToString("C", CultureInfo.GetCultureInfo("en-US")));

*/
using System;
using static System.Console;
using System.Globalization;
using System.Security.Cryptography;

namespace JobDemo
{
class JobDemo
{
static void Main()
{
// public Job jobOne = new Job(jobNumber, customerName, jobDescription, jobHours);

// My attempt at making objects
var jobOne = new Job(1, "Cherry Painting", "Paint exterior of building", 12);
var2 jobTwo = new Job(1, "Apple Painting", "Paint the interior of the building", 12);
var3 jobThree = new Job(2, "Banna Painting", "Paint the Highschool Baseball Team Building", 12);
Console.WriteLine(jobOne.Equals(jobTwo));
Console.WriteLine(jobTwo).Equals(jobThree);
}


// Job Class
public class Job
{
public double price = hours * 45.00;
private double hours;
public int jobNumber { get; set; }
public string customer { get; set; }
public string description { get; set; }


// An Equals() method that determines two Jobs are equal if they have the same job number
public override string Equals()
{
if (jobOne.jobNumber.Equals(jobTwo.jobNumber))
{
return True;
}
return False;
}

}
}
mtreit
mtreit•2mo ago
That might be getting too complicated.
strikeouts27
strikeouts27OP•2mo ago
Like this?
mtreit
mtreit•2mo ago
public override string Equals()
{
if (jobOne.jobNumber.Equals(jobTwo.jobNumber))
{
return True;
}
return False;
}
public override string Equals()
{
if (jobOne.jobNumber.Equals(jobTwo.jobNumber))
{
return True;
}
return False;
}
So, there is a lot wrong with this.
strikeouts27
strikeouts27OP•2mo ago
were trying to use a method right? dot notation?
mtreit
mtreit•2mo ago
Again, forget about using override I think I was making things harder than necessary. And Equials should return bool not string.
strikeouts27
strikeouts27OP•2mo ago
good catch yes the type needs to be bool. updated.
mtreit
mtreit•2mo ago
And inside your Equals method, instances like jobOne and jobTwo don't exist. The entire point is that the method is part of the definition of the class. You can have thousands of instances. The Equals method needs to work without any knowledge about instances of itself (the class).
strikeouts27
strikeouts27OP•2mo ago
that makes sense. this is a method for every object not just those two.
mtreit
mtreit•2mo ago
It should look something like this:
public bool Equals(Job other)
{
// Implement the method by comparing the current instance's
// properties (per the assignment, just the Id property)
// with the value of the property of the input parameter
}
public bool Equals(Job other)
{
// Implement the method by comparing the current instance's
// properties (per the assignment, just the Id property)
// with the value of the property of the input parameter
}
strikeouts27
strikeouts27OP•2mo ago
maybe an input parame ter? current instance. the this keyword?
mtreit
mtreit•2mo ago
Using the this keyword is optional, but yes. Inside the instance, this.Id and Id are the same thing.
strikeouts27
strikeouts27OP•2mo ago
What I have so far. So the user would need to input a job for this to work. How would I specify/ know the job being passed in? Who knows what the user wants without input methods?
public bool Equals(Job 2)
{
if (this.jobNumber == Job.jobNumber))
{
return True;
}
return False;
}
public bool Equals(Job 2)
{
if (this.jobNumber == Job.jobNumber))
{
return True;
}
return False;
}
maybe thats not my problem i just need to specify another job needs to be passed in.
mtreit
mtreit•2mo ago
For one thing, true and false are all lowercase in C#. So fix that. And you don't seem to quite understand the syntax for input parameters.
strikeouts27
strikeouts27OP•2mo ago
int number string "Texas" but for job objects we specify the Object name right? since an object becomes a type in c#?
mtreit
mtreit•2mo ago
The first part is the type, in this case Job. The second is the name of the input parameter. You can name this whatever you like, but it can't start with a number. So in your code above Job 2 is not valid. But you need to be typing this code in and trying to compile it and fixing the errors. It feels like you are not seeing that obvious errors that the compiler will tell you.
strikeouts27
strikeouts27OP•2mo ago
okay i will do that.
mtreit
mtreit•2mo ago
In your case the "user" is the code calling the method, which will be in the demo program.
mtreit
mtreit•2mo ago
In the demo program, the values are just hard-coded:
No description
mtreit
mtreit•2mo ago
But the Equals method doesn't know that or care. It just uses whatever instances was passed in.
mtreit
mtreit•2mo ago
No description
mtreit
mtreit•2mo ago
Inside the implementation of Equals, does it need to know where jobTwo came from? No it does not.
strikeouts27
strikeouts27OP•2mo ago
/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.cs(41,30): error CS1729: 'JobDemo.Job' does not contain a constructor that takes 4 arguments [/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.csproj] Running this says I need to re create my constructor.
mtreit
mtreit•2mo ago
Note that the calling code in the Main method names the job variable it is passing to the method jobTwo. But in the implementation of the method, the implementation can call the input parameter anything it wants. The name used inside the method and the name used outside the method are completely indenpendent. Yes, you have not implemented a constructor yet.
strikeouts27
strikeouts27OP•2mo ago
/*
Exercise 10-3A

Create an application named JobDemo that declares and uses Job objects. The Job class holds job information for a home repair service. The class has five properties that include:

JobNumber - The job number - int
Customer - The customer name - string
Description - The job description - string
Hours - The estimated hours - double
price - The price for the job

Create a constructor that requires parameters for all the data except price. Follow the order and data type above. Include auto-implemented properties for the job number, customer name, and job description, but not for hours or price; the price field value is calculated as estimated hours times $45.00 ($45.00 / hour) whenever the hours value is set.

Also create the following methods for theJob class:

An Equals() method that determines two Jobs are equal if they have the same job number
A GetHashCode() method that returns the job number
A ToString() method that returns a string containing all job information in the following format:

Job 111 Smith exterior paint 20 hours @$45.00 per hour. Total price is $900.00

The JobDemo application declares a few Job objects, sets their values, and demonstrates that all the methods work as expected.

info> In order to prepend the $ to currency values, the program will need to use the CultureInfo.GetCultureInfo method. In order to do this, include the statement using System.Globalization; at the top of your program and format the output statements as follows: WriteLine("This is an example: {0}", value.ToString("C", CultureInfo.GetCultureInfo("en-US")));

*/
using System;
using static System.Console;
using System.Globalization;
using System.Security.Cryptography;

namespace JobDemo
{
class JobDemo
{
static void Main()
{
// public Job jobOne = new Job(jobNumber, customerName, jobDescription, jobHours);

// My attempt at making objects
var jobOne = new Job(1, "Cherry Painting", "Paint exterior of building", 12);
var2 jobTwo = new Job(1, "Apple Painting", "Paint the interior of the building", 12);
var3 jobThree = new Job(2, "Banna Painting", "Paint the Highschool Baseball Team Building", 12);
Console.WriteLine(jobOne.Equals(jobTwo));
Console.WriteLine(jobTwo).Equals(jobThree);
}


// Job Class
public class Job
{
public double price = hours * 45.00;
private double hours;
public int jobNumber { get; set; }
public string customer { get; set; }
public string description { get; set; }


// An Equals() method that determines two Jobs are equal if they have the same job number
public bool Equals(Job two)
{
if (this.jobNumber == Job.jobNumber)
{
return true;
}
return false;
}

public Job(int jobNumber, string customer, string description, double hours)
{
jobNumber = jobNumber;
Customer = customer;
description = description;
Hours = hours;
}
}

}
}
/*
Exercise 10-3A

Create an application named JobDemo that declares and uses Job objects. The Job class holds job information for a home repair service. The class has five properties that include:

JobNumber - The job number - int
Customer - The customer name - string
Description - The job description - string
Hours - The estimated hours - double
price - The price for the job

Create a constructor that requires parameters for all the data except price. Follow the order and data type above. Include auto-implemented properties for the job number, customer name, and job description, but not for hours or price; the price field value is calculated as estimated hours times $45.00 ($45.00 / hour) whenever the hours value is set.

Also create the following methods for theJob class:

An Equals() method that determines two Jobs are equal if they have the same job number
A GetHashCode() method that returns the job number
A ToString() method that returns a string containing all job information in the following format:

Job 111 Smith exterior paint 20 hours @$45.00 per hour. Total price is $900.00

The JobDemo application declares a few Job objects, sets their values, and demonstrates that all the methods work as expected.

info> In order to prepend the $ to currency values, the program will need to use the CultureInfo.GetCultureInfo method. In order to do this, include the statement using System.Globalization; at the top of your program and format the output statements as follows: WriteLine("This is an example: {0}", value.ToString("C", CultureInfo.GetCultureInfo("en-US")));

*/
using System;
using static System.Console;
using System.Globalization;
using System.Security.Cryptography;

namespace JobDemo
{
class JobDemo
{
static void Main()
{
// public Job jobOne = new Job(jobNumber, customerName, jobDescription, jobHours);

// My attempt at making objects
var jobOne = new Job(1, "Cherry Painting", "Paint exterior of building", 12);
var2 jobTwo = new Job(1, "Apple Painting", "Paint the interior of the building", 12);
var3 jobThree = new Job(2, "Banna Painting", "Paint the Highschool Baseball Team Building", 12);
Console.WriteLine(jobOne.Equals(jobTwo));
Console.WriteLine(jobTwo).Equals(jobThree);
}


// Job Class
public class Job
{
public double price = hours * 45.00;
private double hours;
public int jobNumber { get; set; }
public string customer { get; set; }
public string description { get; set; }


// An Equals() method that determines two Jobs are equal if they have the same job number
public bool Equals(Job two)
{
if (this.jobNumber == Job.jobNumber)
{
return true;
}
return false;
}

public Job(int jobNumber, string customer, string description, double hours)
{
jobNumber = jobNumber;
Customer = customer;
description = description;
Hours = hours;
}
}

}
}
mtreit
mtreit•2mo ago
Remember that C# is case sensitive. You don't have a property or field called Hours with a capital H.
strikeouts27
strikeouts27OP•2mo ago
running code again /workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.cs(42,13): error CS0246: The type or namespace name 'var2' could not be found (are you missing a using directive or an assembly reference?) [/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.csproj] that was AI I tried making backing fields and get sets but C# fought me.
mtreit
mtreit•2mo ago
You shouldn't be using AI for an assignment like this...
strikeouts27
strikeouts27OP•2mo ago
I have discovered that very painfully. I just don't know what to do when stuck. I search the book for answers but they only give me generic examples. I have to figure out how to make the concepts work for me in my situation.
mtreit
mtreit•2mo ago
And you are misunderstanding the var keyword - it just lets you omit the type from the left side of the assignment and let it be inferred from the right side of the assignment.
var first = new A();
var second = new A();
var first = new A();
var second = new A();
strikeouts27
strikeouts27OP•2mo ago
oh I thought var held variables like it does in javascript. my bad.
mtreit
mtreit•2mo ago
No, here first and second are the variables. var is just a keyword that lets you not have to type the entire Type name out when it's obvious from the right-hand side of the equals. It's just a nice feature so you don't have to type stuff like this:
Dictionary<string, int> dict = new Dictionary<string, int>();
Dictionary<string, int> dict = new Dictionary<string, int>();
You can just write:
var dict = new Dictionary<string, int>();
var dict = new Dictionary<string, int>();
Which is more readable.
strikeouts27
strikeouts27OP•2mo ago
what was wrong with saying Job jobOne am I not specifying that it is of the job object?
mtreit
mtreit•2mo ago
These are identical:
Job jobOne = new Job(...);
var jobOne = new Job(...);
Job jobOne = new Job(...);
var jobOne = new Job(...);
Up to you which to use. As type names get longer, var can be nice.
strikeouts27
strikeouts27OP•2mo ago
/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.cs(45,38): error CS0023: Operator '.' cannot be applied to operand of type 'void' [/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.csproj]
/*
Exercise 10-3A

Create an application named JobDemo that declares and uses Job objects. The Job class holds job information for a home repair service. The class has five properties that include:

JobNumber - The job number - int
Customer - The customer name - string
Description - The job description - string
Hours - The estimated hours - double
price - The price for the job

Create a constructor that requires parameters for all the data except price. Follow the order and data type above. Include auto-implemented properties for the job number, customer name, and job description, but not for hours or price; the price field value is calculated as estimated hours times $45.00 ($45.00 / hour) whenever the hours value is set.

Also create the following methods for theJob class:

An Equals() method that determines two Jobs are equal if they have the same job number
A GetHashCode() method that returns the job number
A ToString() method that returns a string containing all job information in the following format:

Job 111 Smith exterior paint 20 hours @$45.00 per hour. Total price is $900.00

The JobDemo application declares a few Job objects, sets their values, and demonstrates that all the methods work as expected.

info> In order to prepend the $ to currency values, the program will need to use the CultureInfo.GetCultureInfo method. In order to do this, include the statement using System.Globalization; at the top of your program and format the output statements as follows: WriteLine("This is an example: {0}", value.ToString("C", CultureInfo.GetCultureInfo("en-US")));

*/
using System;
using static System.Console;
using System.Globalization;
using System.Security.Cryptography;

namespace JobDemo
{
class JobDemo
{
static void Main()
{
// public Job jobOne = new Job(jobNumber, customerName, jobDescription, jobHours);

// My attempt at making objects
Job jobOne = new Job(1, "Cherry Painting", "Paint exterior of building", 12);
Job jobTwo = new Job(1, "Apple Painting", "Paint the interior of the building", 12);
Job jobThree = new Job(2, "Banna Painting", "Paint the Highschool Baseball Team Building", 12);
Console.WriteLine(jobOne.Equals(jobTwo));
Console.WriteLine(jobTwo).Equals(jobThree);
}


// Job Class
public class Job
{
public double price = hours * 45.00;
private double hours;
public int jobNumber { get; set; }
public string customer { get; set; }
public string description { get; set; }


// An Equals() method that determines two Jobs are equal if they have the same job number
public bool Equals(Job two)
{
if (this.jobNumber == Job.jobNumber)
{
return true;
}
return false;
}

public Job(int jobNumber, string customer, string description, double hours)
{
jobNumber = jobNumber;
Customer = customer;
description = description;
Hours = hours;
}
}

}
}
/*
Exercise 10-3A

Create an application named JobDemo that declares and uses Job objects. The Job class holds job information for a home repair service. The class has five properties that include:

JobNumber - The job number - int
Customer - The customer name - string
Description - The job description - string
Hours - The estimated hours - double
price - The price for the job

Create a constructor that requires parameters for all the data except price. Follow the order and data type above. Include auto-implemented properties for the job number, customer name, and job description, but not for hours or price; the price field value is calculated as estimated hours times $45.00 ($45.00 / hour) whenever the hours value is set.

Also create the following methods for theJob class:

An Equals() method that determines two Jobs are equal if they have the same job number
A GetHashCode() method that returns the job number
A ToString() method that returns a string containing all job information in the following format:

Job 111 Smith exterior paint 20 hours @$45.00 per hour. Total price is $900.00

The JobDemo application declares a few Job objects, sets their values, and demonstrates that all the methods work as expected.

info> In order to prepend the $ to currency values, the program will need to use the CultureInfo.GetCultureInfo method. In order to do this, include the statement using System.Globalization; at the top of your program and format the output statements as follows: WriteLine("This is an example: {0}", value.ToString("C", CultureInfo.GetCultureInfo("en-US")));

*/
using System;
using static System.Console;
using System.Globalization;
using System.Security.Cryptography;

namespace JobDemo
{
class JobDemo
{
static void Main()
{
// public Job jobOne = new Job(jobNumber, customerName, jobDescription, jobHours);

// My attempt at making objects
Job jobOne = new Job(1, "Cherry Painting", "Paint exterior of building", 12);
Job jobTwo = new Job(1, "Apple Painting", "Paint the interior of the building", 12);
Job jobThree = new Job(2, "Banna Painting", "Paint the Highschool Baseball Team Building", 12);
Console.WriteLine(jobOne.Equals(jobTwo));
Console.WriteLine(jobTwo).Equals(jobThree);
}


// Job Class
public class Job
{
public double price = hours * 45.00;
private double hours;
public int jobNumber { get; set; }
public string customer { get; set; }
public string description { get; set; }


// An Equals() method that determines two Jobs are equal if they have the same job number
public bool Equals(Job two)
{
if (this.jobNumber == Job.jobNumber)
{
return true;
}
return false;
}

public Job(int jobNumber, string customer, string description, double hours)
{
jobNumber = jobNumber;
Customer = customer;
description = description;
Hours = hours;
}
}

}
}
mtreit
mtreit•2mo ago
What are you using to compile?
mtreit
mtreit•2mo ago
You should see multiple errors:
No description
mtreit
mtreit•2mo ago
And you need to fix all of them 🙂
strikeouts27
strikeouts27OP•2mo ago
vs code
mtreit
mtreit•2mo ago
Well you should be getting a bunch of squiggles showing all of the errors.
strikeouts27
strikeouts27OP•2mo ago
I cannot use dot notation on something that returns void. oh the ) /workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.cs(52,35): error CS0236: A field initializer cannot reference the non-static field, method, or property 'JobDemo.Job.hours' [/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.csproj] checking public double price = hours * 45.00; it says it cannot access hours I am assuming. should i change the field to protected to get around that?
mtreit
mtreit•2mo ago
This is a field. A field is just a dumb location that holds data. It doesn't run code. This is unlike a property. So when you use equals to assign to a field, that code only runs once. It doesn't run again when the field is accessed. So this is not legal syntax.
No description
strikeouts27
strikeouts27OP•2mo ago
so I need it to hold one value and make a method.
private double price = 45.00;
private double price = 45.00;
mtreit
mtreit•2mo ago
That's what properties are. They can be assigned to in a way that looks like assigning to a field, but behind the scenes they are methods. You need to learn the difference between fields and properties. I think that's part of what this assignment is all about.
strikeouts27
strikeouts27OP•2mo ago
properties have a backing field, a get method to grab values from other places and set actually sets the objects attribute to that value. fields are just variables or storage containers on an object.
mtreit
mtreit•2mo ago
Right
strikeouts27
strikeouts27OP•2mo ago
/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.cs(62,39): error CS0120: An object reference is required for the non-static field, method, or property 'JobDemo.Job.jobNumber' [/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.csproj]
/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.cs(62,39): error CS0120: An object reference is required for the non-static field, method, or property 'JobDemo.Job.jobNumber' [/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.csproj]
mtreit
mtreit•2mo ago
See anything wrong here?
No description
strikeouts27
strikeouts27OP•2mo ago
I am trying to access an object inside of a method scope. And C# cannot see it.
mtreit
mtreit•2mo ago
No, you are trying to access the type. An object is an instance of a type.
strikeouts27
strikeouts27OP•2mo ago
I am trying to compare the job numbers using the Equals method.d If I cannot use dot notation.... hmm.....
mtreit
mtreit•2mo ago
What's the name of the instance passed to Equals?
strikeouts27
strikeouts27OP•2mo ago
two
mtreit
mtreit•2mo ago
Right so...
strikeouts27
strikeouts27OP•2mo ago
two.jobNumber
mtreit
mtreit•2mo ago
Yes 🙂
strikeouts27
strikeouts27OP•2mo ago
Whew. all that over one little function. Now I have a bunch of yelllow warnings.
/*
Exercise 10-3A

Create an application named JobDemo that declares and uses Job objects. The Job class holds job information for a home repair service. The class has five properties that include:

JobNumber - The job number - int
Customer - The customer name - string
Description - The job description - string
Hours - The estimated hours - double
price - The price for the job

Create a constructor that requires parameters for all the data except price. Follow the order and data type above. Include auto-implemented properties for the job number, customer name, and job description, but not for hours or price; the price field value is calculated as estimated hours times $45.00 ($45.00 / hour) whenever the hours value is set.

Also create the following methods for theJob class:

An Equals() method that determines two Jobs are equal if they have the same job number
A GetHashCode() method that returns the job number
A ToString() method that returns a string containing all job information in the following format:

Job 111 Smith exterior paint 20 hours @$45.00 per hour. Total price is $900.00

The JobDemo application declares a few Job objects, sets their values, and demonstrates that all the methods work as expected.

info> In order to prepend the $ to currency values, the program will need to use the CultureInfo.GetCultureInfo method. In order to do this, include the statement using System.Globalization; at the top of your program and format the output statements as follows: WriteLine("This is an example: {0}", value.ToString("C", CultureInfo.GetCultureInfo("en-US")));

*/
using System;
using static System.Console;
using System.Globalization;
using System.Security.Cryptography;

namespace JobDemo
{
class JobDemo
{
static void Main()
{
// public Job jobOne = new Job(jobNumber, customerName, jobDescription, jobHours);

// My attempt at making objects
Job jobOne = new Job(1, "Cherry Painting", "Paint exterior of building", 12);
Job jobTwo = new Job(1, "Apple Painting", "Paint the interior of the building", 12);
Job jobThree = new Job(2, "Banna Painting", "Paint the Highschool Baseball Team Building", 12);
Console.WriteLine(jobOne.Equals(jobTwo));
Console.WriteLine(jobTwo.Equals(jobThree));
}


// Job Class
public class Job
{
private double price = 45.00;
private double hours;
public int jobNumber { get; set; }
public string customer { get; set; }
public string description { get; set; }


// An Equals() method that determines two Jobs are equal if they have the same job number
public bool Equals(Job two)
{
if (this.jobNumber == two.jobNumber)
{
return true;
}
return false;
}

public Job(int jobNumber, string customer, string description, double hours)
{
jobNumber = jobNumber;
customer = customer;
description = description;
hours = hours;
}
}

}
}
/*
Exercise 10-3A

Create an application named JobDemo that declares and uses Job objects. The Job class holds job information for a home repair service. The class has five properties that include:

JobNumber - The job number - int
Customer - The customer name - string
Description - The job description - string
Hours - The estimated hours - double
price - The price for the job

Create a constructor that requires parameters for all the data except price. Follow the order and data type above. Include auto-implemented properties for the job number, customer name, and job description, but not for hours or price; the price field value is calculated as estimated hours times $45.00 ($45.00 / hour) whenever the hours value is set.

Also create the following methods for theJob class:

An Equals() method that determines two Jobs are equal if they have the same job number
A GetHashCode() method that returns the job number
A ToString() method that returns a string containing all job information in the following format:

Job 111 Smith exterior paint 20 hours @$45.00 per hour. Total price is $900.00

The JobDemo application declares a few Job objects, sets their values, and demonstrates that all the methods work as expected.

info> In order to prepend the $ to currency values, the program will need to use the CultureInfo.GetCultureInfo method. In order to do this, include the statement using System.Globalization; at the top of your program and format the output statements as follows: WriteLine("This is an example: {0}", value.ToString("C", CultureInfo.GetCultureInfo("en-US")));

*/
using System;
using static System.Console;
using System.Globalization;
using System.Security.Cryptography;

namespace JobDemo
{
class JobDemo
{
static void Main()
{
// public Job jobOne = new Job(jobNumber, customerName, jobDescription, jobHours);

// My attempt at making objects
Job jobOne = new Job(1, "Cherry Painting", "Paint exterior of building", 12);
Job jobTwo = new Job(1, "Apple Painting", "Paint the interior of the building", 12);
Job jobThree = new Job(2, "Banna Painting", "Paint the Highschool Baseball Team Building", 12);
Console.WriteLine(jobOne.Equals(jobTwo));
Console.WriteLine(jobTwo.Equals(jobThree));
}


// Job Class
public class Job
{
private double price = 45.00;
private double hours;
public int jobNumber { get; set; }
public string customer { get; set; }
public string description { get; set; }


// An Equals() method that determines two Jobs are equal if they have the same job number
public bool Equals(Job two)
{
if (this.jobNumber == two.jobNumber)
{
return true;
}
return false;
}

public Job(int jobNumber, string customer, string description, double hours)
{
jobNumber = jobNumber;
customer = customer;
description = description;
hours = hours;
}
}

}
}
its complaining about hours = hours and customer = customer I belive that you have what I would call the vision for this. the abaility to see what is wrong and make corrections. how can I acquire this vision? Are there any resources, drills, or learning materials so I can improve my troubleshooting?
mtreit
mtreit•2mo ago
This is a case where you need to use this to disambiguate, or name the input parameter different than the field or property. Properties should be upper case so if you make it Customer instead of customer for instance your code will work.
Customer = customer;
Customer = customer;
Well, yes I do because I have been programming for many years. It just comes with practice and experience. You will develop "the vision" by just keeping making mistakes, learning how to fix them, and writing more code. It gets easier. You are learning the very basic fundamentals like the difference between types and instances of types, field vs. properties, how methods work, etc. Once that stuff becomes second nature things will be much easier. But you have to learn the fundamentals first.
strikeouts27
strikeouts27OP•2mo ago
mtreit I owe you one. Thank you for helping me. It seems I have a lot more textbook re reading to do to grasp the fundamentals. I think I am going to take a break for the time being.
mtreit
mtreit•2mo ago
Looks like you are making progress. Glad I could help. I tried to steer you to the right answer rather than just giving you the answer, so hopefully you learned a few things 🙂 Good luck, I have to take off as well.
strikeouts27
strikeouts27OP•2mo ago
thank you again.

Did you find this page helpful?