Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. null issue before initializing object

null issue before initializing object

Scheduled Pinned Locked Moved C#
helpquestionlounge
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    spif2001
    wrote on last edited by
    #1

    I have a class like the following. The DataClass2 type is also derived from ASuperObject. public class DataClass1 : ASuperObject { private DataClass2 dataClass2; public DataClass1(){} public DataClass2 MyDataClass2 { get { if(this.dataClass2 == null) this.dataClass2 = DataClass2Holder.Instance.Dummy; return this.dataClass2; } set{this.dataClass2 = value;} } } From a function I fool around with an instance of DataClass1 and at some time I use the MyDataClass2 property, to get the dataClass2 value. The dataClass2 has not been instantiated yet, so the 'if' clause sets the value. Now the function that called the property would like to know, if the 'if' clause was invoked or not (see code below)- can this be done? int localCount = 1; PropertyInfo[] properties = this.GetType().GetProperties(); foreach(PropertyInfo property in properties) { if(property.PropertyType.BaseType == typeof(ASuperObject)) { //***The following call needs to know if the property was null*** ((ASuperObject)property.GetValue(this,null)).nameAddition += localCount+"_"; //Recursive call following ((ASuperObject)property.GetValue(this,null)).InitializeHierarchy(); localCount++; } } (it must be general and not just a bool being set in DataClass1)

    C R 2 Replies Last reply
    0
    • S spif2001

      I have a class like the following. The DataClass2 type is also derived from ASuperObject. public class DataClass1 : ASuperObject { private DataClass2 dataClass2; public DataClass1(){} public DataClass2 MyDataClass2 { get { if(this.dataClass2 == null) this.dataClass2 = DataClass2Holder.Instance.Dummy; return this.dataClass2; } set{this.dataClass2 = value;} } } From a function I fool around with an instance of DataClass1 and at some time I use the MyDataClass2 property, to get the dataClass2 value. The dataClass2 has not been instantiated yet, so the 'if' clause sets the value. Now the function that called the property would like to know, if the 'if' clause was invoked or not (see code below)- can this be done? int localCount = 1; PropertyInfo[] properties = this.GetType().GetProperties(); foreach(PropertyInfo property in properties) { if(property.PropertyType.BaseType == typeof(ASuperObject)) { //***The following call needs to know if the property was null*** ((ASuperObject)property.GetValue(this,null)).nameAddition += localCount+"_"; //Recursive call following ((ASuperObject)property.GetValue(this,null)).InitializeHierarchy(); localCount++; } } (it must be general and not just a bool being set in DataClass1)

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      spif2001 wrote: it must be general and not just a bool being set in DataClass1 No - you have encapsulated the dataClass2 member variable and only permit access throught the MyDataClass2 property (which could do anything it liked). As the MyDataClass2 property is designed to always return a DataClass2 object (or one derived from it) then you can never know by what the state of the member variable was before.


      My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

      1 Reply Last reply
      0
      • S spif2001

        I have a class like the following. The DataClass2 type is also derived from ASuperObject. public class DataClass1 : ASuperObject { private DataClass2 dataClass2; public DataClass1(){} public DataClass2 MyDataClass2 { get { if(this.dataClass2 == null) this.dataClass2 = DataClass2Holder.Instance.Dummy; return this.dataClass2; } set{this.dataClass2 = value;} } } From a function I fool around with an instance of DataClass1 and at some time I use the MyDataClass2 property, to get the dataClass2 value. The dataClass2 has not been instantiated yet, so the 'if' clause sets the value. Now the function that called the property would like to know, if the 'if' clause was invoked or not (see code below)- can this be done? int localCount = 1; PropertyInfo[] properties = this.GetType().GetProperties(); foreach(PropertyInfo property in properties) { if(property.PropertyType.BaseType == typeof(ASuperObject)) { //***The following call needs to know if the property was null*** ((ASuperObject)property.GetValue(this,null)).nameAddition += localCount+"_"; //Recursive call following ((ASuperObject)property.GetValue(this,null)).InitializeHierarchy(); localCount++; } } (it must be general and not just a bool being set in DataClass1)

        R Offline
        R Offline
        Robert Rohde
        wrote on last edited by
        #3

        1. Solution: Dont return a DataClass2 object but a structure with a DataClass2 and a boolean value (would require to change the type of the property). 2. Solution: Instead of making a property make a method like: public DataClass2 GetMyDataClass2(out bool tookedDummyObject) { if(this.dataClass2 == null) { this.dataClass2 = DataClass2Holder.Instance.Dummy; tookedDummyObject = true; } else { tookedDummyObject = false; } return this.dataClass2; } If you still want the property to be present (for other callers who do not need the extra info) it could be implemented like: get { bool notNeeded; return GetMyDataClass2(out notNeeded) }

        S 1 Reply Last reply
        0
        • R Robert Rohde

          1. Solution: Dont return a DataClass2 object but a structure with a DataClass2 and a boolean value (would require to change the type of the property). 2. Solution: Instead of making a property make a method like: public DataClass2 GetMyDataClass2(out bool tookedDummyObject) { if(this.dataClass2 == null) { this.dataClass2 = DataClass2Holder.Instance.Dummy; tookedDummyObject = true; } else { tookedDummyObject = false; } return this.dataClass2; } If you still want the property to be present (for other callers who do not need the extra info) it could be implemented like: get { bool notNeeded; return GetMyDataClass2(out notNeeded) }

          S Offline
          S Offline
          spif2001
          wrote on last edited by
          #4

          Good solutions there, but I have to minimize the code added to the ASuperObject sub-classes. Ended up with a boolean solution after all, namely adding a bool isDummy to the ASuperObject abstract class. Then in my DataClass2Holder(Singleton) i set the bool after the Dummy DataClass2 has been instantiated. Then I don't need to alter the property impl. in DataClass1 and the Dummy becomes semi-invisible. Hope I find a better solution along the way, but for now it will have to do. Thanks for the help you both. -spif2001

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups