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. Visual Basic
  4. Array of an Object

Array of an Object

Scheduled Pinned Locked Moved Visual Basic
helpdata-structuresbusinessquestion
5 Posts 4 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.
  • C Offline
    C Offline
    codeamin
    wrote on last edited by
    #1

    Hello there, I have a Requirement class. I wanted to instantiate a number of objects of the same class. So I created a array of Requirement object. Here is what I had: dim req() as Requirements Private sub CreateRequirements(num as Integer) for i as integer=0 to num req(i) = new Requirements next end sub Above code gives an error like "Object reference not set to an instance of an object". Can someone help me out? Thank you, Bashar Amin Sr. Software Engineer

    M A 2 Replies Last reply
    0
    • C codeamin

      Hello there, I have a Requirement class. I wanted to instantiate a number of objects of the same class. So I created a array of Requirement object. Here is what I had: dim req() as Requirements Private sub CreateRequirements(num as Integer) for i as integer=0 to num req(i) = new Requirements next end sub Above code gives an error like "Object reference not set to an instance of an object". Can someone help me out? Thank you, Bashar Amin Sr. Software Engineer

      M Offline
      M Offline
      MohammadAmiry
      wrote on last edited by
      #2

      You should Redim the array first:

      dim req() as Requirements
      
      Private sub CreateRequirements(num as Integer)
      Redim req(num) as Requirements
      for i as integer=0 to num
      req(i) = new Requirements
      next
      
      end sub
      
      C 1 Reply Last reply
      0
      • M MohammadAmiry

        You should Redim the array first:

        dim req() as Requirements
        
        Private sub CreateRequirements(num as Integer)
        Redim req(num) as Requirements
        for i as integer=0 to num
        req(i) = new Requirements
        next
        
        end sub
        
        C Offline
        C Offline
        codeamin
        wrote on last edited by
        #3

        Thank you. It solves the problem but does not need "as Requirements" for the Redim statement. I have another question. If you do not know the size at a time and I want to create the Requirement object dynamicly, do I need the Redim statement? Bashar Amin Sr. Software Engineer

        Q 1 Reply Last reply
        0
        • C codeamin

          Thank you. It solves the problem but does not need "as Requirements" for the Redim statement. I have another question. If you do not know the size at a time and I want to create the Requirement object dynamicly, do I need the Redim statement? Bashar Amin Sr. Software Engineer

          Q Offline
          Q Offline
          Qhalis
          wrote on last edited by
          #4

          Just curious: Do you have a hard reason why you need to use an array as opposed to an arraylist? It would handle all the resizing issues for you and, if needed, you can always generate an array from the ArrayList using ToArray(). Q.

          1 Reply Last reply
          0
          • C codeamin

            Hello there, I have a Requirement class. I wanted to instantiate a number of objects of the same class. So I created a array of Requirement object. Here is what I had: dim req() as Requirements Private sub CreateRequirements(num as Integer) for i as integer=0 to num req(i) = new Requirements next end sub Above code gives an error like "Object reference not set to an instance of an object". Can someone help me out? Thank you, Bashar Amin Sr. Software Engineer

            A Offline
            A Offline
            AhClem
            wrote on last edited by
            #5

            This statement: dim req() as Requirements only declares a reference to an array of type Requirements. It doesn't instantiate it. You need to initialize the reference to an instance of an array of that type before you can start assigning values to it. Will

            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