0

i have a little problem on my form Login when i Login, i want send my username on the string username in my class functions.

And when my Main Form is loaded i want this class functions get the username from my form login with my username

i have try something like this:

My form login:

public Functions FUNCTIONS = new Functions(); //for my class Functions

FUNCTIONS.Username = "Username123";

My class Functions:

  public class Functions
    {
       public string Username = ""; //empty
    }

and my Main form after login

public Functions FUNCTIONS = new FUNCTIONS();

  private void Main_Load(object sender, EventArgs e)
   {
      MessageBox.Show("Welcome "+ FUNCTIONS.Username " to my application.");
   }

When my Main Form is loaded it's don't show the username string it's keep this empty, thanks for your time and your help for fix my problem.

stuartd
  • 70,509
  • 14
  • 132
  • 163
Sakira57
  • 33
  • 4

2 Answers2

1

You could try

public class Functions
{
    public static string Username { get; set; }
}

Also this way you dont need to initialize Functions with new keyword, Functions.Username would be enough

It would work because static keyword ensures that there is one instance of this peroperty for yor application lifetime. Also you could consider using singleton pattern with dependency injection, read more there :

https://csharpindepth.com/articles/singleton

Roman Kalinchuk
  • 718
  • 3
  • 14
  • Dependency injection in windows forms is far more trouble than it's worth, and IMO the 'singleton pattern' is best avoided. Just `static` is fine. – stuartd Jul 03 '20 at 22:38
  • @stuartd in fact i had no expirience using DI in WinWorms so thanks for your point. Still, it is possible to use singletons without DI :) – Roman Kalinchuk Jul 03 '20 at 22:41
  • What advantage does a singleton give over a static property in this use case? To me it adds unnecessary complexity just to access a value in multiple forms? Maybe I'm missing something? – stuartd Jul 03 '20 at 22:46
  • @stuartd no, you right :) it is only another way to solve task and could be a point of interest later. But there's really no need to do smthn like this : https://gist.github.com/lolzballs/2152bc0f31ee0286b722 :) – Roman Kalinchuk Jul 03 '20 at 22:54
1

Don't create new 2nd time, new creates an another Functions instance. Pass to main Form the existing one instead and assign it to the field in main Form.

public partial class LoginForm : Form
{
    private Functions functions = new Functions();

    public LoginForm()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        functions.Username = "Username123";
        new MainForm.Show(functions);
        this.Close();
    }
}
public partial class MainForm : Form
{
    private Functions functions;

    public MainForm()
    {
        InitializeComponent();
    }

    public MainForm(Functions f) : this()
    {
        functions = f;
    }

    private void MainForm_Load(object sender, EventArgs e)
    {
        MessageBox.Show(functions.Username);
    }
}
aepot
  • 4,558
  • 2
  • 12
  • 24
  • It is not to flexible, don't you think? I mean "What if we need Username on another form? Do we need to pass it via controller each time?" – Roman Kalinchuk Jul 03 '20 at 22:36
  • Windows forms without paramterless constructors can't be loaded in the designer - _"Design decisions made regarding the way Windows Forms works more or less preclude parameterized .constructors for windows forms components."_ - [source](https://stackoverflow.com/questions/1784303/usercontrol-constructor-with-parameters-in-c-sharp) – stuartd Jul 03 '20 at 22:40
  • @RomanKalinchuk I think that OP doesn't know what is `new` itself. On my perspective, I prefer powerful and flexible WPF+MVVM. ...and I hate Singleton. Almost no one can create and use it properly. Let it be a lesson how to pass an argument to the `.ctor`. – aepot Jul 03 '20 at 22:45
  • 1
    I mostly work with web (.Net Core) and my eyes are bleeding almost each time i see WinForms code :))) Still, petterns (and singleton) are good when they are used well – Roman Kalinchuk Jul 03 '20 at 22:49
  • Um, so after your last edit when the login form **loads** (`LoginForm_Load`) you set the username and show the main form? Which doesn't a) give the user a chance to log in or b) close the login form ..? – stuartd Jul 03 '20 at 22:53
  • @stuartd it's not a complete code. Just an example how to pass an argument. It's reproducible, that's enough I guess. – aepot Jul 03 '20 at 23:02
  • 1
    @stuartd btw, fixed again. – aepot Jul 03 '20 at 23:06