Results 1 to 2 of 2

Thread: null Delegate/event

  1. #1

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    null Delegate/event

    All,

    Can you help? I am kinda new to C#, a long-time VB guy.

    I have nearly identical code in another project, but this code (kinda copied-pasted) is not working.

    The event "MyDataChanged" is always null when I try to call it. Cannot figure out why?

    Thanks!

    Dave
    Code:
    namespace OPC_Process_Driver
    {
        public partial class frmMain : Form
        {
            private OPC_Process.cls_OPC_Group m_OPC_Process;
    
            // dhs - added delegate for VB Event consumption
            public delegate void theDataChanged(object source, string strContext, string strTagName, string strTagValue);
            public event theDataChanged MyDataChanged;
            public void NotifyDataChanged(string strContext, string strTagName, string strTagValue)
            {
                if (MyDataChanged != null)
                {
                    MyDataChanged(this, strContext, strTagName, strTagValue);
                    // THIS IS ALWAYS NULL!!!            }
            }
    
            public frmMain()
            {
                InitializeComponent();
                m_OPC_Process = new cls_OPC_Group();
                m_OPC_Process.DataChanged += new __cls_OPC_Group_DataChangedEventHandler(this.HandleDataChanged);
            }
            private void HandleDataChanged(ref string Context, ref string TagName, ref string TagValue)
            {
                // Handle the event
                this.NotifyDataChanged(Context, TagName, TagValue); 
            }
    
            private void btn_TestDataChanged_Click(object sender, EventArgs e)
            {
                NotifyDataChanged("Context", "TagName", "TagValue");
            }
    
            private void btn_InitOPC_Click(object sender, EventArgs e)
            {
                string server, context, refresh;
                server = "SWToolbox.TOPServer";
                context = "Channel_1";
                refresh = "100";
                m_OPC_Process.Initialize_OPC(ref server, ref context, ref refresh);
            }
    
            private void btn_AddItem_Click(object sender, EventArgs e)
            {
                string context, tagname;
                context = "Channel_1";
                tagname = "Device_1.HeartBeat";
                m_OPC_Process.AddListener(ref context, ref tagname);
            }
    
    
        }
    }
    I'm trying to do something like this but it won't compile:

    Code:
            public frmMain()
            {
                InitializeComponent();
                m_OPC_Process = new cls_OPC_Group();
                m_OPC_Process.DataChanged += new __cls_OPC_Group_DataChangedEventHandler(this.HandleDataChanged);
                 this.MyDataChanged += new System.EventHandler(MyDataChanged);        
    }
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  2. #2

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: null Delegate/event

    OK I was able to get past this using this code:

    Code:
            public frmMain()
            {
                InitializeComponent();
                m_OPC_Process = new cls_OPC_Group();
                m_OPC_Process.DataChanged += new __cls_OPC_Group_DataChangedEventHandler(this.HandleDataChanged);
                this.MyDataChanged += new theDataChanged(frmMain_MyDataChanged);
            }
    
            private void frmMain_MyDataChanged(object source, string strContext, string strTagName, string strTagValue)
            {
                this.Text = strTagValue;
            }
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width