-2

I'm making easy password generator, but i cant pick int from try and string from if. Here's the code. I hope you help me. I cant make this I as textbox and i cant do nothing with it.

private void button3_Click(object sender, EventArgs e)
{
    try
    {
        int i = Int32.Parse(textBox2.Text);
        return;
    }
    catch
    {

    }
    CreatePassword(i);
}

and here is part of CreatePassword function

public string CreatePassword(int length)
{
    if (checkBox2.Checked)
    {
        const string src = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        return src;
    }
    else
    {
        const string src = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        return src;
    }
}
L_J
  • 2,351
  • 10
  • 23
  • 28
nakinfk
  • 1
  • 2

4 Answers4

2

There are several problems with your code. First, you're trying to access the variable i outside of the scope in which it is declared; it's not visible outside of the try statement. Second, it seems like you're expecting the password to be generated from the integer you parsed, but you're explicitly returning before the password can be created. Thirdly, you're not doing anything with the created password, just throwing it away.

Try the following:

    private void button3_Click(object sender, EventArgs e)
    {
        try
        {
            int i = Int32.Parse(textBox2.Text);
            string password = CreatePassword(i);
            // TODO: use the 'password' string for something.
            return;
        }
        catch
        {

        }
    }

You should also consider using int.TryParse instead, which won't throw an exception.

    if (int.TryParse(textbox2.Text, out int i) {
        string password = CreatePassword(i);
        // Do something with 'password' 
    } else {
        // Display an error.
    }
John Källén
  • 7,551
  • 31
  • 64
  • I want to get int from textbox as password lenght. Already fixed this try thx. But still i cant return value from IF. I want to return value called "src" back into function. – nakinfk Apr 13 '21 at 09:25
1

From your example, it looks like all your need is Int32.TryParse:

int.TryParse(textBox2.Text, out int i);
CreatePassword(i);

However, to answer your original question: you need to initialize i variable outside of the try block in order to be able to use it after it. For instance:

        int i = 0;
        try
        {
            i = int.Parse("test");
        }
        catch
        {

        }
        Console.WriteLine(i); // 0
Andrii
  • 133
  • 1
  • 10
  • 1
    what exactly isn't working for you? if you are not satisfied with the idea of having 0 in `i`, as it may be a valid parse result, you can use `int? i = null;` and then check `if(i != null)` after `try-catch` block – Andrii Apr 13 '21 at 09:54
  • I have problem in IF. It called "variable src not exist" when i setup it inside if. so my question is how can i return this variable src to use in out of this statement – nakinfk Apr 13 '21 at 10:09
  • If you're talking about your 2nd code snippet, `CreatePassword` method - move `src` declaration outside of `if-else` block and just assign its value inside it. So `CreatePassword` should be: `public string CreatePassword(int length) { string src = string.Empty; if (checkBox2.Checked) { src = "ifpassword"; } else { src = "elsapassword"; } return src; }` – Andrii Apr 13 '21 at 10:19
  • I should mention that I agree with John Källén and RvdK, as they've made valid points about the logic itself. But I wanted to just show you how to move forward in development, leaving the business-logic side at your discretion. – Andrii Apr 13 '21 at 10:26
0

You logic is a bit flawed. If Textbox2 does not contain a valid integer, you ignore the exception and just create a password. What kind of password you expect to create?

I think you mean to do something like this:

private void button3_Click(object sender, EventArgs e)
    {
        try
        {
            int i = Int32.Parse(textBox2.Text);
            CreatePassword(i);
        }
        catch
        {
            // Show a messagebox or something
        }
    }
RvdK
  • 19,580
  • 4
  • 64
  • 107
  • Textbox2 have string with some number. So i wanted to convert to int with Int32.Parse. I just want to generate a simple string. If you click on checkbox then you gen password with numbers. If not checked then you make pass without num – nakinfk Apr 13 '21 at 09:22
0

Guys all thanks for help. I did it. Here you have my source of my application as thanks for you all. My app completely work and i believe you understand my logic :D

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Password_generator
{
    public partial class Form1 : Form
    {
        private bool _dragging = false;
        private Point _start_point = new Point(0, 0);
        public Form1()
        {
            InitializeComponent();
        }

        public string CreatePassword(int length)
        {
            string src;
            var sb = new StringBuilder();
            Random RNG = new Random();

            src = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

            if (checkBox2.Checked)
            {
                src += "1234567890";
            }
            if (checkBox3.Checked)
            {
                src += "@#$%^&*()";
            }
            for (var i = 0; i < length; i++)
            {
                var c = src[RNG.Next(0, src.Length)];
                sb.Append(c);
            }
            textBox1.Text = sb.ToString();

            if (checkBox1.Checked)
            {
                try
                {
                    File.AppendAllText("hesla.txt", textBox1.Text + Environment.NewLine);
                }
                catch(Exception o)
                {
                    MessageBox.Show("Něco se nepovedlo! " + Environment.NewLine + "(" + o.Message + ")");
                }
            }

            return textBox1.Text;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                int i = Int32.Parse(textBox2.Text);
                CreatePassword(i);
            }
            catch
            {
                MessageBox.Show("Musíš zadat číslo!");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            _dragging = false;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (_dragging)
            {
                Point p = PointToScreen(e.Location);
                Location = new Point(p.X - this._start_point.X, p.Y - this._start_point.Y);
            }
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            _dragging = true;
            _start_point = new Point(e.X, e.Y);
        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {

        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                Clipboard.SetText(textBox1.Text);
            }
            catch
            {

            }
            
        }
    }
}
nakinfk
  • 1
  • 2