How would I assign the members of one instance of a class to another instance of the same class in VB.NET?

Marc Menzel 61 Reputation points
2023-02-04T00:05:49.46+00:00

How would I assign the members of one instance of a class to another instance of the same class in VB.NET?

Both EETSettings and EETLightColors are istances of the same class in a VB.NET Windows Forms (.NET Framework) Application.


      EETSettings = New ColorTheme

        With EETSettings

            Dim PickedThemeSelected As String

            Dim PickedColorThemeName As String

            Dim PickedLetterColor As Color

            Dim PickedLetterColorName As String

            Dim PickedPromptColor As Color

            Dim PickedPromptColorName As String

            Dim PickedCommandColor As Color

            Dim PickedCommandColorName As String

            Dim PickedBackGroundColor As Color

            Dim PickedBackGroundName As String

            Dim PickedInterfaceColor As Color

            Dim PickedInterfaceColorName As String

            Dim PickedLabelColor As Color

            Dim PickedLabelColorName As String

            Dim PickedButtonBackGroundColor As Color

            Dim PickedButtonBackGroundColorName As String

            Dim PickedButtonForeGroundColor As Color

            Dim PickedButtonForeGroundColorName As String

            EETSettings = New ColorTheme

            With EETSettings

                PickedThemeSelected = EETLightColors.Selected

                .Selected = PickedThemeSelected

                PickedColorThemeName = EETLightColors.ThemeName

                .ThemeName = PickedColorThemeName

                PickedLetterColor = EETLightColors.ForeKolor

                .ForeKolor = LetterColor

                PickedLetterColorName = EETLightColors.ForeKolorName

                .ForeKolorName = PickedLetterColorName

                PickedPromptColor = EETLightColors.PromptKolor

                .PromptKolor = PickedPromptColor

                PickedPromptColorName = EETLightColors.PromptKolorName

                .PromptKolorName = PickedPromptColorName

                PickedCommandColor = EETLightColors.CommandKolor

                .CommandKolor = PickedCommandColor

                PickedCommandColorName = EETLightColors.CommandKolorName

                .CommandKolorName = PickedCommandColorName

                PickedBackGroundColor = EETLightColors.BackKolor

                .BackKolor = PickedBackGroundColor

                PickedBackGroundName = EETLightColors.BackKolorName

                .BackKolorName = PickedBackGroundName

                PickedInterfaceColor = EETLightColors.InterfaceKolor

                .InterfaceKolor = PickedInterfaceColor

                PickedInterfaceColorName = EETLightColors.InterfaceKolorName

                .InterfaceKolorName = PickedInterfaceColorName

                PickedLabelColor = EETLightColors.LabelKolor

                .LabelKolor = PickedLabelColor

                PickedLabelColorName = EETLightColors.LabelKolorName

                .LabelKolorName = PickedLabelColorName

                PickedButtonBackGroundColor = EETLightColors.ButtonBackKolor

                .ButtonBackKolor = PickedButtonBackGroundColor

                PickedButtonBackGroundColorName = EETLightColors.ButtonBackKolorName

                .ButtonBackKolorName = PickedButtonBackGroundColorName

                PickedButtonForeGroundColor = EETLightColors.ButtonForeKolor

                .ButtonForeKolor = PickedButtonForeGroundColor

                PickedButtonForeGroundColorName = EETLightColors.ButtonForeKolorName

                .ButtonForeKolorName = PickedButtonForeGroundColorName

            End With

        End With

    End Sub
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,345 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,564 questions
{count} votes

3 answers

Sort by: Most helpful
  1. JosePichardo22-9478 1 Reputation point
    2023-02-04T00:14:09.09+00:00
    EETSettings = New ColorTheme
    

  2. Reza Aghaei 4,936 Reputation points MVP
    2023-02-04T00:45:15.7666667+00:00

    Basically you need to assign properties one by one:

    Dim t1 = New Test()
    t1.Property1 = "1"
    t1.Property2 = Color.Red
    
    Dim t2 = New Test()
    t2.Property1 = t1.Property1
    t2.Property2 = t1.Property2
    

    But if the number of properties are too much, you can do something like this:

    Dim t1 = New Test()
    t1.Property1 = "1"
    t1.Property2 = Color.Red
    
    Dim t2 = New Test()
    
    For Each item As PropertyInfo In
        GetType(Test).GetProperties(
            BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance)
    
    item.SetValue(t2, item.GetValue(t1))
    Next
    

    But in general, even if there's 100 properties there no reason to not do it like the first option. It's a one time code.

    0 comments No comments

  3. LesHay 7,126 Reputation points
    2023-02-04T01:10:32.3633333+00:00

    Hi

    Late to the party, but I cooked it so I'll post it ................... basically the same answer as previous - I think* you had a slight mix up in your description.

    The data types are different but the principle is the same.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' create a NEW instance of OneClass
        Dim EETSettings As New OneClass
        ' set 3 items as this
        With EETSettings
          .Item1 = "Item1 Test1"
          .Item2 = 123
          .Item3 = Now
        End With
    
        ' here is a NEW instance of OneClass
        ' assigning the value of Item3 of original
        ' OneClass instance to it.
        Dim EETLightColors As New OneClass
        EETLightColors.Item3 = EETSettings.Item3
    
        ' at this point, EETLightColors.Item3 is
        ' equal to EETSettings.Item3
      End Sub
      Class OneClass
        Property Item1 As String
        Property Item2 As Integer
        Property Item3 As DateTime
      End Class
    End Class
    
    0 comments No comments