C
C#•3y ago
Jules

i dont understand why its erroring, streamreader is red

I've been stuck on this for a while but I seriously don't know what to do, Total is supposed to show the total price but it's just stuck on total
52 Replies
Jules
JulesOP•3y ago
private decimal calculateTotal_Click(object sender, EventArgs e)
{
// This procedure calculates the total of an order
//decimal subtotal = 0;
//decimal tax = 0;
decimal total = 0;
total = memCost() + optCost();
//tax = Final(subtotal);
//total = subtotal + tax;
total = total;
//taxLabel.Text = tax.ToString();
//totalLabel.Text = total.ToString();
return total;
}

private Button Total_Click(object sender, EventArgs e)
{
return calculateTotal;

}
}
}
private decimal calculateTotal_Click(object sender, EventArgs e)
{
// This procedure calculates the total of an order
//decimal subtotal = 0;
//decimal tax = 0;
decimal total = 0;
total = memCost() + optCost();
//tax = Final(subtotal);
//total = subtotal + tax;
total = total;
//taxLabel.Text = tax.ToString();
//totalLabel.Text = total.ToString();
return total;
}

private Button Total_Click(object sender, EventArgs e)
{
return calculateTotal;

}
}
}
this is what i have
TheBoxyBear
TheBoxyBear•3y ago
The Total click handler simply returns the button and doesn't call its click event Is calculateTotal_Click tied to an event? Typycally you want event handlers to be void as you're not meant to call events handlers manually or raise them to get a value
Jules
JulesOP•3y ago
i dont think so, all i have it tied to this click to calculate total
TheBoxyBear
TheBoxyBear•3y ago
If you want to clean it up, you would have one void event handler but the button click that only calls a decimal method That other method gets the input from the form, does the calculations and returns the result. The event handler takes the result and displays it Or keep the getting of input in the event handler and pass them as parameters to the calculation method so that one becomes independent of the front-end (form)
Jules
JulesOP•3y ago
i have it like this
Jules
JulesOP•3y ago
TheBoxyBear
TheBoxyBear•3y ago
So that method handles the click, the other just does the calculation, not taking a sender or eventargs
Jules
JulesOP•3y ago
uhhh this is my code
Jules
JulesOP•3y ago
i feel i need to type something in total_click
TheBoxyBear
TheBoxyBear•3y ago
You can remove that one entirely
Jules
JulesOP•3y ago
okay i took it off, but how would i make calculateTotal show me the answer? total still isnt changing (sorry im a bit learning disabled)
TheBoxyBear
TheBoxyBear•3y ago
You commented out the line that sets the label text
Jules
JulesOP•3y ago
but .Text isn't working, thats why i commented it out
Jules
JulesOP•3y ago
TheBoxyBear
TheBoxyBear•3y ago
total is a decimal variable You need to reference your label The line right above it Same principle
Jules
JulesOP•3y ago
.. oh huh
TheBoxyBear
TheBoxyBear•3y ago
You can also remove total = total
Jules
JulesOP•3y ago
yeah idk what i was doing with that i think im getting closer
TheBoxyBear
TheBoxyBear•3y ago
Coding is an iterative process after all 🙂
Jules
JulesOP•3y ago
but im still trying to figure out how to get totalLabel.Text to work still im getting lil frustrated lol im almost there
TheBoxyBear
TheBoxyBear•3y ago
What line do you have now to set it? Maybe it's not the right label
Jules
JulesOP•3y ago
TheBoxyBear
TheBoxyBear•3y ago
Try putting a breakpoint to see if the method runs
Jules
JulesOP•3y ago
its like this now
TheBoxyBear
TheBoxyBear•3y ago
And what's the error? You shouldn't be able to run the app if there are errors
Jules
JulesOP•3y ago
The name 'totalLabel' does not exist in the current context
TheBoxyBear
TheBoxyBear•3y ago
If you select Yes on the prompt when running it, it's running the last successful build What is the label called in the form?
Jules
JulesOP•3y ago
legit i called it total
TheBoxyBear
TheBoxyBear•3y ago
so it should be total.Text
Jules
JulesOP•3y ago
wait its not capital
TheBoxyBear
TheBoxyBear•3y ago
However that means you have to rename either the label of variable to avoid a conflict
Jules
JulesOP•3y ago
IT WASNT CAPITAL I DIDNT REALIZE I CAPITALIZED IT LMAO
TheBoxyBear
TheBoxyBear•3y ago
totalLabel would be an appropriate name for it following the naming scheme of the rest of the form
Jules
JulesOP•3y ago
but now its being weird and not showing stuff from optional costs hm
TheBoxyBear
TheBoxyBear•3y ago
Variable names are case sensitive so you won't have a conflict with total and Total but you should avoid this as you can easily confuse which is which
Jules
JulesOP•3y ago
yes but its completely ignoring part of my code
TheBoxyBear
TheBoxyBear•3y ago
Does it compile?
Jules
JulesOP•3y ago
yeah
TheBoxyBear
TheBoxyBear•3y ago
Put a breakpoint in the code that's supposed to run and see if it gets hit
Jules
JulesOP•3y ago
private decimal optCost()
{
decimal optCost = 0;
// pick opt
if (yogaCheckBox.Checked)
{
optCost += Yoga;
}
if (zumbaCheckBox.Checked)
{
optCost += Zumba;
}
if (karateCheckBox.Checked)
{
optCost += Karate;
}
if (waterCheckBox.Checked)
{
optCost += Water;
}
if (trainerCheckBox.Checked)
{
optCost += Trainer;
}
return optCost;
}

