Jump to content
  • Advertisement
Sign in to follow this  
MarcoKotrotsos

C# Newby question

This topic is 4863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Goodevening, I am wondering if someone can help me with this. I am not new to programming but i alway's programmed outside the OO paradigm. Now i want to (need to) learn oo and i am using c# to start. I have an object that i wish to create but it is created outside the calling class (say, in form_load) how can I access the methods of that class in other classes (like in another form) I hope you understand, is there a way to create an object globally... What i want is (it's just a test) that I have this form, it has a textbox and a button. Everytime i click that button it increments by one. I have a form and a button

public void button1_Click(object sender, EventArgs e)
        {

            
            Counter icounter = new Counter();
            string text;
            int counter;

            counter = icounter.Number;
            counter++;
            
            text = Convert.ToString(counter).ToString();
         
            testTextbox1.Text = text;
            


        }

and i have a class (just a plain o'll class here)
public class Counter
    {
        public int number;

        public int Number
        {
            get
            {
                
                return number;
                
            }
            set
            {
         
                number = value;
                

            }
        }
        
    }

now this is really stupid coding probably (and i know why it doesnt work) . But can someone explain to me how I can declare an object when the form loads, and execute an increment (using the class) a value by one everytime i click a button.... I am really, really curious. Hope it's not too newby.:) Marco

Share this post


Link to post
Share on other sites
Advertisement
make the counter a class variable, for instance:

public Counter counter = new Counter();

void Form_load(...) {
counter.Number++;
}

void Button1_click(object sender, EventArgs args) {
counter.Number++;
testTextbox1.Text = "" + counter.Number;
}

Share this post


Link to post
Share on other sites
You declare them at the class level:

public class MyForm : Form {
private Counter counter = new Counter();

MyForm() {
... stuff ...
}

... other things ...
}

for instance.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!