none
Few fundamental C# questions

    Question


  • I'm a fairly good user of C# but I'm new to CRM and the SDK. As a result, I'm on a mission to learn as much as I can to help me so I have these questions:


    1) How can I pass parameters into an event handler? I'm using .NET 1.1. The prob;em is having an event handler which calls a function which takes parameters.

    2) (CRM SDK specific) I need to retrieve columns from 2 entities - contact (multiple) and account (single). What I need to do is write these to a csv file. I can open the file but, as I have stored each column attribute into a string variable (all columns retrieved being of type string), the file just says system.string[] (the type).

    How can I actually write the real value of each attribute into the file (ie for phone nu mber, 020348384 or whatever it may be). As I store the columnset into a string variable I only get the methods and properties of a string. I'm fine with the streamwriter part of this, but not the retrieval.

    I know this is to do with using the businessentitycollection object and iterating through this but despite everything I've tried I've been unsuccessful.

    3) In a retrieve single, I need to set the GUID. Problem is, the ID for each entity is of type Key but I want a GUID. How can I successfully cast this into GUID and then pass it into the retrieve statement? I know it's all to do with casting but this is one of my weak areas.

    I keep getting object reference not set to an instance of an object errors. Do I also have to pass the var as a function parameter? Which brings me back nicely to point 1...

    I will post code samples up to aid these questions, soon.

    Thanks
    Friday, February 08, 2008 9:24 PM

Answers

  • Hi,

     

    1) Pass the arguments to the event handler when you raise the event, as demonstrated in the following sample:

     

            static void Main(string[] args)

            {

                TestClass testObj = new TestClass();

                testObj.EventName += new EventNameEventHandler(testObj_EventName);

                testObj.TestMethod();

            }

     

            //An event handler

            static void testObj_EventName(object sender, Program.EventNameEventArgs e)

            {

                //Output the event argument e.TestArg

                if (!string.IsNullOrEmpty(e.TestArg))

                {

                    Console.WriteLine(e.TestArg);

                }

            }

     

            //The event delegate

            public delegate void EventNameEventHandler(object sender, EventNameEventArgs e);

     

            //The event arguments

            public class EventNameEventArgs : EventArgs

            {

                //An argument which is passed to the event handlers.

                public string TestArg;

            }

     

            public class TestClass

            {

                //The event

                public event EventNameEventHandler EventName;

     

                //Raise the event within the class when needed.

                private void RaiseEventName(EventNameEventArgs e)

                {

                    if (EventName != null)

                    {

                        EventName(this, e);

                    }

                }

     

                public void TestMethod()

                {

                    //The arguments to be passed to eventhandlers.

                    EventNameEventArgs e = new EventNameEventArgs();

                    e.TestArg = "This is a test argument.";

     

                    RaiseEventName(e);

                }

            }

     

     

    If you have trouble understanding the concepts please read this link:

      http://msdn2.microsoft.com/en-us/library/17sde2xt.aspx

     

    2) Please go to the Microsoft Dynamics CRM newsgroup where you can get better suggestions:

      http://www.microsoft.com/Businesssolutions/Community/Newsgroups/dgbrowser/en-us/default.mspx?dg=microsoft.public.crm&lang=en&cr=US

     

    3) Sorry I could not understand what you intended to do. If it is related to CRM ask it in the newsgroup too.

     

    Best Regards

    Chunsheng Tang

     

    Tuesday, February 12, 2008 4:57 AM

All replies

  • Hi,

     

    1) Pass the arguments to the event handler when you raise the event, as demonstrated in the following sample:

     

            static void Main(string[] args)

            {

                TestClass testObj = new TestClass();

                testObj.EventName += new EventNameEventHandler(testObj_EventName);

                testObj.TestMethod();

            }

     

            //An event handler

            static void testObj_EventName(object sender, Program.EventNameEventArgs e)

            {

                //Output the event argument e.TestArg

                if (!string.IsNullOrEmpty(e.TestArg))

                {

                    Console.WriteLine(e.TestArg);

                }

            }

     

            //The event delegate

            public delegate void EventNameEventHandler(object sender, EventNameEventArgs e);

     

            //The event arguments

            public class EventNameEventArgs : EventArgs

            {

                //An argument which is passed to the event handlers.

                public string TestArg;

            }

     

            public class TestClass

            {

                //The event

                public event EventNameEventHandler EventName;

     

                //Raise the event within the class when needed.

                private void RaiseEventName(EventNameEventArgs e)

                {

                    if (EventName != null)

                    {

                        EventName(this, e);

                    }

                }

     

                public void TestMethod()

                {

                    //The arguments to be passed to eventhandlers.

                    EventNameEventArgs e = new EventNameEventArgs();

                    e.TestArg = "This is a test argument.";

     

                    RaiseEventName(e);

                }

            }

     

     

    If you have trouble understanding the concepts please read this link:

      http://msdn2.microsoft.com/en-us/library/17sde2xt.aspx

     

    2) Please go to the Microsoft Dynamics CRM newsgroup where you can get better suggestions:

      http://www.microsoft.com/Businesssolutions/Community/Newsgroups/dgbrowser/en-us/default.mspx?dg=microsoft.public.crm&lang=en&cr=US

     

    3) Sorry I could not understand what you intended to do. If it is related to CRM ask it in the newsgroup too.

     

    Best Regards

    Chunsheng Tang

     

    Tuesday, February 12, 2008 4:57 AM
  • Thanks for this.

    That works, but why do we have to pass arguments to an event in such a long winded way?

    Now I know why ASP.NET MVC disables events.
    Tuesday, April 15, 2008 5:55 PM
  • That's the pattern to use the events and it is refined. You can make use of the EventHandler<TEventArgs> generic delegate to make the code a little simpler.

    Wednesday, April 16, 2008 2:40 AM