C#, Am I passing strings correctly?

Discussion in 'Computer Programming, Emulation, and Game Modding' started by ground, Nov 26, 2014.

  1. ground
    OP

    ground GBAtemp Advanced Fan

    Member
    909
    281
    Mar 22, 2007
    Netherlands
    I just started coding in c# recently (I made some programs in c# a few years ago, but that was more scraping:P). I need to pass a few strings from one form to another.

    My program does the following:

    - first it opens a login screen. 4 Values are entered here and passed to another form (if it can connect),
    - the second form is the main program, but it needs the values from the login screen to.

    so here is my code:

    program.cs:
    Code:
    static void Main()
         {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Login login = new Login();
                if (login.ShowDialog() == DialogResult.OK)
    
                {
    
                    string hostname = login.hostname;
                    string port = login.port;
                    string user = login.user;
                    string password = login.password;
    
                    Application.Run(new Main(hostname,port,user,password));
                }
    
                else
                {
                    Application.Exit();
                }
            }
    login.cs:
    Code:
    public partial class Login : Form
    
        {
            public string hostname {get; private set;}
            public string port { get; private set; }
            public string user { get; private set; }
            public string password { get; private set; }
          
            public Login()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
    
            {
    
                l_version.Text = "Version: XXXX";
                TextBox.hostname.Text = Properties.Settings.Default.hostname;
                TextBox.port.Text = Properties.Settings.Default.port;
                TextBox.user.Text = Properties.Settings.Default.user;
                encryption decr = new encryption();
                TextBox.password.Text = decr.decrypt(Properties.Settings.Default.password);
    
            }
    
            private void b_connecTextBox.Click(object sender, EventArgs e)
    
            {
                try
    
                {              
                    hostname = TextBox.hostname.Text;
                    port = TextBox.port.Text;
                    user = TextBox.user.Text;
                    password = TextBox.password.Text;               
    
                    if (CheckBox.remember.Checked)
                    {
                        Properties.Settings.Default.hostname = hostname;
                        Properties.Settings.Default.port = port;
                        Properties.Settings.Default.user = user;
                        encryption encr = new encryption();
                        Properties.Settings.Default.password = encr.encrypt(password);
                        Properties.Settings.Default.Save();
                    }        
    
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
    
                catch
                {
                    MessageBox.Show("Error: Could not connect.");
                }
            }
    main.cs:
    Code:
    partial class Main : Form
    
        {
            string hostname;
            string port;
            string user;
            string password;
     
           public Main(string Hostname,string Port, string User, string Password)
            {
                InitializeComponent();
                hostname = Hostname;
                port = Port;
                user = User;
                password = Password;
            }
    
            private void Main_Load(object sender, EventArgs e)
            {
            }
    
        }
    This works perfectly, but i was just wondering if I overlooked something and if there is a "better"way to do this.

    thanks in advance:)
     
  2. thecasperrojas

    thecasperrojas Member

    Newcomer
    10
    1
    Dec 11, 2014
    Grand Rapids, Michigan
    nice code man , i don't see a better way (even thought theres near to infinit ways to ago about your code) , so that said , theres acually no "correct way" to do this , theres only "the way it works for my knowlegde" so your good to go as long as it works
     
    ground likes this.
  3. filfat

    filfat Musician, Developer & Entrepreneur

    Member
    1,229
    858
    Nov 24, 2012
    Nope, seems perfect in my eyes :)
    Small tip though, create either an class or struct for the data and pass that one value instead of multiple values, that makes the code a lot easier to read and more flexible :)
    (by flexible I mean if you want to add more data without editing the function etc etc)
     
    ground likes this.
  4. sparky28000

    sparky28000 GBAtemp Regular

    Member
    245
    16
    Sep 20, 2008
    Netherlands
    One little thing you might want to use is this.
    It's a lot nicer than using an uppercase character.

    Code:
    private string hostname;
    private string port;
    private string user;
    private string password;
     
    public Main(string hostname, string port, string user, string password)
    {
        InitializeComponent();
        this.hostname = hostname;
        this.port = port;
        this.user = user;
        this.password = password;
    }
     
    ground and filfat like this.
  5. sparky28000

    sparky28000 GBAtemp Regular

    Member
    245
    16
    Sep 20, 2008
    Netherlands
    Woops double post.
     
  6. ground
    OP

    ground GBAtemp Advanced Fan

    Member
    909
    281
    Mar 22, 2007
    Netherlands
    I totally forgot about this thread:P. Oh well thanks for all the reactions:) and thanks for the tip sparky28000 and filfat.

    p.s.
    does anybody know a better sshclient for c#? basicly to control a python script on my raspberry. This one opens a new session every command so interaction with the python script isn't possible.
     
  7. LividPixel

    LividPixel Member

    Newcomer
    11
    1
    Jul 19, 2014
    United States
    A bit late, but as someone that's used C# for many years, my thoughts:
    The optimal convention for naming:
    Code:
    private string _hostname;
    private string _port;
    private string _user;
    private string _password;
     
    public Main(string hostname, string port, string user, string password)
    {
        InitializeComponent();
        _hostname = hostname;
        _port = port;
        _user = user;
        _password = password;
    }
    Rather than having to use 'this.', private variables could be prefixed with an underscore.
    (Some people use (this) and don't prefix with underscores, but I find the underscore to be more elegant, as I'll know something's scope from an identifier (private if it has an underscore, local if it doesn't).

    Inside parameters lists, you should keep everything camel case (somethingLikeThis). The same for local variables.


    Inside of your 'Login':

    Code:
    public string Hostname {get; private set;}
    public string Port { get; private set; }
    public string User { get; private set; }
    public string Password { get; private set; }
    Public variables and properties should generally be uppercase. (Also, if you're using 'Password' you should have the corresponding user variable be named 'Username', unless you're trying to coincide with what a library is using or something.)

    This won't change anything funtionally, but following the standard conventions will make your code more readable and you'll be able to identify something's accessibility by just it's name.
     
    ground likes this.
  8. ground
    OP

    ground GBAtemp Advanced Fan

    Member
    909
    281
    Mar 22, 2007
    Netherlands
    thanks for the tips, right now i am still looking for another sshclient, but it will definitely help as i progress into the program.