Understanding get; set; backing field requirements.
Per the instructions (full instructions listed in the comments)
"make 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 very last {} totalPrice needs to be created. Which requires defining it in a get set
I was under the impression that if you are going to write a custom get set that is NOT an auto property, you must create a backing field for it. So I made a totalPrice double and tried to run the code. I got this error.
why is this?
55 Replies
I am thinking that my problem is I named the backing field the same as my Property attempting a casing change
is there a particular reason why you think you need a property instead of just a local inside ToString?
well I attempted to do hours * price but it gave me pushback
what pushback?
checking notes....
Also, your backing field needs a different name from your prop.
As for the error, since you provided a set impl, you're no longer in autoprop land, so the compiler doesn't konw the backing field, so it can't fill in get for you.
so even if I succeeded in making a property with a backing field I will still fail.
or whatever
but also, i don't see why you can't just use an autoprop here
just compute the total price inside ToString
err, sorry, I don't see why you need a TotalPrice prop at all
the purpose of the ToString function is to get the total job cost
currently there is price at 45.00 in a variable. and hours in a variable.
@strikeouts27 "the function" being what? ToString?
yeah, so compute it in there
attempting...
The assignment says:
the price field value is calculated as estimated hours times $45.00 ($45.00 / hour) whenever the hours value is set.
I think i tried simply doing price * hours and it wasn't reachable for the tostring method.
error method must have a return type, checking tostring now
There's no method in this code
that error isn't about your prop
yeah, that
(also, $code to make it more readable)
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif in chat
For longer snippets, use: https://paste.mod.gg/https://paste.mod.gg/eleywrdlbxex/0
error code
BlazeBin - eleywrdlbxex
A tool for sharing your source code with the world!
I'm on a new error now I consider this progress.
Well, the error is quite obvious
price is not defined so I need to make it accessible for the method to be used.
It is passed in as a parameter. Considering this.
There's no field named
price, no property named price, no other member named price, no parameter named price nor a local variable named price
Is it?
ToString(), where you use price, has no parameters at alloh thats right I destroyed it making the backing field
so just change the price to hourly rate
For example, yes
okay cool that error dissapeared.
attempting to get it to print out output. trying to solve on my own. will come back if i have more questions.
:Ok:
Okay I rediscovered the error that was preventing a calculation. Basically the complier is saying the job instance methods have not been created at the time of calculation.
error code
https://paste.mod.gg/wtfaufnfbwjw/0
BlazeBin - wtfaufnfbwjw
A tool for sharing your source code with the world!
The error is telling you exactly what the issue is.
You can't do this:
The initializer for totalPrices (that is, the stuff on the right-side of the equal sign) cannot refer to other non-static fields. In this case,
hoursim thinking...
At the time that initializer runs, hours does not yet have a value.
if its a timing issue than maybe i make a method?
I would need to make something that runs after the values are inintalized correct? And methods calls run after instantiation.
Or while they are being initialized
(*cough cough* constructor)
ah, so your saying that its possible to quick start a method call by editing the constructor with a called method. so on creation it runs the method.
Assuming you even need a method here
But sure
I'm adding this to my textbook notes and this error message. okay I will look up how to do that.
BlazeBin - unwltqceovem
A tool for sharing your source code with the world!
error message
My question is, why is it not instantiating hours, hourlyrate, and TotalPrice?
mtreit
You can't do this:
Quoted by
<@406536255426396160> from #Understanding get; set; backing field requirements. (click here)
React with ❌ to remove this embed.
mtreit
At the time that initializer runs, hours does not yet have a value.
Quoted by
<@406536255426396160> from #Understanding get; set; backing field requirements. (click here)
React with ❌ to remove this embed.
didnt my constructor intializer take care of that?
so making a constructor means its too late.
You can never do what you are trying because the constructor doesn't run until all of the field intializers have executed.
It means you should stop trying to do any math in the field initializer.
And do it in the constructor instead.
You might fix up your use of
this to be consistent.so the field zone is a no math zone.
okay i will edit my notes to that.
Well, no, you can do math with constant or static values. Just not using values from other instance members, like other fields.
PROJECT COMPLETE!
So that next time i can be a better researcher does anyone have any reccomended learning materials for looking up C# related problems? Learning websites?
We normally suggest people start with $helloworld
Get started with beginner tutorials
- Interactive text tutorial
- Video tutorial
- Fundamentals documentation
So be it. Determined face.
You can also do things like solve small programming problems using C# to get more experience with it.
Advent of Code is great for this:
https://adventofcode.com/
You can go back and do all of past years problems. For instance, go solve day 1 from every year for practice. Then do day 2. (They get harder in general as the days go on.)
There is also $projects
Collections of application ideas that anyone can solve in any programming language to improve coding skills:
https://github.com/dotnet/dotnet-console-games
https://github.com/karan/Projects
https://github.com/florinpop17/app-ideas