none
Get property value from another property value? RRS feed

  • Question

  • Hallo,

    I have 2 classes like this 

        Public Class Person
            Public Property firstName As String
            Public Property lastName As String
            Public Property NickName As String
        End Class
    
        Public Class Contact
            Public Property NickName As String
            Public Property email As String
            Public Property Phone As String
            Public Property Address String
        End Class


    for example i have a combo box where i can select the first name of a person, then i get the nickname by following 

    Dim nick as string = cType(combobox.SelectedItem, Person).NickName

    Now how do i get the address or email for the person (nick) from class Contact?

    thanks.


    Saturday, June 27, 2020 9:07 PM

Answers

  • Hello,

    No matter how you populate these classes I would recommend using the following using mocked up data. Data can come from any place.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim people As New List(Of Person) From {
                New Person With {
                    .Id = 1,
                    .FirstName = "Jim",
                    .LastName = "Smith",
                .Contact = New Contact() With {
                    .ContactId = 10,
                    .PersonId = 1,
                    .Phone = "5555555555",
                    .Address = "123 Apple Lane",
                    .Email = "js@comcast.net",
                    .NickName = "XYZ"}},
                New Person With {
                    .Id = 2,
                    .FirstName = "Mary",
                    .LastName = "Bickmen",
                .Contact = New Contact() With {
                    .ContactId = 20,
                    .PersonId = 2,
                    .Phone = "6666666666",
                    .Address = "33 Wyndmoor Lane",
                    .Email = "mb@gmail",
                    .NickName = "Hot apples"}}
                }
        PeopleComboBox.DataSource = people
    
    End Sub

    Get information

    Private Sub GetCurrentPersonButton_Click(sender As Object, e As EventArgs) Handles GetCurrentPersonButton.Click
        Dim person = CType(PeopleComboBox.SelectedItem, Person)
        Dim contact = person.Contact
    End Sub

    If you wanted to you could event breakdown email and phone as the average person has more than one of each.

    Public Class Person
        Public Property Id() As Integer
        Public Property FirstName As String
        Public Property LastName As String
        Public Property Contact() As Contact
    
        Public Overrides Function ToString() As String
            Return FirstName
        End Function
    End Class
    
    Public Class Contact
        Public Property ContactId() As Integer
        Public Property PersonId() As Integer
        Public Property NickName As String
        Public Property Email As String
        Public Property Phone As String
        Public Property Address As String
    End Class

    Note no need to set DisplayMember as .ToString handles this in the person class.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    Saturday, June 27, 2020 9:33 PM
    Moderator

All replies

  • Hello,

    No matter how you populate these classes I would recommend using the following using mocked up data. Data can come from any place.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim people As New List(Of Person) From {
                New Person With {
                    .Id = 1,
                    .FirstName = "Jim",
                    .LastName = "Smith",
                .Contact = New Contact() With {
                    .ContactId = 10,
                    .PersonId = 1,
                    .Phone = "5555555555",
                    .Address = "123 Apple Lane",
                    .Email = "js@comcast.net",
                    .NickName = "XYZ"}},
                New Person With {
                    .Id = 2,
                    .FirstName = "Mary",
                    .LastName = "Bickmen",
                .Contact = New Contact() With {
                    .ContactId = 20,
                    .PersonId = 2,
                    .Phone = "6666666666",
                    .Address = "33 Wyndmoor Lane",
                    .Email = "mb@gmail",
                    .NickName = "Hot apples"}}
                }
        PeopleComboBox.DataSource = people
    
    End Sub

    Get information

    Private Sub GetCurrentPersonButton_Click(sender As Object, e As EventArgs) Handles GetCurrentPersonButton.Click
        Dim person = CType(PeopleComboBox.SelectedItem, Person)
        Dim contact = person.Contact
    End Sub

    If you wanted to you could event breakdown email and phone as the average person has more than one of each.

    Public Class Person
        Public Property Id() As Integer
        Public Property FirstName As String
        Public Property LastName As String
        Public Property Contact() As Contact
    
        Public Overrides Function ToString() As String
            Return FirstName
        End Function
    End Class
    
    Public Class Contact
        Public Property ContactId() As Integer
        Public Property PersonId() As Integer
        Public Property NickName As String
        Public Property Email As String
        Public Property Phone As String
        Public Property Address As String
    End Class

    Note no need to set DisplayMember as .ToString handles this in the person class.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    Saturday, June 27, 2020 9:33 PM
    Moderator
  • Shan I thought I make sample for you using your classes. 

    But in fact they are senseless. Karen shows that to you but does not tell why (the main reason). 

    An Address class can be very useful and help to optimize the database. 

    However, not when it can only be used by one user. 

    In this way it can be useful

     Public Class Person
            Public Property PersonID as Guid
            Public Property firstName As String
            Public Property lastName As String
            Public Property NickName As String
            Public Property AddressID as Guid
        End Class
    
        Public Class Address
            Public Property AddressID As Guid
            Public Property SharedEmail As String
            Public Property Phone As String
            Public Property Address String
        End Class
    Now you can have endless users who share the same address


    Success
    Cor




    Sunday, June 28, 2020 7:10 PM