How can I build like it: myControl.Persons("Alex").age="50"

Alexandre Bisewski 41 Reputation points
2021-11-01T01:43:10.933+00:00

Hi everybody.

I have a very noob question.

what I need to create to get something like this:

myControl.Persons("Alex").age = "50"

mycontrol is a CustomControl for winForms. I am using public Properties for some variables.

But for this I thing Taht I will need a List or Structure or a Class but I dont undertand how can I pass this string "Alex".

Thank you

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,768 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,331 Reputation points
    2021-11-01T08:56:51.76+00:00

    Hi Alexandre,
    for demo try following code:

    Imports System.Threading  
      
    Public Class Form25  
      
      Private sc As SynchronizationContext = SynchronizationContext.Current  
      
      Private myControl As New Form25CC1  
      Private Sub Form25_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        Me.Controls.Add(myControl)  
        Task.Factory.StartNew(New Action(Async Sub()  
                                           Await Task.Delay(2000)  
                                           myControl.Persons("Alex").Age = "50"  
                                           sc.Post(New SendOrPostCallback(Sub()  
                                                                            Me.Refresh()  
                                                                          End Sub), Nothing)  
                                         End Sub))  
      End Sub  
    End Class  
      
    End Class  
      
    Public Class Form25CC1  
      Inherits Control  
      
      Public Sub New()  
        Me.Width = 200  
        Me.Height = 200  
        PersonsList.Add(New Person With {.Name = "Peter"})  
        PersonsList.Add(New Person With {.Name = "Alex"})  
      End Sub  
      
      Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)  
        For i = 0 To PersonsList.Count - 1  
          e.Graphics.DrawString($"Name: {PersonsList(i).Name}, Age: {PersonsList(i).Age}", Me.Font, Brushes.Black, New PointF(10, i * 20))  
        Next  
        MyBase.OnPaint(e)  
      End Sub  
      
      Public ReadOnly Property Persons(name As String) As Person  
        Get  
          Return PersonsList.Find(Function(p)  
                                    Return p.Name = name  
                                  End Function)  
        End Get  
      End Property  
      
      Private PersonsList As New List(Of Person)  
      
      Public Class Person  
        Public Property Name As String  
        Public Property Age As String = "0"  
      End Class  
      
    End Class  
    

    or property like this:

      Public ReadOnly Property Persons(name As String) As Person  
        Get  
          Return PersonsList.Find(Function(p) p.Name.Equals(name))  
        End Get  
      End Property  
    

    Result:

    145844-x.gif


2 additional answers

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,371 Reputation points
    2021-11-01T07:37:04.16+00:00

    Hi @Alexandre Bisewski ,

    myControl.Persons("Alex").age = "50"

    It seems that you want to find 'Person' named “Alex” and set it age property to '50'.
    Take a look at the following code:

    myControl.Persons.Find(Function(x) x.name.Equals("Alex")).age = "50"  
    

    Hope it could be helpful.
    Besides, if I have any misunderstanding, please provide more related code about your List here.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Alexandre Bisewski 41 Reputation points
    2021-11-02T15:30:02.393+00:00

    Hi Peter.

    I am using your code and is working very well but I need to redraw my control when I change one of this values.

    Like in myControl.Persons("Alex").Age = "50" how can I call Invalidate() if I change this value?

    I need to redraw my control when I change this value.

    I have other properties but declareted inside my custonControl class like
    Public Property TotalCandlesView As Integer
    Get
    Return _candlesView
    End Get
    Set(value as integer)
    Invalidate()
    End Set
    End Property

    I tryed to use it in Public Class Person but not work.

    Thank you!!!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.