none
Sharing a property from a Class to another Class/Custom object class ? RRS feed

  • Question

  • Hallo,

    I have a class setup like below. I want to access Currency property of the project in Class "Good", How do I do that ?

    Public Class Project
    Public Property Currency() As Double
    Public Property Goods() As Goods
    End Class
    
    Public Class Good
      Public Property Price1() As Integer
      Public Property Price2() As Integer
      Public Property Price3() As Integer
    
      Public Readonly Property TotalCost() As Double
           Get
              Return Get_TotalCost()
           End Get
      End Property
    
      Public Function Get_totalCost() As Double
          Return Me.Price1* Me.Price2* Me.Price3* Currency
      End function
    
    End Class
    
    Public Class Goods
        Inherits System.ComponentModel.BindingList(Of Good)
    End Class

    Thanks

    vendredi 23 octobre 2020 09:16

Réponses

  • Try this, see if this is what you are after.

    Public Class Project
        Public Property Currency() As Double
        Public Property Goods() As Goods
    End Class
    
    Public Class Good
        Inherits Project
        Public Property Price1() As Integer
        Public Property Price2() As Integer
        Public Property Price3() As Integer
    
        Public ReadOnly Property TotalCost() As Double
            Get
                Return GetTotalCost()
            End Get
        End Property
    
        Public Function GetTotalCost() As Double
            Return Price1 * Price2 * Price3 * Currency
        End Function
    
    End Class
    
    Public Class Goods
        Inherits ComponentModel.BindingList(Of Good)
    End 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

    • Marqué comme réponse Shan1986 vendredi 23 octobre 2020 12:50
    vendredi 23 octobre 2020 11:50
    Modérateur

Toutes les réponses

  • Try this, see if this is what you are after.

    Public Class Project
        Public Property Currency() As Double
        Public Property Goods() As Goods
    End Class
    
    Public Class Good
        Inherits Project
        Public Property Price1() As Integer
        Public Property Price2() As Integer
        Public Property Price3() As Integer
    
        Public ReadOnly Property TotalCost() As Double
            Get
                Return GetTotalCost()
            End Get
        End Property
    
        Public Function GetTotalCost() As Double
            Return Price1 * Price2 * Price3 * Currency
        End Function
    
    End Class
    
    Public Class Goods
        Inherits ComponentModel.BindingList(Of Good)
    End 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

    • Marqué comme réponse Shan1986 vendredi 23 octobre 2020 12:50
    vendredi 23 octobre 2020 11:50
    Modérateur
  • Thanks a lot Karen..It workd 
    vendredi 23 octobre 2020 12:50
  • Try this, see if this is what you are after.

    Public Class Project
        Public Property Currency() As Double
        Public Property Goods() As Goods
    End Class
    
    Public Class Good
        Inherits Project
        Public Property Price1() As Integer
        Public Property Price2() As Integer
        Public Property Price3() As Integer
    
        Public ReadOnly Property TotalCost() As Double
            Get
                Return GetTotalCost()
            End Get
        End Property
    
        Public Function GetTotalCost() As Double
            Return Price1 * Price2 * Price3 * Currency
        End Function
    
    End Class
    
    Public Class Goods
        Inherits ComponentModel.BindingList(Of Good)
    End 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

    Sorry . I have another problem . Now my binding source for the "Goods" also shows all properties from Project . Is there a way only Currency is passed to Class "Good"? 
    vendredi 23 octobre 2020 13:03
  • Try this, see if this is what you are after.

    Public Class Project
        Public Property Currency() As Double
        Public Property Goods() As Goods
    End Class
    
    Public Class Good
        Inherits Project
        Public Property Price1() As Integer
        Public Property Price2() As Integer
        Public Property Price3() As Integer
    
        Public ReadOnly Property TotalCost() As Double
            Get
                Return GetTotalCost()
            End Get
        End Property
    
        Public Function GetTotalCost() As Double
            Return Price1 * Price2 * Price3 * Currency
        End Function
    
    End Class
    
    Public Class Goods
        Inherits ComponentModel.BindingList(Of Good)
    End 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

    Sorry . I have another problem . Now my binding source for the "Goods" also shows all properties from Project . Is there a way only Currency is passed to Class "Good"? 

    Hello,

    It's going to be a while until I can look at it as I'm teaching all day today at work.


    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

    vendredi 23 octobre 2020 15:27
    Modérateur
  • Try this, see if this is what you are after.

    Public Class Project
        Public Property Currency() As Double
        Public Property Goods() As Goods
    End Class
    
    Public Class Good
        Inherits Project
        Public Property Price1() As Integer
        Public Property Price2() As Integer
        Public Property Price3() As Integer
    
        Public ReadOnly Property TotalCost() As Double
            Get
                Return GetTotalCost()
            End Get
        End Property
    
        Public Function GetTotalCost() As Double
            Return Price1 * Price2 * Price3 * Currency
        End Function
    
    End Class
    
    Public Class Goods
        Inherits ComponentModel.BindingList(Of Good)
    End 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

    Sorry . I have another problem . Now my binding source for the "Goods" also shows all properties from Project . Is there a way only Currency is passed to Class "Good"? 

    Hello,

    It's going to be a while until I can look at it as I'm teaching all day today at work.


    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

    Hallo Karen, Is there a way to just expose one property from the class to other class?
    mercredi 28 octobre 2020 16:32
  • Here really is no nice way to do this, you should only inherit a base class if you're implementing all of the functionality of that base class.

    Here is what I came up with that means Currency property in Project is not accessible from inheritors.

    It's good to think OOP but in this case not wise.

    Public Class Project
        ''' <summary>
        ''' In Project class
        ''' </summary>
        ''' <returns></returns>
        <Browsable(False),
            EditorBrowsable(EditorBrowsableState.Never),
            Bindable(False),
            DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
        Public Overridable Property Currency() As Double
        Public Property Goods() As Goods
    End Class
    
    Public Class Good
        Inherits Project
    
        ''' <summary>
        ''' In Good class
        ''' </summary>
        ''' <returns></returns>
        Public Overrides Property Currency() As Double
        Public Property Price1() As Integer
        Public Property Price2() As Integer
        Public Property Price3() As Integer
    
        Public ReadOnly Property TotalCost() As Double
            Get
                Return GetTotalCost()
            End Get
        End Property
    
        Public Function GetTotalCost() As Double
            Return Price1 * Price2 * Price3 * Currency
        End Function
    
    End Class
    
    Public Class Goods
        Inherits BindingList(Of Good)
    End Class

    Note someone can simply use backing variables to get past the above.

    Public Class Project
        ''' <summary>
        ''' In Project class
        ''' </summary>
        ''' <returns></returns>
        <Browsable(False),
            EditorBrowsable(EditorBrowsableState.Never),
            Bindable(False),
            DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
        Public Overridable Property Currency() As Double
        Public Property Goods() As Goods
    End Class
    
    Public Class Good
        Inherits Project
    
        Private _currency1 As Double
    
        ''' <summary>
        ''' In Good class
        ''' </summary>
        ''' <returns></returns>
        Public Overrides Property Currency() As Double
            Get
                Return MyBase.Currency
            End Get
            Set
                _currency1 = value
            End Set
        End Property
    
        Public Property Price1() As Integer
        Public Property Price2() As Integer
        Public Property Price3() As Integer
    
        Public ReadOnly Property TotalCost() As Double
            Get
                Return GetTotalCost()
            End Get
        End Property
    
        Public Function GetTotalCost() As Double
            Return Price1 * Price2 * Price3 * Currency
        End Function
    
    End Class
    
    Public Class Goods
        Inherits BindingList(Of Good)
    End 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


    mercredi 28 octobre 2020 18:53
    Modérateur