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.

0

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

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 in fact i had no expirience using DI in WinWorms so thanks for your point. Still, it is possible to use singletons without DI :)
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 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 : gist.github.com/lolzballs/2152bc0f31ee0286b722 :)
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);
    }
}

7 Comments

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?"
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
@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.
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
@stuartd btw, fixed again.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.