0

I'm having problems in passing the values of the array.

I created an array in a class, then I instantiate it in frmMain(form1) then i put some values in the array(I signed up I put username and password in the array). frmMain should pass the values in the the array right?

My problem is when I instantiate the class(which holds the array) in frmProfile(form2) its like i never put some values in it.

In short, I want the array to be accessible in all forms. Do I need a constructor method?(something like get; set;?).

public class Tagasalo //this is the class i created
{
    public String[,] array = new String[5, 2];

}

heres my code in frmMain(form1)

    Tagasalo arr = new Tagasalo();
    frmProfile profile = new frmProfile();

    int row;
    bool validInput = true;
    int tries = 3;


    public frmMain()
    {
        InitializeComponent();

    }

    public void btnSignUp_Click(object sender, EventArgs e)
    {
        if (txtUsernameS.Text.Trim() == String.Empty)
        {
            errorProvider1.SetError(txtUsernameS, "Please put a Username.");
        }
        else if (txtUsernameS.TextLength > 1 && txtUsernameS.TextLength < 4)
        {
            errorProvider1.SetError(txtUsernameS, "Your Username must have 4 or more characters.");
        }
        else
        {
            errorProvider1.Clear();
        }

        if (txtPasswordS.Text == String.Empty)
        {
            errorProvider2.SetError(txtPasswordS, "Please put a Password.");
        }
        else if (txtPasswordS.TextLength > 1 && txtPasswordS.TextLength < 6)
        {
            errorProvider2.SetError(txtPasswordS, "Your Password must have 6 or more characters.");
        }
        else
        {
            errorProvider2.Clear();
        }

        for (row = 0; row < 5; row++)
        {
            if (txtUsernameS.TextLength >= 4 && txtPasswordS.TextLength >= 6)
            {
                if (txtConfirmPassword.Text != txtPasswordS.Text)
                {
                    errorProvider3.SetError(txtConfirmPassword, "Your Password does not match.");
                }
                else if (txtConfirmPassword.Text == txtPasswordS.Text)
                {
                    if (arr.array[row, 0] == null && arr.array[row, 1] == null)
                    {
                        arr.array[row, 0] = txtUsernameS.Text;
                        arr.array[row, 1] = txtPasswordS.Text;
                        MessageBox.Show("Signed-Up Successfully!");

                        break;
                    }
                    else if(txtUsernameS.Text == arr.array[row,0])
                    {
                        MessageBox.Show("Username already Used.");
                        break;
                    }
                }
            }
        }
        if (row > 5)
        {
            MessageBox.Show("Sorry, maximum number of accounts has been reached.");
        }
    }

here's my code in frmProfile(form2)- In this form I just want the label to show the username of the user.

    Tagasalo arr = new Tagasalo();
    int row;

    public frmProfile()
    {
        InitializeComponent();

    }

    private void changePasswordToolStripMenuItem_Click(object sender, EventArgs e)
    {
        frmChangePassword changepass = new frmChangePassword();
        changepass.Show();
        this.Hide();
    }

    private void logOutToolStripMenuItem_Click(object sender, EventArgs e)
    {
        frmMain logout = new frmMain();
        logout.Show();
        this.Hide();
    }

    public void frmProfile_Load(object sender, EventArgs e)
    {
        for (row = 0; row < 5; row++ )
        {
            if(arr.array[row,0] == arr.array[row,1])
            {
                lblUsername.Text = arr.array[row, 0];
                break;
            }
        }
    }

