Variable not retaining value between methods c#

c# pass object by value
c# passing variables between methods
c# pass by value
c# pass reference type by value
c# reference variable
c# store reference to variable
c# ref class
c# when to use ref

I'm making a WPF Application and I want the Text within my Textbox to change as I change the variable value, however, although I'm setting the variable correctly I can't get it to update the Textbox.

I have this class:

public class UserSettings : INotifyPropertyChanged 
{
    public event PropertyChangedEventHandler PropertyChanged;
    private string username;
    public string nameuser
    {
        get { return username;} set { username= value; OnPropertyChanged(nameuser); }
    }
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Which gets called in this class:

    public partial class User: Window
{
    private UserSettings objsettings = null;   
    public User()
    {

        objsettings = new UserSettings();
        DataContext = objsettings; 
        InitializeComponent();
        Console.WriteLine("objsettings.username1: " + objsettings.nameuser);
    }

    public void SetUserSettings(string username)
    {
        Console.WriteLine("Username: " + username);
        objsettings.nameuser= username;
        Console.WriteLine("objsettings.username2: " + objsettings.nameuser);
    }

and the XAML is:

  <TextBox Text="{Binding nameuser, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" Margin="210,193,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="30"/>

The console print in the SetUserSettings prints the value however the console print at the top prints the value as nothing. I'm aware that the top value is printing nothing as it gets set AFTER the console print is called (hence it wouldn't contain a value yet), but how do I update it so the window prints the new value, how do I get it to continously loop through User() without opening new windows but just update the values?


You send incorrect Notify:

 get { return username;} set { username= value; OnPropertyChanged(nameof(nameuser)); }

You should send not nameuser, but nameof(nameuser).

As for initial username pass it to the User class:

public User(string username)
    {

        objsettings = new UserSettings();
        objsettings.nameuser = username;
        DataContext = objsettings; 
        InitializeComponent();
        Console.WriteLine("objsettings.username1: " + objsettings.nameuser);
    }

C#, This is because a and b are not storing the data but a reference to the By default, passing a reference type variable to a method passes a copy of can prevent you from making mistakes that change the value of your data. [Solved] Global variable does not keep (retain) its value after PostBack using C# in ASP.Net. Answered Active Solved. Ask Question. Last Reply on Dec 15,


You send an incorrect Notification. You should send nameof(property name) instead the property value.

In addition, to increase the performance, we don't send notifications if there is no change in property value:

public string nameuser
{
    get { 
         return username;
        } 

    set { 
         if(username == value)
         {
             return;
         }

         username= value; 
         OnPropertyChanged(nameof(nameuser));
        }
}

Passing Value-Type Parameters, Any changes to the parameter that take place inside the method have no affect on the original data stored in the argument variable. If you want  By default, passing a reference type variable to a method passes a copy of the reference, not the actual data. If you change a value inside the reference type, this will also change the value outside the method as well, since it's pointing at the same reference location in memory.


OnPropertyChanged method expects the property name as string. In your case the code compiles since nameuser is also of type string. But essentially you are just telling the UI to listen for changes in property whose name is same as value of nameuser.

Changing it to the correct property name as OnPropertyChanged("nameuser"), will get the results you want.

In C# version 6 you can also use the nameof feature as it ensures that there are no magic strings in the code OnPropertyChange(nameof(IsBarNull));

ref keyword, In a method signature, to return a value to the caller by reference. This differs from out parameters, whose arguments do not have to be explicitly the returned valued by storing the value in a local ref readonly variable. Variable not retaining value between methods c#. Ask Question Asked 1 year, 4 months ago. Is there a reason for C#'s reuse of the variable in a foreach?


Ref return values and ref locals (C# Guide), In other words, it cannot be a local variable in the method that returns it. the by-​value return value from a property, or a method of a class or  The part of the program where a particular variable is accessible is termed as the Scope of that variable. A variable can be defined in a class, method, loop etc. In C/C++, all identifiers are lexically (or statically) scoped, i.e.scope of a variable can be determined at compile time and independent of the function call stack. But the C#


C# 2008 For Dummies, After they are back in Main(), these variables retain their modified values. Compare this to the PassByValue() method in which the variables do not retain their  1) It won't compile - C# is case sensitive, and Public is not the same as public 2) Your Singleton class as shown doesn't contain a getInstanse method, which is crucial to your problem. The whole idea of a singleton class is that there is one and only one instance of the class in the system - so the constructor is always declared as private


NET for Java Developers Migrating to C#, A value parameter variable ceases to exist after the method call. Variables pointing to primitive types do not retain changes made to them inside the method​  Hi. I have a webpage (say W) that is displayed inside an iframe of a main page (say M) . All the below events are in the webpage (W). I calculate the gridview total using jquery and store it in a