BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
|
| 31 Oct 2013 12:55 PM |
Hello I'm currently learning C#. I created this random application that generates a random number when you click button1 if you click button1 ten times then it shows a message box that shows "Limit reached!:", but it seems that my code is not working, and I do not get an output.
int num1 = 1; int num2 = 2; int clicks = 0; Random random = new Random(); int randomNumber = random.Next(100,10000); label1.Text = ("Random: "+randomNumber); clicks = clicks + 1; if (clicks == 10) { button1.Enabled = false; MessageBox.Show("Limit reached!","Notice!"); } |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 31 Oct 2013 12:57 PM |
| If you declare "int clicks = 0;" inside the body of the function, then it isn't carried over to the next time you click it. Also, what are the num1 and num2 variables for? |
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
|
| 31 Oct 2013 12:58 PM |
| Oh those are just random integers i created ignore them. |
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
|
| 31 Oct 2013 01:01 PM |
| So, where do I place the integer? |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 31 Oct 2013 01:03 PM |
| Outside the function body. |
|
|
| Report Abuse |
|
|
| |
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
|
| 31 Oct 2013 01:04 PM |
| I'm sorry I'm just new to this .-. |
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
|
| 31 Oct 2013 01:09 PM |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) //int clicks = 0; here? { Random random = new Random(); int randomNumber = random.Next(100,10000); clicks = clicks + 1; label1.Text = ("Random: "+randomNumber+" Clicks: "+clicks.ToString()); if (clicks >= 10) { button1.Enabled = false; MessageBox.Show("Limit reached!","Notice!"); } else if(clicks != 10){ MessageBox.Show("Continue!", "Notice!"); } }
private void textBox1_TextChanged(object sender, EventArgs e) {
}
private void label1_Click(object sender, EventArgs e) {
} } }
|
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 31 Oct 2013 01:10 PM |
| No, before the function header (the private void button1_Click line) |
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
| |
|
|
| 31 Oct 2013 01:13 PM |
| Dr01d, I still haven't seen sharp, so I have a question, is his code logically invalid because the value keeps resetting or because it makes a new variable(or both?)? |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 31 Oct 2013 01:15 PM |
@Bruce: It worked then? :D
@Android: Variable scope. Because clicks was declared inside the function, as soon the function stops the value is lost, and so next time it's clicked, a new variable called clicks is created again with the value of 0. |
|
|
| Report Abuse |
|
|
|
| 31 Oct 2013 01:17 PM |
| So, it's both? Does this also happen in C? |
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
| |
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 31 Oct 2013 01:18 PM |
| @Android: Yes variable scope is important in C, and C++, and Java, and Lua, and a lot of languages. |
|
|
| Report Abuse |
|
|
|
| 31 Oct 2013 01:20 PM |
| Really? I didn't see this happen in Lua. :/ |
|
|
| Report Abuse |
|
|
|
| 31 Oct 2013 01:22 PM |
do local var = 1 end print(var)
>nil
Or am I missing something? |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 31 Oct 2013 01:22 PM |
Run this:
local function clicked() local clicks = 0; print("Clicked"); clicks = clicks + 1; if (clicks == 10) then print("10 clicks!"); end end
for i = 1, 10 do clicked(); end |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 31 Oct 2013 01:23 PM |
| @Not: Remove the "local" before var and it would print 1 because then var would be declared in the global scope, rather than the local scope of that do block. |
|
|
| Report Abuse |
|
|
|
| 31 Oct 2013 01:29 PM |
| @dr01d: But he said 'I didn't see this happen in Lua', and I am not sure if I am missing some large part of this conversation. All my brain is thinking right now about knives, and how they are sharp. |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
| |
|
|
| 31 Oct 2013 01:31 PM |
| Without the "local" I mean. |
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
|
| 31 Oct 2013 02:16 PM |
int clicks = 0; int limit = 100; int warn = limit-1;
Why doesn't this work? Output: Error 1 A field initializer cannot reference the non-static field, method, or property
|
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 31 Oct 2013 02:19 PM |
Make limit and warn static:
private int clicks = 0; private readonly static int limit = 100; private readonly static int warn = limit - 1; |
|
|
| Report Abuse |
|
|
BruceAB12
|
  |
| Joined: 19 Jan 2012 |
| Total Posts: 3238 |
|
|
| 31 Oct 2013 02:23 PM |
| It works thanks but, I would like to know what exactly does static do? |
|
|
| Report Abuse |
|
|