Results 1 to 21 of 21

Thread: About array

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    About array

    Hi!

    I declare an array like;

    Dim myarray() as string.

    But i want to declare like control arrays.

    example

    for x = 1 to 5
    dim myarray(x) () as string
    next

    i want to add winsock data to these arrays.





    is it possible

    sorry for English

    Best regards

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    4,685

    Re: About array

    Use a user defined type:
    Code:
    Private Type tpStringArray
      aValues()
    End Type
    
    Private Sub MySub()
      Dim tStringArray() As tpStringArray
      Dim i As Long
    
      ReDim tStringArray(9)
      For i = 0 to 9
        ReDim tStringArray(i).aValues(5)
      Next i
    End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: About array

    Arnoutdv

    Thank you very much.

    I will try

    thank you

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,751

    Re: About array

    I'd avoid the clunky, obsolete, and hard to work with UDT. You can do this using Variant data instead:

    Code:
    Option Explicit
    
    Private myArray() As Variant
    
    Private Sub MySub()
        Dim Stringss() As String
        Dim I As Long
    
        ReDim myArray(9)
        ReDim Strings(5)
        For I = 0 To 9
            myArray(I) = Strings
        Next
    End Sub
    
    Private Sub Form_Load()
        MySub
        myArray(0)(5) = "Hello"
        myArray(9)(5) = "World"
        Debug.Print myArray(0)(5), myArray(9)(5)
    End Sub

  5. #5
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    4,685

    Re: About array

    Obsolete??

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: About array

    Hi Arnoutdv

    Thank you very much.

    how can i use this code in winsock dataarrival event?


    Best regards
    Last edited by jajiyiko; Jul 24th, 2021 at 10:32 AM.

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,751

    Re: About array

    Yes obsolete.

    The only real purpose left to UDTs is as a stand-in for a C struct. Aside from that they are only in VB6 at all in order to support legacy I/O used by earlier incarnations of Microsoft Basic.

    It is generally best to avoid them at all costs. They can't be marshaled anyway unless defined in a published typelib.

  8. #8
    PowerPoster
    Join Date
    Feb 2017
    Posts
    3,372

    Re: About array

    Obsolete: not in use any more, having been replaced by something newer and better or more fashionable.

    I prefer to use classes, because with UDTs as long as you need to do somthing more than just storing values, you can't.

    Also when you need to pass the data to other part of the program you can get an error "Only public user defined types defined in public object modules can be used as parameters or return types for public procedures of class modules or as fields of public user defined types"

    But classes have an overhead, so I think that UDTs still have their place for processing/memory intensive algorythms when you want to optimize the speed and/or memory as much as you can.

    Also when you need to store a few variables (not many) that must work toghether I prefer to use individual variables (or individual arrays). But I think that that's has to do with the way that we are used to do these things, UDTs also work.

    So my opinion is that they are quite obsolete... but not totally.

  9. #9
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    4,685

    Re: About array

    Sometimes Iíve to process ten thousands of records, putting them in classes would be very very slow and it takes ages to destroy them.
    Obsolete maybe for you, but not for my applications.

    Oh and VB6 is obsolete, there are much better alternatives..

  10. #10
    PowerPoster
    Join Date
    Feb 2017
    Posts
    3,372

    Re: About array

    Quote Originally Posted by Arnoutdv View Post
    Oh and VB6 is obsolete, there are much better alternatives..
    Like for example?

  11. #11

  12. #12
    Frenzied Member
    Join Date
    Jan 2020
    Posts
    1,563

    Re: About array

    Code:
    Dim a() As String
    ReDim Preserve a(3, 10)
    Dim X As Long, Y As Long
    For X = 1 To 3
        For Y = 1 To 10
            a(X, Y) = X & "," & Y
        Next
    Next
    ReDim Preserve a(3, 12)
    For X = 1 To 3
        For Y = 11 To 12
            a(X, Y) = X & "," & Y
        Next
    Next

  13. #13
    Frenzied Member
    Join Date
    Jan 2020
    Posts
    1,563

    Re: About array

    Quote Originally Posted by Arnoutdv View Post
    Sometimes Iíve to process ten thousands of records, putting them in classes would be very very slow and it takes ages to destroy them.
    Obsolete maybe for you, but not for my applications.

    Oh and VB6 is obsolete, there are much better alternatives..
    Now the memory is too cheap. I have installed 32G, but I usually use less than 10G of memory. I also made 10G of RAM into RAMDISK to store TEMP and other files, but there is still 12G of memory left.
    If enough memory is allocated in advance, the string array will be very fast.
    Because of the new crown epidemic, the price of memory and CPU have risen, and AMD's price/performance ratio has also decreased. The future is the world of electric vehicles, and car chips are hard to find.
    Mobile phone memory is getting bigger and bigger. DDR5 memory is the first to use, and large memory is also used inside the solid state drive as a cache.

  14. #14
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,751

    Re: About array

    Using a 2 dimensional array is pretty obvious.

    I assumed he wanted "an array of arrays" for some reason though, such as different inner-array dimensions for each outer-array element. Maybe I shouldn't have assumed he knew what he was doing. Hard to tell from what was written.

  15. #15
    Frenzied Member
    Join Date
    Jan 2020
    Posts
    1,563

    Re: About array

    Quote Originally Posted by Eduardo- View Post
    Obsolete: not in use any more, having been replaced by something newer and better or more fashionable.

    I prefer to use classes, because with UDTs as long as you need to do somthing more than just storing values, you can't.

    Also when you need to pass the data to other part of the program you can get an error "Only public user defined types defined in public object modules can be used as parameters or return types for public procedures of class modules or as fields of public user defined types"

    But classes have an overhead, so I think that UDTs still have their place for processing/memory intensive algorythms when you want to optimize the speed and/or memory as much as you can.

    Also when you need to store a few variables (not many) that must work toghether I prefer to use individual variables (or individual arrays). But I think that that's has to do with the way that we are used to do these things, UDTs also work.

    So my opinion is that they are quite obsolete... but not totally.
    I used to develop a system for applications such as data acquisition of factory assembly line equipment and control equipment start and stop.
    It can manage hundreds of PLC devices, using the method of data structure types and dictionary objects.
    Application scenario: Siemens PLC, network TCP communication, the port may be different, the IP is different, the data memory address corresponding to each instrument and equipment such as the motor and encoder managed by the PLC, the length is different.
    I save the configuration file in the ACCESS database table and load it when the program starts. If which PLC can be connected successfully, then read the data of different addresses of multiple PLCs at the same time and display it.

    In essence, it can be implemented in a class, and it is more difficult to use a structure.
    For example: plc("equipment 1"). Collect data, plc("equipment 2"). Collect data

  16. #16
    Frenzied Member
    Join Date
    Jan 2020
    Posts
    1,563

    Re: About array

    Quote Originally Posted by dilettante View Post
    Using a 2 dimensional array is pretty obvious.

    I assumed he wanted "an array of arrays" for some reason though, such as different inner-array dimensions for each outer-array element. Maybe I shouldn't have assumed he knew what he was doing. Hard to tell from what was written.
    If the memory is large enough, it is also a good way to allocate as much memory as possible. It is also possible to allocate dimensions of the same length. You can add an array to save the amount of data received by each TCP connection object. However, it is more convenient to use structure types as much as possible

    If multiple winsock control arrays are used to download web pages in batches, and receive data, it is best to use byte arrays, append each time, and then convert it to a string after receiving it, otherwise receiving unicode characters will cause garbled characters
    Code:
    Dim a() As String
    ReDim Preserve a(3, 10000)
    Dim X As Long, Y As Long
    For X = 1 To 3
        For Y = 1 To 10
            a(X, Y) = X & "," & Y
        Next
    Next
    a(n,0) for save string count
    a(1,0)=11
    a(2,0)=100
    a(3,0)=1000
    Last edited by xiaoyao; Jul 24th, 2021 at 06:13 PM.

  17. #17
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    6,379

    Re: About array

    UDTs are bare metal structures, VARIANTS and COM classes are not. I'd say 90% of the time it's better to use classes. However, if you need bleeding edge performance, use UDTs. UDTs are directly represented in memory and their fields can be directly accessed with a few assembly instructions. Data from VARIANTs and COM classes are accessed through a few layers of function calls which I don't recommend for very CPU intensive workloads, for example, image processing.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  18. #18
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,751

    Re: About array

    Micro optimization is not a virtue. Simple UDTs are covered in warts. A proper UDT must be published in a typelib, and then it gets accessed via IRecordInfo instances.

    Avoid UDTs.

  19. #19
    PowerPoster
    Join Date
    Jun 2013
    Posts
    5,569

    Re: About array

    Quote Originally Posted by dilettante View Post
    A proper UDT must be published in a typelib, and then it gets accessed via IRecordInfo instances.

    Avoid UDTs.
    ... on Public interfaces of your own, self-compiled COM-Binaries, that is ...

    On the inside of your Binaries, UDTs offer significant performance-advantages over Classes
    (when you have to handle a "list of tuples" with a count larger than 10000 or so...).

    Image-Processing was already mentioned, as a typical usecase for that.

    Olaf

  20. #20
    Frenzied Member
    Join Date
    Jan 2020
    Posts
    1,563

    Re: About array

    Code:
    Dim a() As String, DataCount() As Long
    Private Sub Form_Load()
    ReDim a(10, 8) 'winsock1(0-10)  ,have 11 controls
    ReDim DataCount(10)
    End Sub
    
    Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim x As Single
    x = DataCount(Index) + 1
    DataCount(Index) = x
     
    Winsock1(Index).GetData a(Index, x)
    If x = 8 Then DataCount(Index) = 0
    
    End Sub
    Code:
    Dim A() As String, DataCount() As Long, ControlsUb As Long
    Private Sub Form_Load()
    ControlsUb = 10
    ReDim A(ControlsUb, 0) 'winsock1(0-10)  ,have 11 controls
    ReDim DataCount(ControlsUb)
    End Sub
    
    Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim x As Single
    x = DataCount(Index)
    ReDim Preserve A(ControlsUb, x)
    Winsock1(Index).GetData A(Index, x)
    DataCount(Index) = x + 1
    End Sub
    Last edited by xiaoyao; Jul 25th, 2021 at 09:08 PM.

  21. #21
    Hyperactive Member
    Join Date
    Aug 2020
    Posts
    447

    Re: About array

    VB6 has too many restrictions on UDT. It would be great if we could break through the restrictions on UDT by VB6.

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