locked
partial class does not initialize class scope variable.

    Question

  • Hello

    I have a question. I have a partial class in two .cs files. One of them is generated by a web service proxy as "reference.cs" and other one i added in order to implement class behaviour. But class scope variables in the second file is not initialized. Some code here:


        public partial class ListSelector
        {
            GorevveIsTakipServisiSoapClient svc = new GorevveIsTakipServisiSoapClient();
            string simplevar = string.Empty;
            public void kol()
            {
                //following line generates null exception in sv variable. In addition, simplevar variable seems null too when i check it.
                svc.ListPairOfIntStringCompleted += new EventHandler<ListPairOfIntStringCompletedEventArgs>(svc_ListPairOfIntStringCompleted);
            }
            void svc_ListPairOfIntStringCompleted(object sender, ListPairOfIntStringCompletedEventArgs e)
            {
                //...
            }
        }


    This is not a problem but just wondering. Thanks!
    Friday, September 25, 2009 8:50 AM

Answers

  • Is this perhaps an object created by WCF? (i.e. returned from a method etc). WCF (specifically, `DataContractSerializer`) does not call any constructor (strange but true). To do any extra initialization of a WCF object (usually data-contract etc), you must use a serialization callback; i.e. `[OnDeserialized]`
    Marc [C# MVP]
    • Marked as answer by fatih ozturk Friday, September 25, 2009 10:12 AM
    Friday, September 25, 2009 9:29 AM

All replies

  • Is this perhaps an object created by WCF? (i.e. returned from a method etc). WCF (specifically, `DataContractSerializer`) does not call any constructor (strange but true). To do any extra initialization of a WCF object (usually data-contract etc), you must use a serialization callback; i.e. `[OnDeserialized]`
    Marc [C# MVP]
    • Marked as answer by fatih ozturk Friday, September 25, 2009 10:12 AM
    Friday, September 25, 2009 9:29 AM
  • Thanks Marc.
    Friday, September 25, 2009 10:21 AM