Click here to Skip to main content
15,891,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have Class2, Form1 and Control1.
class Class2
{

    public int number = 0;
    public string name = "X";

    public event EventHandler DataAvailable;
    public void OnDataAvailable(EventArgs e)
    {
        if (DataAvailable != null) DataAvailable(this, e);
    }
}


1- When I press a button in Control1, i update 'number' in Class2.
2- When I press a button in Form1, i update 'number' in Class2.
3- The event from Class2 is fired every time when a button is pressed from either controls(Form1 or Control1) ; and is updating label1, also in both controls, and label1 from either controls is showing number from Class2.
Clear as mud?

How can I do this? Since Class2 must be an object in Form1 OR Control1, but not both since it will be 2 objects. Also, without making Class2 static.

Thank you.

What I have tried:

a lot of stuff since 2005..................................
Posted
Updated 19-Sep-18 6:47am
v3

1 solution

You can create a public method in Class2 which fires the event, and call it from the other locations.

Or make number a property - which it should be anyway, public fields is generally a bad idea - and fire the even when it is changed in the setter.

But to be honest, that does sound like an ass-backwards design you got there.
 
Share this answer
 
Comments
_Q12_ 19-Sep-18 13:10pm    
I made this version:

static public class Class1
{
static public Class2 c2 = new Class2();
}
and i can call c2 from either Form1 or Control1. I made it work in Control1 already. Now i am thinking how to make it work for Form1. Any ideas?
I will try your suggestion also, but i rarely work with properties.I'm not that used to them. I have no idea what are they good for, and what the've been design to do. Fields are light and simple.
I did this already and is working for Control1:
public UserControl1()
{
InitializeComponent();
Class1.c2.DataAvailable += new EventHandler(c2_DataAvailable);
}
private void button1_Click(object sender, EventArgs e)
{
Class1.c2.number++;
label1.Text = Class1.c2.number.ToString();
}

void c2_DataAvailable(object sender, EventArgs e)
{
Class1.c2.OnDataAvailable(null);
}
OriginalGriff 19-Sep-18 14:07pm    
Why are you making your classes static?
Sorry, but either I'm missing something here, or this design looks like it's been thrown together without any real thought.
_Q12_ 19-Sep-18 16:20pm    
Hmm, its not that hard what im asking. Hard is the implementation and probably impossible as I can see it so far. I give you an example: You see in a game, when you have a Main screen (Form1 in my case), where all the game is rendered, and when you click on some elements inside that Main screen, other little menus pop up (Control1,2,3etc in my case) and after changing some values there, you close those menus and the changes are reflected back in main screen(Form1). Simple, right? Also, what is happening in main screen (Form1), some values are being stored and updated somewhere, and when a menu is opened, those stored values are shown inside it. For example, upgrades of building, resource field, etc. I want to create something similar too. That's it.
OriginalGriff 20-Sep-18 3:40am    
Yes - and you are going all around the houses to do it when the simple solution is for Form2 / Control 1 to notify Form1 of a change and let it handle it. That way, the forms / controls become independent which is much better form an OOPs POV, and well as decreasing the complexity (and thus reliability and maintainability) of your whole system. Have a look at the link, and it's two "companion" tips - they cover this stuff.
_Q12_ 20-Sep-18 22:41pm    
I still dont get it. Can you be so kind to make a sample(functional) code for me? (not a pseudo code - but a very simple normal code) Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900