Click here to Skip to main content
15,916,600 members
Home / Discussions / C#
   

C#

 
GeneralRe: Access controls from different class Pin
soulidentities17-May-09 20:16
soulidentities17-May-09 20:16 
GeneralRe: Access controls from different class Pin
Ravi Bhavnani17-May-09 20:35
professionalRavi Bhavnani17-May-09 20:35 
GeneralRe: Access controls from different class Pin
soulidentities17-May-09 21:13
soulidentities17-May-09 21:13 
GeneralRe: Access controls from different class Pin
Ravi Bhavnani17-May-09 21:18
professionalRavi Bhavnani17-May-09 21:18 
GeneralRe: Access controls from different class Pin
soulidentities17-May-09 21:30
soulidentities17-May-09 21:30 
AnswerRe: Access controls from different class Pin
DaveyM6917-May-09 22:52
professionalDaveyM6917-May-09 22:52 
GeneralRe: Access controls from different class Pin
soulidentities18-May-09 0:01
soulidentities18-May-09 0:01 
GeneralRe: Access controls from different class Pin
DaveyM6918-May-09 1:19
professionalDaveyM6918-May-09 1:19 
Class1 is doing the job with controls which it doesn't own - a far from ideal situation. It's a quick and dirty solution, but I advise you to avoid it.

An event driven system is much better and not difficult. Simple example below:
// Class1.cs
using System;

public class Class1
{
    public event EventHandler<MyEventArgs> MyEvent;

    // Method for demo only
    public void PerformMyMethod(string item)
    {
        OnMyEvent(new MyEventArgs(item));
    }

    protected virtual void OnMyEvent(MyEventArgs e)
    {
        EventHandler<MyEventArgs> eh = MyEvent;
        if (eh != null)
            eh(this, e);
    }
}
// MyEventArgs.cs
public class MyEventArgs : EventArgs
{
    private string _MyString;

    public MyEventArgs(string myString)
    {
        _MyString = myString;
    }

    public String MyString
    {
        get { return _MyString; }
    }
}
// Form1.cs
using System;
using System.Windows.Forms;

public partial class Form1 : Form
{
    Class1 class1;

    public Form1()
    {
        InitializeComponent();
        class1 = new Class1();
        class1.MyEvent += new EventHandler<MyEventArgs>(class1_MyEvent);
        class1.PerformMyMethod("Hello World");
    }

    void class1_MyEvent(object sender, MyEventArgs e)
    {
        MessageBox.Show(string.Format(
            "{0} from {1}!", e.MyString, sender));
    }
}


Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)

GeneralRe: Access controls from different class Pin
soulidentities18-May-09 5:43
soulidentities18-May-09 5:43 
GeneralRe: Access controls from different class Pin
DaveyM6918-May-09 6:30
professionalDaveyM6918-May-09 6:30 
Question.NET Framework Version Pin
Blue36517-May-09 15:06
Blue36517-May-09 15:06 
AnswerRe: .NET Framework Version Pin
PIEBALDconsult17-May-09 16:14
mvePIEBALDconsult17-May-09 16:14 
GeneralRe: .NET Framework Version Pin
adatapost17-May-09 17:20
adatapost17-May-09 17:20 
GeneralRe: .NET Framework Version Pin
PIEBALDconsult17-May-09 18:00
mvePIEBALDconsult17-May-09 18:00 
AnswerRe: .NET Framework Version Pin
adatapost17-May-09 20:59
adatapost17-May-09 20:59 
AnswerRe: .NET Framework Version Pin
DaveyM6917-May-09 22:58
professionalDaveyM6917-May-09 22:58 
GeneralRe: .NET Framework Version Pin
Blue36517-May-09 23:25
Blue36517-May-09 23:25 
GeneralRe: .NET Framework Version Pin
PIEBALDconsult18-May-09 4:09
mvePIEBALDconsult18-May-09 4:09 
Generalget complex type from nusoap Pin
jahc17-May-09 14:36
jahc17-May-09 14:36 
Questioneasy wait(delay) like an "watabletimer & waitforsingleobjectex" Pin
Bernard3817-May-09 13:27
Bernard3817-May-09 13:27 
AnswerRe: easy wait(delay) like an "watabletimer & waitforsingleobjectex" Pin
Luc Pattyn17-May-09 16:11
sitebuilderLuc Pattyn17-May-09 16:11 
QuestionResizable control at runtime. Pin
stardust161117-May-09 9:58
stardust161117-May-09 9:58 
QuestionUsing Exponentiation Operator ^ on Deciaml Pin
GregoryWB17-May-09 9:41
GregoryWB17-May-09 9:41 
AnswerRe: Using Exponentiation Operator ^ on Deciaml Pin
Luc Pattyn17-May-09 9:51
sitebuilderLuc Pattyn17-May-09 9:51 
GeneralRe: Using Exponentiation Operator ^ on Deciaml [modified] Pin
GregoryWB17-May-09 10:12
GregoryWB17-May-09 10:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.