then, i want to change the password. replace the value of the array and when i log-out the array is empty again. I cant log-in again with the same account.

    Tagasalo arr = new Tagasalo();

    public frmChangePassword()
    {
        InitializeComponent();
    }

    private void btnSaveChanges_Click(object sender, EventArgs e)
    {
        if (txtCurrentPassword.Text == String.Empty)
        {
            errorProvider1.SetError(txtCurrentPassword, "Please type your Current Password.");
        }
        else
        {
            errorProvider1.Clear();
        }


        if (txtNewPassword.Text == String.Empty)
        {
            errorProvider2.SetError(txtNewPassword, "Please type your New Password.");
        }
        else
        {
            errorProvider2.Clear();
        }
        if (txtConfirmNewPassword.Text != txtNewPassword.Text)
        {
            errorProvider3.SetError(txtConfirmNewPassword, "Re-type your New Password.");
        }
        else
        {
            errorProvider3.Clear();
        }

        if(txtCurrentPassword.Text != String.Empty && txtNewPassword.Text != String.Empty && txtConfirmNewPassword.Text == txtNewPassword.Text)
        {

            MessageBox.Show("Changed Password Successfully!");
            frmProfile profile = new frmProfile();

            profile.Show();
            this.Hide();
        }
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        frmProfile profile = new frmProfile();
        profile.Show();
        this.Hide();
    }

Sorry if it's too long. I've been searching the net for days after school and I cant find the answer.

2
  • Have you published your project? just using the 'debugger' means nothing is actually 'saved' once the application closes. To be honest i'm not certain if it will save that way either without saving to a db (which you've said is not suitable). Commented Aug 12, 2014 at 14:34
  • Yes. I know that nothing will be saved once the application closes. Commented Aug 12, 2014 at 16:09

2 Answers 2

0

What do you mean with "you want the variable accessible in all forms" ?

To acess variables from another class you have to write classname.variablename to access them, for your example this would be:

Tagasalo.array ... do stuff ...

If you want to create a class which describes variables and/or methods of objects you create via the constructor of this class and then access those I would recommend reading about "Classes", "Objects", "Instances" first and if this does not help you ask with this knowledge again.

Another thing that might be helpfull: If you want a class which describes variables and/or methods and the rest of your application shall be able to just create one single and only instance of this class reading about "Singleton-classes".

Hope this helps, specify what you want to do and I might be able to help you more specific.

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

5 Comments

What I mean is if I inserted a value in the array, I want the value to be accessible in other forms too. But the thing is, If I instantiate the class which holds the array in another form. The array seems to be empty.
Because both in frmMain and frmProfile you create a new object of Tagasalo and both have the datas described in Tagasalo, but if you change the datas of arr in 1 form this does not change the datas of the other arr in the second form because they are different objects.
How can i access the datas of array in form 1 to the other form?
I think creating a "Singleton class" where you can just create a single instance would be a good way, you can just create one instance and therefore when you change something on the instance in any of your forms the changes are also made in the SAME instance you use in your other forms. This link ("msdn.microsoft.com/en-us/library/ff650316.aspx") should guide you trough the process.
Content not found in "msdn.microsoft.com/en-us/library/ff650316.aspx"
0

In all honesty, I wouldn't recommend using a global variable, as it uses unnecessary resources.

However,

What you are explaining/describing is a global variable. If you are using windows forms (which I think you are), you can create a class (right click on your project name>add>class). Call it 'MyArrayClass'. Then (as you have already said) use the constructor MyArray(){get;set} You can then access it via : MyArrayClass.MyArray[i] where [i] is the index you want.

I'll edit this post once I find some good documentation as well.

EDIT: This works great (since you are new) : http://www.dotnetperls.com/global-variable.

Just pass it in the constructor of Form2:

Form2 form2 = new Form2(arr);
form2.ShowDialog();

Then in the Form2 constructor:

public Form2(List<double> arr)
{
   //do stuff with it here
}

EDIT 2: For what you are doing, this would be better for you: http://codemyne.net/articles/asp/creating-a-simple-registration-or-signup-and-login-or-signin-form-using-asp.net-and-sqlserver.aspx?visitid=171&type=2

Edit 3

static class Global
{


 private static string _globalVar = "";

    public static string GlobalVar
    {
        get { return _globalVar; }
        set { _globalVar = value; }
    }
}

and for using any where you can write:

    GlobalClass.GlobalVar = "any string value"

This was used for a string value.

13 Comments

that's what I did. I added a class. Where will I put the constructor? In form1? or in all forms? or in the class that I added? @jbutler483
Ensure it is public, then call it via: MyClass.MyGetMethod()
what will be the value? MyArray() { get { return value; }
My assignment is to use array. I tried to ask if I can use a database but they refused.
|

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.