Push Shared Property value change to object instances Vb.Net

Hobbyist_programmer 621 Reputation points
2020-11-25T19:36:42.213+00:00

Hallo All,

I have following code and i am trying to push the shared property change to another class object instances. I have a class setting with shared property language.

I have another class Test where it takes this shared property. I want to change the language property in setting so that the change of language immediately updated in my Test object instances. How do i do it?.

Thanks

Imports System.ComponentModel  
Imports System.Runtime.CompilerServices  
  
Public Class Form1  
  
    Public Test As New Tests  
    Public BS_Setting As New BindingSource  
  
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        BS_Setting.DataSource = Test  
        DataGridView2.DataSource = BS_Setting  
    End Sub  
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click  
        BS_Setting.Add(New Test())  
    End Sub  
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click  
        Setting.language = "French"  
    End Sub  
  
End Class  
  
  
Public Class Setting  
    Implements INotifyPropertyChanged  
  
    Public Sub New()  
    End Sub  
  
    Public Shared languageValue As String = "ENG"  
    Public Shared Property language() As String  
        Get  
            Return languageValue  
        End Get  
        Set(ByVal value As String)  
            languageValue = value  
        End Set  
    End Property  
  
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged  
    Protected Overridable Sub OnPropertyChanged(<CallerMemberName> Optional memberName As String = Nothing)  
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(memberName))  
    End Sub  
End Class  
  
Public Class Test  
    Public Sub New()  
    End Sub  
  
    Private LangValue As String  
    Public Property Lang As String  
        Get  
            Return Setting.language  
        End Get  
        Set(ByVal value As String)  
            LangValue = value  
        End Set  
    End Property  
  
End Class  
  
Public Class Tests  
    Inherits BindingList(Of Test)  
End Class  
  

42822-test.gif

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

Accepted answer
  1. Xingyu Zhao-MSFT 5,356 Reputation points
    2020-11-26T06:01:16.76+00:00

    Hi @Hobbyist_programmer ,

    I want to change the language property in setting so that the change of language immediately updated in my Test object instances.

    Try to refresh the DataGridView when you change the 'language' property.

            Setting.language = "French"  
            DataGridView2.Refresh()  
    

    Result of my test.
    42910-gif.gif
    Hope it could be helpful.

    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.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Hobbyist_programmer 621 Reputation points
    2020-11-26T07:26:50.967+00:00

    wow.. thanks for the answer XingyuZhao.

    0 comments No comments

  2. Hobbyist_programmer 621 Reputation points
    2020-11-26T07:27:56.62+00:00

    Why shared properties are not showing up in the datagridview?

    0 comments No comments

  3. Karen Payne MVP 35,031 Reputation points
    2020-11-28T20:08:57.483+00:00

    Hello @Hobbyist_programmer

    Even though you have an accepted solution I'd like to provide another solution.

    • Rather than a shared field use a non-shared field unless you want all existing rows to change.
    • There is really no need to have a wrapper class simply for the BindingList, instead use the Setting class assigned to a BindingSource in the form. The class should really only have properties and INotifyPropertyChanged event and OnPropertyChanged

    Setting class
    Read the comments

    Imports System.ComponentModel  
    Imports System.Runtime.CompilerServices  
      
    Namespace Classes  
        Public Class Setting  
            Implements INotifyPropertyChanged  
      
            Public Sub New(Optional language As String = "English")  
                languageValue = language  
            End Sub  
            ''' <summary>  
            ''' Shared modifier means all instances are affected,  
            ''' remove Shared if that is not what you want.  
            ''' </summary>  
            Public languageValue As String = ""  
            Public Property Language() As String  
                Get  
                    Return languageValue  
                End Get  
                Set  
                    languageValue = Value  
                    '  
                    ' Make sure to implement the line below  
                    '  
                    OnPropertyChanged()  
                End Set  
            End Property  
      
            Public Event PropertyChanged As PropertyChangedEventHandler _  
                Implements INotifyPropertyChanged.PropertyChanged  
      
            Protected Overridable Sub OnPropertyChanged(  
                <CallerMemberName> Optional memberName As String = Nothing)  
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(memberName))  
      
            End Sub  
        End Class  
    End Namespace  
    

    Form code

    Imports System.ComponentModel  
    Imports BindingWithoutRefreshing.Classes  
      
    Public Class Form1  
      
        Public SettingsBindingSource As New BindingSource  
        Public SettingsBindingList As New BindingList(Of Setting)  
      
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            DataGridView2.AutoGenerateColumns = False  
      
            SettingsBindingList = New BindingList(Of Setting)(New List(Of Setting))  
            SettingsBindingSource.DataSource = New BindingSource(SettingsBindingList, Nothing)  
      
            DataGridView2.DataSource = SettingsBindingSource  
      
            LanguagesComboBox.DataSource = New List(Of String) From {"English", "French", "Spanish"}  
      
        End Sub  
      
        Private Sub AddNewSettingButton_Click(sender As Object, e As EventArgs) _  
            Handles AddNewSettingButton.Click  
      
            SettingsBindingSource.Add(New Setting(LanguagesComboBox.Text))  
            SettingsBindingSource.MoveLast()  
      
        End Sub  
      
        Private Sub ChangeLanguageButton_Click(sender As Object, e As EventArgs) _  
            Handles ChangeLanguageButton.Click  
      
            If SettingsBindingSource.Current IsNot Nothing Then  
                CType(SettingsBindingSource.Current, Setting).Language = LanguagesComboBox.Text  
            End If  
      
        End Sub  
      
        Private Sub DataGridView2_DefaultValuesNeeded(sender As Object, e As DataGridViewRowEventArgs) _  
            Handles DataGridView2.DefaultValuesNeeded  
      
            e.Row.Cells(0).Value = LanguagesComboBox.Text  
      
        End Sub  
    End Class  
    

    Full source GitHub repository

    43405-form.png

    0 comments No comments