8 Replies - 426 Views - Last Post: 18 January 2013 - 08:08 AM Rate Topic: -----

#1 Danikashi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 11-May 12

[WinForms] Value goes missing

Posted 18 January 2013 - 02:20 AM

I have a function that increases/decreases each time it runs depending in the situation, and after it runs the function the value goes missing.
Its supposed to return a value int that adds to a global var but it doesnt, do you have any ideia why this happens?
Are global vars and functions not able to work together?
Is This A Good Question/Topic? 0
  • +

Replies To: [WinForms] Value goes missing

#2 b0zhidar  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 21-December 12

Re: [WinForms] Value goes missing

Posted 18 January 2013 - 02:29 AM

If you posted any code it would be much easier to help you, like this, we have no idea how your code looks.
Was This Post Helpful? 0
  • +
  • -

#3 Danikashi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 11-May 12

Re: [WinForms] Value goes missing

Posted 18 January 2013 - 02:36 AM

View Postb0zhidar, on 18 January 2013 - 02:29 AM, said:

If you posted any code it would be much easier to help you, like this, we have no idea how your code looks.

My bad.
    public int Numbers(Label lbl, int staph, int k)
        {
            int kk;
            if (lbl.Text == "X")
            {
                lbl.BackColor = Color.Transparent;
                lbl.Text = " ";

                staph--;
                MessageBox.Show(cont.ToString());
            }
            else
                if (cont == k)
                {
                    if (k == 5)
                    {
                        MessageBox.Show("Ja inseriu 5 Numeros", "Warning");
                    }
                    else
                    {
                        MessageBox.Show("Ja inseriu 2 Estrelas", "Warning");
                    }
                }
                else
                    if (lbl.Text != "X")
                    {
                        lbl.BackColor = Color.White;
                        lbl.Text = "X";
                        staph++;
                        MessageBox.Show(staph.ToString());
                    }
            kk = staph;
            return kk;
}

Was This Post Helpful? 0
  • +
  • -

#4 b0zhidar  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 21-December 12

Re: [WinForms] Value goes missing

Posted 18 January 2013 - 02:48 AM

So where's the problem?
Where the value goes missing?
Have you tried to debug?
Also why are you doing
 
int kk;
kk=staph;
return kk;


When you can simply
return staph


Plus, I am guessing cont is 5, if it is why are you doing
  if (cont == k)	              
  {	                
    if (k == 5)	        
            {


Only one if statement is enough.

This post has been edited by b0zhidar: 18 January 2013 - 02:52 AM

Was This Post Helpful? 0
  • +
  • -

#5 Danikashi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 11-May 12

Re: [WinForms] Value goes missing

Posted 18 January 2013 - 04:59 AM

Yeah i know i could do "return staph" only... it was to see if it was any return error. Yeah i tried using debug still couldnt find it, it simply resets "cont" to 0.
After i call the function and it "returns" the value the cont resets back to 0.
It can be both 5 or 2 depending on the parameter you use in the function, i'm doing "euromillions".

This post has been edited by tlhIn`toq: 18 January 2013 - 07:49 AM

Was This Post Helpful? 0
  • +
  • -

#6 b0zhidar  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 21-December 12

Re: [WinForms] Value goes missing

Posted 18 January 2013 - 05:10 AM

Can you tell me what are you trying to write so I can do it for you? Because I don't see anything in that code that would reset cont to 0.
Let me know mate.
Was This Post Helpful? 0
  • +
  • -

#7 Danikashi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 11-May 12

Re: [WinForms] Value goes missing

Posted 18 January 2013 - 05:19 AM

I'm testing a label, if that label has an blank space(" ") in it, it changes the label text to an "X" the counter goes +1 and when it reaches 5 or 2 depending if i'm testing for the stars(2) or the numbers(5), it pops up a messagebox saying so.
I don't see any error on the one i wrote either, maybe its because global vars and functions have conflits?

This post has been edited by tlhIn`toq: 18 January 2013 - 07:49 AM

Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5595
  • View blog
  • Posts: 11,970
  • Joined: 02-June 10

Re: [WinForms] Value goes missing

Posted 18 January 2013 - 08:02 AM

b0zhidar: Please don't offer to just write code for posters. We are here to help them learn to write their own code. It sets a terrible precedent to do it for them. We bounce messages where people are just asking for code. It often seems easier to just do it for them, but that's not what we do here.

View PostDanikashi, on 18 January 2013 - 06:19 AM, said:

maybe its because global vars and functions have conflits?


Its probably a matter of scope. Variables within a method are cleared once the method is finished. Your method returns an int. Anything else is meaningless. All you should care about is the returned value.

Its what we call "scope". A thing exists between the {} it was created in. So if you make a thing within a for{} loop, that's its scope. If you want to access a thing for the entire class, then define it in the class braces

class someClass
{
   int yogi = 5; // Accessible to the class
   
   void someMethod()
   {
        int booboo = 3; // Accessible to the method

        while(true)
        {
           string Ranger = "Smith"; // Accessible to the loop
        }
   }
}



Some of my common tips (some may apply more than others to your specific style):
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attempts below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.


Q: I do x and y happens which I didn't expect but I don't know how to figure out why. How do I debug and find my problem?
A:

This post has been edited by tlhIn`toq: 18 January 2013 - 08:02 AM

Was This Post Helpful? 3
  • +
  • -

#9 b0zhidar  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 21-December 12

Re: [WinForms] Value goes missing

Posted 18 January 2013 - 08:08 AM

tlhIn`toq: Sorry mate, I am trying to get used to the forum, I am still new as you can see. And that was awesome reply!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1