C
C#2mo ago
strikeouts27

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
my ToString Method so far
public override string ToString()
{
Console.WriteLine($"Job {jobNumber} {customer} exterior paint {hours} hours @{price} per hour. Total price is {totalPrice}");
}
my ToString Method so far
public override string ToString()
{
Console.WriteLine($"Job {jobNumber} {customer} exterior paint {hours} hours @{price} per hour. Total price is {totalPrice}");
}
The very last {} totalPrice needs to be created. Which requires defining it in a get set
private const double hourlyrate = 45.00;
private double hours;
private double totalPrice;
private double totalPrice
{
get; set
{
hours = value;
price = hourlyrate * price;
}
}
private const double hourlyrate = 45.00;
private double hours;
private double totalPrice;
private double totalPrice
{
get; set
{
hours = value;
price = hourlyrate * price;
}
}
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?
/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.cs(57,41): error CS0501: 'JobDemo.Job.totalPrice.get' must declare a body because it is not marked abstract, extern, or partial [/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(57,41): error CS0501: 'JobDemo.Job.totalPrice.get' must declare a body because it is not marked abstract, extern, or partial [/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.csproj]
55 Replies
strikeouts27
strikeouts27OP2mo ago
I am thinking that my problem is I named the backing field the same as my Property attempting a casing change
ero
ero2mo ago
is there a particular reason why you think you need a property instead of just a local inside ToString?
strikeouts27
strikeouts27OP2mo ago
well I attempted to do hours * price but it gave me pushback
jcotton42
jcotton422mo ago
what pushback?
strikeouts27
strikeouts27OP2mo ago
checking notes....
jcotton42
jcotton422mo ago
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.
strikeouts27
strikeouts27OP2mo ago
so even if I succeeded in making a property with a backing field I will still fail.
jcotton42
jcotton422mo ago
private double totalPrice;
public double TotalPrice
{
get { return totalPrice; }
set { totalPrice = value; }
}
private double totalPrice;
public double TotalPrice
{
get { return totalPrice; }
set { totalPrice = value; }
}
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
strikeouts27
strikeouts27OP2mo ago
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.
jcotton42
jcotton422mo ago
@strikeouts27 "the function" being what? ToString? yeah, so compute it in there
strikeouts27
strikeouts27OP2mo ago
attempting...
mtreit
mtreit2mo ago
The assignment says:
the price field value is calculated as estimated hours times $45.00 ($45.00 / hour) whenever the hours value is set.
strikeouts27
strikeouts27OP2mo ago
I think i tried simply doing price * hours and it wasn't reachable for the tostring method.
cs
private const double hourlyrate = 45.00;
private double hours;
private double totalPrice;
public double TotalPrice
{
get
{
return totalPrice;
}
set {
{ totalPrice = value; }
}
}
cs
private const double hourlyrate = 45.00;
private double hours;
private double totalPrice;
public double TotalPrice
{
get
{
return totalPrice;
}
set {
{ totalPrice = value; }
}
}
error method must have a return type, checking tostring now
Angius
Angius2mo ago
There's no method in this code
jcotton42
jcotton422mo ago
that error isn't about your prop yeah, that
Angius
Angius2mo ago
(also, $code to make it more readable)
MODiX
MODiX2mo ago
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/
strikeouts27
strikeouts27OP2mo ago
https://paste.mod.gg/eleywrdlbxex/0 error code
cs
/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.cs(105,95): error CS0103: The name 'price' does not exist in the current context [/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(103,36): error CS0161: 'JobDemo.Job.ToString()': not all code paths return a value [/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.csproj]
cs
/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.cs(105,95): error CS0103: The name 'price' does not exist in the current context [/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(103,36): error CS0161: 'JobDemo.Job.ToString()': not all code paths return a value [/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.csproj]
BlazeBin - eleywrdlbxex
A tool for sharing your source code with the world!
strikeouts27
strikeouts27OP2mo ago
I'm on a new error now I consider this progress.
Angius
Angius2mo ago
Well, the error is quite obvious
strikeouts27
strikeouts27OP2mo ago
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.
Angius
Angius2mo ago
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 all
strikeouts27
strikeouts27OP2mo ago
oh thats right I destroyed it making the backing field so just change the price to hourly rate
Angius
Angius2mo ago
For example, yes
strikeouts27
strikeouts27OP2mo ago
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.
Angius
Angius2mo ago
:Ok:
strikeouts27
strikeouts27OP2mo ago
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
/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.cs(59,41): 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]
/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.cs(59,41): 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]
https://paste.mod.gg/wtfaufnfbwjw/0
public override string ToString()
{
return $"Job {jobNumber} {customer} {description} {hours} hours @{hourlyrate} per hour. Total price is {TotalPrice}.";
}
public override string ToString()
{
return $"Job {jobNumber} {customer} {description} {hours} hours @{hourlyrate} per hour. Total price is {TotalPrice}.";
}
public class Job
{
private const double hourlyrate = 45.00;
private double hours;
private double totalPrice = hours * hourlyrate;
public double TotalPrice
{
get
{
return totalPrice;
}
set {
{ totalPrice = value; }
}
}
public class Job
{
private const double hourlyrate = 45.00;
private double hours;
private double totalPrice = hours * hourlyrate;
public double TotalPrice
{
get
{
return totalPrice;
}
set {
{ totalPrice = value; }
}
}
BlazeBin - wtfaufnfbwjw
A tool for sharing your source code with the world!
mtreit
mtreit2mo ago
The error is telling you exactly what the issue is.
mtreit
mtreit2mo ago
You can't do this:
No description
mtreit
mtreit2mo ago
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, hours
strikeouts27
strikeouts27OP2mo ago
im thinking...
mtreit
mtreit2mo ago
At the time that initializer runs, hours does not yet have a value.
strikeouts27
strikeouts27OP2mo ago
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.
Angius
Angius2mo ago
Or while they are being initialized (*cough cough* constructor)
strikeouts27
strikeouts27OP2mo ago
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.
Angius
Angius2mo ago
Assuming you even need a method here But sure
strikeouts27
strikeouts27OP2mo ago
I'm adding this to my textbook notes and this error message. okay I will look up how to do that.
strikeouts27
strikeouts27OP2mo ago
public Job(int jobNumber, string customer, string description, double hours) : this(hours, hourlyrate, TotalPrice)
{
this.jobNumber = jobNumber;
this.customer = customer;
this.description = description;
this.hours = hours;
}
public Job(int jobNumber, string customer, string description, double hours) : this(hours, hourlyrate, TotalPrice)
{
this.jobNumber = jobNumber;
this.customer = customer;
this.description = description;
this.hours = hours;
}
https://paste.mod.gg/unwltqceovem/0
BlazeBin - unwltqceovem
A tool for sharing your source code with the world!
strikeouts27
strikeouts27OP2mo ago
error message
/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.cs(59,41): 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]
/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.cs(59,41): 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]
My question is, why is it not instantiating hours, hourlyrate, and TotalPrice?
MODiX
MODiX2mo ago
mtreit
You can't do this:
From mtreit
React with ❌ to remove this embed.
MODiX
MODiX2mo ago
mtreit
At the time that initializer runs, hours does not yet have a value.
React with ❌ to remove this embed.
strikeouts27
strikeouts27OP2mo ago
didnt my constructor intializer take care of that?
mtreit
mtreit2mo ago
private double totalPrice = hours * hourlyrate; // The constructor runs after this.
private double totalPrice = hours * hourlyrate; // The constructor runs after this.
strikeouts27
strikeouts27OP2mo ago
so making a constructor means its too late.
mtreit
mtreit2mo ago
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.
strikeouts27
strikeouts27OP2mo ago
public Job(int jobNumber, string customer, string description, double hours, double hourlyRate)
{
jobNumber = jobNumber;
this.customer = customer;
description = description;
this.hours = hours;

totalPrice = hours * hourlyrate;
}
public Job(int jobNumber, string customer, string description, double hours, double hourlyRate)
{
jobNumber = jobNumber;
this.customer = customer;
description = description;
this.hours = hours;

totalPrice = hours * hourlyrate;
}
mtreit
mtreit2mo ago
You might fix up your use of this to be consistent.
strikeouts27
strikeouts27OP2mo ago
so the field zone is a no math zone. okay i will edit my notes to that.
mtreit
mtreit2mo ago
Well, no, you can do math with constant or static values. Just not using values from other instance members, like other fields.
strikeouts27
strikeouts27OP2mo ago
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?
mtreit
mtreit2mo ago
We normally suggest people start with $helloworld
MODiX
MODiX2mo ago
Get started with beginner tutorials - Interactive text tutorial - Video tutorial - Fundamentals documentation
strikeouts27
strikeouts27OP2mo ago
So be it. Determined face.
mtreit
mtreit2mo ago
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
MODiX
MODiX2mo ago
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

Did you find this page helpful?