how do I perform an action over and over...

This is a discussion on how do I perform an action over and over... within the C# Programming forums, part of the General Programming Boards category; I have this code... Code: namespace Drug_Wars { partial class Form1 : Form { public Form1() { InitializeComponent(); int health ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    how do I perform an action over and over...

    I have this code...

    Code:
    namespace Drug_Wars
    {
        partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                int health = 100;
            }
    
            textBox_health.Text = health;
        }
    }
    I am using visual c# 2005 beta. I have created the textbox and I want to put the varible health in the textbox so it draws the variable health. But I get an error when I put textBox_health.Text = health; at that spot. Where should I put it?

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    First, your "health" has to be a class scope variable. Right now, it's scope is the constructor. Outside the constructor, no "health" exists.

    If you want an event to occur whenever "health" is changed ( for example putting the new value in the textbox ) a property would be fine. The set method could set the textbox ( for simplicity ) or call an OnHealthChanged Event. But I guess that's a bit much, so as step one, declare "health" in the scope of your form like this

    Code:
    namespace Drug_Wars
    {
        partial class Form1 : Form
        {
            private  int health = 100;
    
            public Form1()
            {
                InitializeComponent();
                textBox_health.Text = health.ToString();
            }
         }
    }
    and move your textbox setting line into a function. A class can never have a line that is not a variable declaration outside of a function.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ok I fixed the varible. But the onHealthChanged event didn't work.

    Code:
    private void onHealthChanged()
    {
          textBox2.Text = health.ToString();
    }
    maybe I did I somthing wrong cause I am new to c# but that doesn't work.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Well, you will have to implement the event yourself. Start by creating a property for Health.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ok well umm....

    creating a property huh?

    like I said I am new so umm.... how do you do that?

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    while I am on this topic, I don't want to start a new topic, I have anouther question.

    Why can't any of my friends (running windows xp and some sp and other sp2) nto able to run my programs I made with visual c#. They get an error somtimes saying missing a dll or a memory error or somthing but there computer works fine.

    Is it because I have .net framework 2.0 beta 1 and not 1.0 installed?

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    4
    Quote Originally Posted by Rune Hunter
    while I am on this topic, I don't want to start a new topic, I have anouther question.

    Why can't any of my friends (running windows xp and some sp and other sp2) nto able to run my programs I made with visual c#. They get an error somtimes saying missing a dll or a memory error or somthing but there computer works fine.

    Is it because I have .net framework 2.0 beta 1 and not 1.0 installed?
    Programs compiled with Visual C# 2005 Beta depends on .NET Framework 2.0 beta. Your friends will need to install it inorder to run the programs you compiled.

    Version 1 and 2 .NET Framework should happily co-exist on the same machine. Mine does

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    dang it! This is sooooooo not cool.

    Well it appears some stuff don't even work with visual c# 2005 beta.

    So what do you recomend that uses 1.0 framework. I mean is there anouther free version of vusual C# that uses framework 1.0?

  9. #9
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    www.icsharpcode.net/OpenSource/SD/ uses the .NET Framework 1.1 runtime (the most common version). It's open source, you might give it a try.

  10. #10
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    And there are no free versions of Visual C#, the one you have is only free at the moment because it's beta.

    My recommendation would be to get a book about C# that will teach you why what you have done isn't working. Or you might like to read this:
    http://www.softsteel.co.uk/tutorials/cSharp/cIndex.html

  11. #11
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    maybe c# isn't for me then. Cause I hate sharp devolpe. I just can't stand it. I fallowed your link and tried it again but nope, I just don't like it.

    I am guessing you eithe cna't get visual c# 2003 or it costs money then.




    EDIT: And I do have a book on c#.
    Last edited by Rune Hunter; 02-24-2005 at 07:22 PM.

  12. #12
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    A couple of thousand dollars I think. Do a search for Visual Studio 2003 60 Day Trial though, depending on where you live they may be able to post you a free trial pack.

    Don't hate C# because you don't like the IDE's, it is a very good language and if you take the time to learn it you will come to love it. And don't expect to understand it all in a few days either - I work with people who have been using it for the past two years and are still learning new things. Programmers wouldn't get paid so much if anyone could pick up Visual Studio and write MS Word in 2 days.

  13. #13
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    lol it ins't because I don't know c#. And I do love the lang.

    Just that sharp devlope gives me an error I didn't create, and I just don't like it. Maybe if the product worked right I would like it, but right now I am not sure what to do.

    I found visual c# 2003 standard for abnout 80 dallors on amazon, but does 2003 use .net 1.1? or 1.0?

  14. #14
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    does 2003 use .net 1.1? or 1.0?
    I've got a copy that appears to use 1.1, but I imagine it's quite updatable anyway, especially between minor version numbers.

  15. #15
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Just that sharp devlope gives me an error I didn't create, and I just don't like it. Maybe if the product worked right I would like it, but right now I am not sure what to do.
    Post the error and we will see.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21