Results 1 to 8 of 8

Thread: [RESOLVED] List of a specific control to work with in many events (How to declare?)

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Resolved [RESOLVED] List of a specific control to work with in many events (How to declare?)

    Hi all, o7
    Currently I'm using green line in every single event:
    Code:
        Private Sub BtnAll_Click_1(sender As Object, e As EventArgs) Handles BtnAll.Click
            Dim SelectedUC As UC() = {Form1.UC1, Form1.UC2, Form1.UC3, Form1.UC4, Form1.UC5, Form1.UC6 Form1.UC7, Form1.UC8, Form1.UC9, Form1.UC10, Form1.UC11, Form1.UC12, Form1.UC13, Form1.UC14, Form1.UC15, Form1.UC16, Form1.UC17, Form1.UC18, Form1.UC19, Form1.UC20} 'THE ACTUAL COLLECTION IS UP TO 40
            For i = 0 To 20 'BETTER TO BE REPLACED WITH LENGTH - 1
                TANKS(i).INFOPAGE.Show()
            Next
        End Sub
    I'm sure there is a simpler way to achieve declaring up to 40 UserControls at once (or get existing) like Dim allUCsAtOnce As New List(Of UC) From {} or other ways I'm not aware of. Also you can change their names, increase/decrease the number of them visually and there is no need to change anything else. ALSO variable/collection/array length can be use instead of constant numbers.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: List of a specific control to work with in many events (How to declare?)

    Code:
    Public Class Form1
        Private allUCs() As UC
    
        Public Sub New()
            InitializeComponent()
            allUCs = Me.Controls.OfType(Of UC).ToArray()
        End Sub
    
        Private Sub BtnAll_Click_1(sender As Object, e As EventArgs) Handles BtnAll.Click
            For i = 0 To allUCs.GetUpperBound(0)
                allUCs(i).INFOPAGE.Show()
            Next
        End Sub
    
    End Class

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: List of a specific control to work with in many events (How to declare?)

    To do the same thing, but with a List(Of UC)

    Code:
    Public Class Form1
        Private allUCs As List(Of UC)
    
        Public Sub New()
            InitializeComponent()
            allUCs = Me.Controls.OfType(Of UC).ToList()
        End Sub
    
        Private Sub BtnAll_Click_1(sender As Object, e As EventArgs) Handles BtnAll.Click
            For i = 0 To allUCs.Count - 1
                allUCs(i).INFOPAGE.Show()
            Next
        End Sub
    
    End Class

  4. #4

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: List of a specific control to work with in many events (How to declare?)

    wow, thanks for quick response.

    - What's the differences between ToList() and ToArray()? If I'd be in trouble in near future, I rather to know it now. I personally prefer tolist since ".count" is more familiar to me instead of ".GetUpperBound(0)" plus I think count, length, min, max and etc. were parts of LINQ and I believe in that.
    - Can I get/change their parameters/controls in other form by replacing "Private" to "Public" (Testing it right now)

  5. #5
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    Re: List of a specific control to work with in many events (How to declare?)

    Quote Originally Posted by pourkascheff View Post
    - Can I get/change their parameters/controls in other form by replacing "Private" to "Public" (Testing it right now)
    You can, but I would strongly advise against doing so. Instead of making the controls public and manipulating them directly, add either properties or methods to the form that are public and use these to manage the controls themselves.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: List of a specific control to work with in many events (How to declare?)

    Quote Originally Posted by pourkascheff View Post
    What's the differences between ToList() and ToArray()? If I'd be in trouble in near future, I rather to know it now. I personally prefer tolist since ".count" is more familiar to me instead of ".GetUpperBound(0)" plus I think count, length, min, max and etc. were parts of LINQ and I believe in that.
    The difference is obviously that one creates a generic List and the other creates an array. You call the method that creates the type of list you want/need.

    To be frank, a lot of people uses a generic List when they should use an array. People talk about a List(Of T) being more flexible and that is absolutely true but using it in cases where you're not going to make use of that flexibility provides no advantage. If you aren't going to add and/or remove items then how does the fact that a List(Of T) will grow and shrink dynamically help you? Arrays are fixed-size so, if you don't intend to change the size of the list, that's almost certainly what you should be using. Given that a List(Of T) uses an array internally, you're just adding an extra layer for no good reason if you don't use the functionality that a List(Of T) provides.

    As for what you said, you're basically wrong. Saying you prefer Count to GetUpperBound makes no sense. The Count property gives you the number of items in a List while there is no member to give you the last index. For arrays, the Length property gives you then number of elements while the GetUpperBound method gives you the last index for a particular rank. For 1D arrays, 0 is the only rank but GetUpperBound works for 2D, etc, arrays too. If you prefer to use Length - 1 rather than GetUpperBound(0) for arrays then nobody's stopping you. As for LINQ, extension methods like Count, Min, Max, etc, can be called on any IEnumerable(Of T), which includes arrays, so there's zero difference between a List(Of T) and an array in that context.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: List of a specific control to work with in many events (How to declare?)

    It's possible that using a List over an array will waste a small amount of memory. This isn't generally an issue that should concern anybody, so it isn't a good reason to choose one over the other, but it's something you might be aware of. An array of size S will be an array that holds S items of whatever type the array is. A List is built around an array, but is somewhat optimized for performance...so long as it changes size. If the List never changes size, there aren't any positive performance considerations, so you should use an array. However, if you will be adding items to the List over time, the List will be more efficient.

    The reason is that memory allocation is a relatively expensive operation. These days, you won't notice that, but a few decades back you might have. Arrays can't change size, so if you increase the size of an array (with something like ReDim), then you are actually allocating sufficient memory to hold the array of the new size, then perhaps copying over the information from the old to the new (if you used ReDim Preserve). A List wraps an array, so the same consideration is true, but it is optimized. A List will wrap an array of some size S. You can add items to the List up to size S, but as soon as you add item S+1, then a new array is allocated of size 2*S. In other words, the size that the List can hold, doubles every time it increases. The Count gives you the number of items that the underlying array DOES hold, but it can likely hold more before there is a need to allocate new memory. This is more efficient than allocating a new array every time you add an item to the List. The List is getting more than it needs, then managing what is in the List so long as it can hold it, and when the number of items exceeds what it can hold, it doubles the size. This reduces the number of allocations needed, but also means that it can waste a modest amount of space because the underlying array is likely to be somewhat larger than what is strictly necessary.

    Once again, that doesn't matter in almost every situation. It's almost always better to trade memory for speed, which is what the List does.
    My usual boring signature: Nothing

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: List of a specific control to work with in many events (How to declare?)

    Quote Originally Posted by pourkascheff View Post
    - Can I get/change their parameters/controls in other form by replacing "Private" to "Public" (Testing it right now)
    What is the relationship between 'the other form' and the form where you want to use the list? Does 'the other form' open this form?

Tags for this Thread

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