C#, Am I passing strings correctly?

ground

Well-Known Member
OP
Member
Joined
Mar 22, 2007
Messages
907
Trophies
0
XP
572
Country
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:)
 

Aionmagan

Active Member
Newcomer
Joined
Dec 11, 2014
Messages
26
Trophies
0
Age
30
Location
Ciales
XP
125
Country
Puerto Rico
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
 
  • Like
Reactions: ground

filfat

CTO @ Nordcom Group Inc.
Member
Joined
Nov 24, 2012
Messages
1,261
Trophies
1
Location
Gothenburg, Sweden
Website
www.sweetsideofsweden.com
XP
1,749
Country
Sweden
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)
 
  • Like
Reactions: ground

sparky28000

Well-Known Member
Member
Joined
Sep 20, 2008
Messages
245
Trophies
0
XP
253
Country
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;
}
 
  • Like
Reactions: ground and filfat

ground

Well-Known Member
OP
Member
Joined
Mar 22, 2007
Messages
907
Trophies
0
XP
572
Country
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.
 
Joined
Jul 19, 2014
Messages
11
Trophies
0
XP
93
Country
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.
 
  • Like
Reactions: ground

ground

Well-Known Member
OP
Member
Joined
Mar 22, 2007
Messages
907
Trophies
0
XP
572
Country
Netherlands
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.
thanks for the tips, right now i am still looking for another sshclient, but it will definitely help as i progress into the program.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    K3Nv2 @ K3Nv2: Gong