0

I have just started writing C# a few days ago and would need some help with a program that I'm writing.

This is not the real code just something I wrote to show you how the original code is built.

public partial class form1 : form
{
int container;
int resource;
int capacity;
int price;
int total;

int basket = 10; //container that holds 10 slots
int apples = 2; //resource that takes 2 slots per resource
}

public form1()
{
private void checkbox1_checkedchanged(object sender, eventargs e) //basket checkbox
{

if (((CheckBox)checkbox1).Checked == true)
{
basket = container;
}

}

private void checkbox2_checkedchanged(object sender, eventargs e) //apples checkbox
{

if (((CheckBox)checkbox2.Checked == true)
{

apples = resource;

}

}
private void button1_Click(object sender, EventArgs e) //calculate button
{
container / resource = capacity; //to see how many resources the basket can hold
/* textbox 1 is where they write the price for what 1 apple cost */
price = int.Parse(textbox1.text); 
capacity * price = total;
textbox2.AppendText(total); //textbox2 is where they will se how much the apples cost
}



}

The problem is that I get errors from the code in "button1" that says everything from

  • The left-hand side of an assignment must be avariable, property or indexer
  • Cannot take the address of, get the size of, or declare a pointer to a managed type ('capacity')
  • 'WindowsFormApplication1.Form1.Capacity' is a 'field' but is used like a 'type'
  • Pointers are fixed size buffers may only be used in a unsafe context
  • cannot implicitly convert type 'int' to 'capacity*'. An explicit conversion exists (are you missing a cast?)
  • The best overloaded method match for 'System.Window.Forms.TextBoxBase.AppendText(String)'hassome invalid arguments
  • Argument 1: cannot convert from 'int' to 'string'

So I'm wondering what I'm doing wrong to achieve the kind of application I want. The application looks something like this:

enter image description here

Edit: Original code has been removed

Edit2: This is how the button1 looks like now and there is no errors except one that occurs when I try to divide vehicle with salt (Haven't tried any other resource but thats the one that is listed). The code looks the same like before but I changed this thanks to David Pilkington.

private void button1_Click(object sender, EventArgs e) /* calculate-button */
    {
        capacity = vehicle / resource;
        total = capacity * price;
        textBox4.AppendText(total.ToString());
    }
  • 3
    Can you actually paste the real code? The one above is far from anything that would compile so it's too hard to say what is the problem? – Szymon Mar 05 '14 at 09:03
  • The code is pretty long because of all the kinds of vehicles and resources, should I just edit the post or something? Because the code takes up pretty much space. –  Mar 05 '14 at 09:14
  • No, no, here's what's wrong (at least at a quick glance): 1. Everything is lower case. 2. Assignments are the other way round, e.g. `container / resource = capacity;`. 3. Braces are in wrong positions. – Szymon Mar 05 '14 at 09:17
  • Please post the code that you actually have. Not all of it, just the relevant part - the part that is giving you problems. – Szymon Mar 05 '14 at 09:23
  • @Szymon, I just posted the whole thing before I saw your comment. –  Mar 05 '14 at 09:24

2 Answers2

0
private void button1_Click(object sender, EventArgs e) //calculate button
{
    capacity = container / resource; //to see how many resources the basket can hold
    price = int.Parse(textbox1.text); 
    total= capacity * price;
    textbox2.AppendText(total.ToString()); //textbox2 is where they will se how much the     apples cost
}

Your assignments are the wrong way round and you need to convert the int to a string before adding it

You may also want to use TryParse to handle messy input

private void button1_Click(object sender, EventArgs e) //calculate button
{
    capacity = container / resource; //to see how many resources the basket can hold
    if(int.TryParse(textbox1.text, out price)) 
    {
       total= capacity * price;
       textbox2.AppendText(total.ToString()); //textbox2 is where they will se how much the apples cost
    }
}

BIG EDIT:

It seems your assignments are all the wrong way round. This means that resource is never set and will create and error in this line capacity = container / resource;

David Pilkington
  • 13,528
  • 3
  • 41
  • 73
  • The code in the question is so wrong that any attempt at answering is quite futile and pure guesswork. – Szymon Mar 05 '14 at 09:18
  • @Szymon Too you maybe. The code provided here will solve all errors listed. But go ahead an downvote because you couldnt see it – David Pilkington Mar 05 '14 at 09:19
  • What about closed class definition just after variables? Constructor that has methods inside it? Non-existent types (look at the casing everywhere)? – Szymon Mar 05 '14 at 09:21
  • @Szymon look at the errors listed then look at his code. It is clear what the problem is. Instead of berating him for his copy paste. Try and help him. But you donwvoted ALL answers, just move on – David Pilkington Mar 05 '14 at 09:23
  • Ok, downvotes removed, they were maybe a bit rushed. But answering a question that is clearly in a bad state is not something that I personally like to see. – Szymon Mar 05 '14 at 09:25
  • Thanks, this removed all errors that showed up before and button1 look something like this now: capacity = vehicle / resource; total = capacity * price; textBox4.AppendText(total.ToString()); But, when I choose "offroad truck" and "apples" and write in 1 as price per resource in my application (the one I posted a image of above) the application tab down and it says "Attempted to divide by zero" but capacity for salt are listed (=3) and the vehicle slot for offroad truck are also listed (=65). Which means it should have divided 65 with 3, but instead nothing happens and I get error. –  Mar 05 '14 at 11:05
  • apparently code doesn't work very well in comments so I will update my question thread instead. –  Mar 05 '14 at 11:06
  • @user3382605 look at all of your vehicle assignments. It is the same issue as before. `vehicle` should be on the left – David Pilkington Mar 05 '14 at 11:14
  • Thanks, I just came up with another fix myself. But this one is better. Thank you so much David :) - One question, how come the resource does not have to be on the left like "vehicle"? example: apples = resource; –  Mar 05 '14 at 11:19
0

Your code is all wrong in the button2_click()

container / resource = capacity; //to see how many resources the basket can hold
/* textbox 1 is where they write the price for what 1 apple cost */
price = int.Parse(textbox1.text); 
capacity * price = total;
textbox2.AppendText(total); 

Should (perhaps?) be

capacity = container / resource; //to see how many resources the basket can hold
/* textbox 1 is where they write the price for what 1 apple cost */
price = int.Parse(textbox1.text); 
total = capacity * price;
textbox2.AppendText(total.ToString());

That should remove most of your errors in the code.

Hope it helps! //KH.

Karl-Henrik
  • 1,113
  • 1
  • 11
  • 17
  • And how your answer is different from that of `David Pilkington` despite, he wrote it a lot earlier? Also, in his code it doesn't get clear what's the value of `resource` it may very well be zero. – Leron Mar 05 '14 at 09:15
  • The code in the question is so wrong that any attempt at answering is quite futile and pure guesswork. The question needs to be cleaned up or closed. – Szymon Mar 05 '14 at 09:18
  • Leron - Not sure what actually happend, I updated the site and did not see a reply so I sent mine in and there were no reply then but now that I revisit it I can see davids clear as day.. odd. – Karl-Henrik Mar 05 '14 at 09:23