Class with class properties: aObject reference not set to an instance...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vagueante
    New Member
    • Aug 2009
    • 10

    Class with class properties: aObject reference not set to an instance...

    Hi,

    I have a class in which some of the fields are also classes.

    I can put values in the "normal" properties, but in the others when o try to assign values it gives the: "Object reference not set to an instance of an object"

    But the code compiles fine.


    Code:
    dados_Defenicao = New ServiceReference1.draftClaimEntryDefinition
    
    dados_Defenicao.arCode = "123"
    dados_Defenicao.claimMarket.warrantyType = "w" - Here it gives the error
    How can i fix this ?
    Thanxs
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    When you are making your new ServiceReferenc e1.draftClaimEn tryDefinition you need for it's constructor to initialize all the classes within.

    In other words... You made a new ServiceReferenc e1.draftClaimEn tryDefinition
    but you didn't make a new .arCode class when you did that

    Comment

    • vagueante
      New Member
      • Aug 2009
      • 10

      #3
      Done.

      the second field "claimmarke t" its a class so i have to instance it
      Code:
      dados_Defenicao = New ServiceReference1.draftClaimEntryDefinition
      
      dados_Defenicao.arCode = "123"
      
      Dim clClaimMarket As New claimMarket
      clClaimMarket.warrantyType = "Warranty data"
      dados_Defenicao.claimMarket = clClaimMarket
      Now i need to iteract (loop) through all the class properties, that is easy, but some of the properties are classes, as noted before :(

      Code:
      Dim classInstance As New ServiceReference1.draftClaimEntryDefinition
              For Each PropertyItem As PropertyInfo In classInstance.GetType().GetProperties()
                  Dim strPropName As String = String.Empty
                  strPropName = PropertyItem.Name
              Next
      My problem is: I will receive a xml file, and have to get the data in a class, but as mentioned before the class has a lot of classes as properties, and i want do do it without hardcode, I want to loop the class properties names and compare them to the xml tags (it haves the same name as the class properties), and put the values from the xml file in an instance object of the class.

      Any help?

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        I think you are over thinking this... making it harder than need be.

        In your parent class...
        just instance each of the contained classes.

        Code:
        public class Daddy
        {
            SON mySon = new SON();
            DOG myDog = new DOG();
            CAT myCat = new Cat();
        
             public Daddy()
             {
             }
            
            public class SON
            {
            }
        
            public class DOG
            {
            }
        
            public class CAT
            {
            }
        
        }

        Comment

        • vagueante
          New Member
          • Aug 2009
          • 10

          #5
          The problem is that it's not my class, but the class created by a wsdl when i had it as a service reference.
          I didn't want to change the original class

          Comment

          Working...