Results 1 to 6 of 6

Thread: Collection?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Collection?

    I have individual objects that have multiple variables.

    Eg. ObjName, XPosition, YPosition, ObjAngle

    I think it may be a collection that I need to use but I don't know how to use one. Hoping someone can give me an example.

    Looking at MSDN is a collection only 2D? Ie one variable, one listing?

    Basically I want a "table" to lookup. One variable, with a list of attributes to that variable.

    Previously I've created a bunch of 2d arrays but as you can't redim preserve the first part of the 2d array it's hard to add extra data.

    Might be wrong in what I'm saying but hoping someone can point me in the right direction.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Collection?

    Are you by any chance refering to ENUM?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Collection?

    A Collection is just a list (1D, just like MyArray(10) is), and is not what you want.

    koolsid is on the right lines, but I don't think an Enum is what you are after - I think you want a User Defined Type (UDT), which would be something like this:
    Code:
      'in the Declarations section
    Type MyObject
      ObjName as String
      XPosition as Integer
      YPosition as Integer
      ObjAngle as Single
    End Type
    
      'whereever you want to declare the variable(s)
    Dim SingleObject as MyObject
    
      'to use the variable(s)
    SingleObject.ObjName = "sid"
    SingleObject.XPosition = 0
    SingleObject.YPosition = 2
    SingleObject.ObjAngle = 0
    You can also create/use an array, just as you would with any other data type - and as you only need the array to be 1D, you can ReDim Preserve if needed.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Collection?

    Si,
    Hello again. Seems to be what I need but how do I define the UDT as an array? Might be correct but the following doesn't seem to autoprompt as vb normally does. Hard to explain what I'm talking about though.

    vb Code:
    1. 'in the Declarations section
    2. Type MyObject
    3.   ObjName() As String
    4.   XPosition() As Integer
    5.   YPosition() As Integer
    6.   ObjAngle() As Single
    7. End Type
    8.  
    9.   'whereever you want to declare the variable(s)
    10. Dim SingleObject As MyObject
    11.  
    12.   'to use the variable(s)
    13. SingleObject.ObjName(i) = "sid"
    14. SingleObject.XPosition(i) = 0
    15. SingleObject.YPosition(i) = 2
    16. SingleObject.ObjAngle(i) = 0

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Collection?

    What you did is valid, but not what you wanted - that creates a structure containing arrays, rather than an array containing a structure.

    Use the Type declaration as I showed, and change the other parts like this:
    Code:
    Dim ArrayOfObject() as MyObject
    
    ReDim ArrayOfObject(0) as MyObject
    
    ArrayOfObject(0).ObjName = "sid"
    ArrayOfObject(0).XPosition = 0
    ArrayOfObject(0).YPosition = 2
    ArrayOfObject(0).ObjAngle = 0
    You could also use a With block, eg:
    Code:
    With ArrayOfObject(0)
      .ObjName = "sid"
      .XPosition = 0
      .YPosition = 2
      .ObjAngle = 0
    End With

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Collection?

    Thankyou. Made things so much easier and simpler!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width