88 Replies
I mean, it has an obvious syntax error.
So presumably this code does not compile.
Revisit the syntax for an
if statement.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?
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.
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
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.
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?
Just hard-code the values in the demo program if I understand the assignment correctly.
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!";
}
Your code has a bunch of remaining invalid-syntax issues though.
sigh. agreed. one fire at a time.
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.
// 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;
}
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.thats unreadable nvm
It was fine, I was reading it 😄
But looks like they are teaching you how it works which is good.
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
Uh...
No.
That's not close to correct.
It has public override its named equals. it returns true and false
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.
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
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:
This is a complete program. It currently prints:
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:
If you can make the above program (which is simpler subproblem of your assignment) I think you will be able to make some progres...
I appreciate your advice. I will try to do that right now.
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.
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?
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?
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.
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:
(or possibly this is you have nullable reference types enabled):
You know what.
Re-reading your assignment, I'm not sure they want you to override the base Equals method.
Like This?
That might be getting too complicated.
Like this?
So, there is a lot wrong with this.
were trying to use a method right? dot notation?
Again, forget about using
override I think I was making things harder than necessary.
And Equials should return bool not string.good catch yes the type needs to be bool. updated.
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).that makes sense. this is a method for every object not just those two.
It should look something like this:
maybe an input parame ter?
current instance. the this keyword?
Using the
this keyword is optional, but yes.
Inside the instance, this.Id and Id are the same thing.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?
maybe thats not my problem i just need to specify another job needs to be passed in.
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.int number string "Texas"
but for job objects we specify the Object name right? since an object becomes a type in c#?
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.okay i will do that.
In your case the "user" is the code calling the method, which will be in the demo program.
In the demo program, the values are just hard-coded:

But the Equals method doesn't know that or care. It just uses whatever instances was passed in.
Inside the implementation of Equals, does it need to know where jobTwo came from? No it does not.
/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.
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.Remember that C# is case sensitive. You don't have a property or field called
Hours with a capital H.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.
You shouldn't be using AI for an assignment like this...
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.
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.
oh I thought var held variables like it does in javascript. my bad.
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:
You can just write:
Which is more readable.what was wrong with saying Job jobOne am I not specifying that it is of the job object?
These are identical:
Up to you which to use.
As type names get longer,
var can be nice./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]
What are you using to compile?
You should see multiple errors:

And you need to fix all of them 🙂
vs code
Well you should be getting a bunch of squiggles showing all of the errors.
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?
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.

so I need it to hold one value and make a method.
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.
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.
Right
See anything wrong here?

I am trying to access an object inside of a method scope. And C# cannot see it.
No, you are trying to access the type. An object is an instance of a type.
I am trying to compare the job numbers using the Equals method.d If I cannot use dot notation.... hmm.....
What's the name of the instance passed to Equals?
two
Right so...
two.jobNumber
Yes 🙂
Whew. all that over one little function.
Now I have a bunch of yelllow warnings.
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?
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.
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.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.
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.
thank you again.