C#, Am I passing strings correctly?

ground

Well-Known Member
OP
Member
Joined
Mar 22, 2007
Messages
907
Trophies
0
XP
597
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
597
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
597
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
  • Xdqwerty @ Xdqwerty:
    although im off school that day
  • BakerMan @ BakerMan:
    today is the 7th anniversary of Captain Underpants, which was actually a surprisingly good movie
  • K3Nv2 @ K3Nv2:
    That's been around since I was in elementary
  • Xdqwerty @ Xdqwerty:
    @BakerMan, i recall watching it on fox
  • SylverReZ @ SylverReZ:
    Who watches FOX nowadays? Only reason to get alt-right viewers interested.
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, that was like when i was 10
  • SylverReZ @ SylverReZ:
    @Xdqwerty, We didn't have FOX Kids over here, back then it was Jetix.
  • SylverReZ @ SylverReZ:
    Similar to FOX Kids. I remember when Sonic X aired.
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, the latin american fox feed doesnt air news, it airs movies and tv shows
    +1
  • SylverReZ @ SylverReZ:
    Ah, I see.
  • Xdqwerty @ Xdqwerty:
    and i wasnt even born when fox kids/jetix was alive
  • Xdqwerty @ Xdqwerty:
    also a couple years ago latin american fox got renamed to star channel
  • SylverReZ @ SylverReZ:
    Yes?
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, sorry
    i thought you didnt read the last couple messages before quoting you agin
  • BakerMan @ BakerMan:
    ah sonic x, that show is valid
  • BakerMan @ BakerMan:
    (of course the mf with a sonic pfp would say that 😭)
  • BigOnYa @ BigOnYa:
    @Xdqwerty you are not dumb or stupid, you need to stop saying that, you are very smart, even about alot of things I have no clue about. We all love you here, like a big family.
  • K3Nv2 @ K3Nv2:
    Yeah @BigOnYa love me so much he started yelling at me for having my shoes on in the bed when he caught me with his wife
    +1
  • BigOnYa @ BigOnYa:
    True, I don't want mud crumbs in my bed, and btw you left without paying your tab. Ill add it to next month but getting tired of extending.
  • K3Nv2 @ K3Nv2:
    I hope silent hill 2 turns out good
    +1
  • BigOnYa @ BigOnYa:
    I seen that, hope so too, the first was so epic back then.
  • K3Nv2 @ K3Nv2:
    Didn't look into much is it just a remake
  • BigOnYa @ BigOnYa:
    Only thing drive me crazy is all these new horror games nowadays is its so dark, all time. I can't even play them. I understand they trying to create spooky but be creative a little, not just darkness with pop out scare scenes.
    BigOnYa @ BigOnYa: Only thing drive me crazy is all these new horror games nowadays is its so dark, all time. I...