private decimal memCost()
{
decimal memCost = 0;

// pick an age
if (teenRadioButton.Checked)
{
memCost = Teens;
}
else if (youngRadioButton.Checked)
{
memCost = Young_Adults;
}
else if (adultRadioButton.Checked)
{
memCost = Adult;
}
else if (seniorRadioButton.Checked)
{
memCost = Senior;
}
return memCost;
}
private decimal optCost()
{
decimal optCost = 0;
// pick opt
if (yogaCheckBox.Checked)
{
optCost += Yoga;
}
if (zumbaCheckBox.Checked)
{
optCost += Zumba;
}
if (karateCheckBox.Checked)
{
optCost += Karate;
}
if (waterCheckBox.Checked)
{
optCost += Water;
}
if (trainerCheckBox.Checked)
{
optCost += Trainer;
}
return optCost;
}

private decimal memCost()
{
decimal memCost = 0;

// pick an age
if (teenRadioButton.Checked)
{
memCost = Teens;
}
else if (youngRadioButton.Checked)
{
memCost = Young_Adults;
}
else if (adultRadioButton.Checked)
{
memCost = Adult;
}
else if (seniorRadioButton.Checked)
{
memCost = Senior;
}
return memCost;
}
just to save what i got
TheBoxyBear
TheBoxyBear•3y ago
Which part is it ignoring?
Jules
JulesOP•3y ago
opt
TheBoxyBear
TheBoxyBear•3y ago
Put a breakpoint in it
Jules
JulesOP•3y ago
got it! the button isnt actually fully working but
TheBoxyBear
TheBoxyBear•3y ago
Judging by this screenshot it should run
Jules
JulesOP•3y ago
^q^ it runs! thank you so so so much New issue, new stuff
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());

const in SIZE = 43;
int[] numbers = new int[SIZE];

int index = 0;

StreamReader inputFile;
inputFile = File.OpenText("Scores.txt");

while (index < numbers.Length && !inputFile.EndOfStream)
{
numbers[index] = int.Parse(inputFile.ReadLine());
index++;
}
inputFile.Close();

for (int index = 0; index < units.Length; index++)
{
total += units[index]
}
}
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());

const in SIZE = 43;
int[] numbers = new int[SIZE];

int index = 0;

StreamReader inputFile;
inputFile = File.OpenText("Scores.txt");

while (index < numbers.Length && !inputFile.EndOfStream)
{
numbers[index] = int.Parse(inputFile.ReadLine());
index++;
}
inputFile.Close();

for (int index = 0; index < units.Length; index++)
{
total += units[index]
}
}
StreamReader, File, SIZE, and index is all in red
Anchy
Anchy•3y ago
probably missing namespace declarations and size has an incomplete type specifier
Jules
JulesOP•3y ago
how would i fix that?
Anchy
Anchy•3y ago
add the appropriate usings to the top of the file for those types or use ctrl + . to bring up suggestions that will most likely suggest to add them to have it done for you is there a reason you are putting this in your entrypoint for your forms app
Jules
JulesOP•3y ago
Hey! Just saw this, it’s a console app
Anchy
Anchy•3y ago
the first 3 lines of your Main entrypoint says otherwise?
Jules
JulesOP•3y ago
I told it to do console… huh. Weird.
Want results from more Discord servers?
Add your server