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.

Learner
  • 1
  • 1
  • 6
  • 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). – jbutler483 Aug 12 '14 at 14:34
  • Yes. I know that nothing will be saved once the application closes. – Learner Aug 12 '14 at 16:09

2 Answers2

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.

Paul Weiland
  • 727
  • 10
  • 24
  • 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. – Learner Aug 12 '14 at 12:10
  • 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. – Paul Weiland Aug 12 '14 at 12:23
  • How can i access the datas of array in form 1 to the other form? – Learner Aug 12 '14 at 12:28
  • 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 ("http://msdn.microsoft.com/en-us/library/ff650316.aspx") should guide you trough the process. – Paul Weiland Aug 12 '14 at 13:25
  • Content not found in "msdn.microsoft.com/en-us/library/ff650316.aspx" – Learner Aug 12 '14 at 13:34
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.

jbutler483
  • 24,074
  • 9
  • 92
  • 145
  • 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 – Learner Aug 12 '14 at 12:27
  • Ensure it is public, then call it via: MyClass.MyGetMethod() – jbutler483 Aug 12 '14 at 12:29
  • what will be the value? MyArray() { get { return value; } – Learner Aug 12 '14 at 12:35
  • http://stackoverflow.com/questions/8734413/declaring-and-using-global-arrays-c-sharp – jbutler483 Aug 12 '14 at 12:37
  • My assignment is to use array. I tried to ask if I can use a database but they refused. – Learner Aug 12 '14 at 12:39
  • Why is it the Form2 constructor became a List? – Learner Aug 12 '14 at 12:39
  • Sorry if I keep asking questions. – Learner Aug 12 '14 at 12:40
  • Lists just mean that there is 'no' limitation as to how many users you can have (i.e. can have more than your array of 5) as it is dynamic. I haven't used an array in this way (globally) as the .Net framework no longer uses them (not good practise). What I will do though is go through my old work (from a while back) and get you an example (hopefully I still have one). – jbutler483 Aug 12 '14 at 12:42
  • Also, make your array static. – jbutler483 Aug 12 '14 at 12:46
  • Member 'FirstCaseStudy.Tagasalo.array' cannot be accessed with an instance reference; qualify it with a type name instead – Learner Aug 12 '14 at 12:49
  • and if you remove it? – jbutler483 Aug 12 '14 at 13:21
  • no errors. but still cant pass the value of the array to other forms. – Learner Aug 12 '14 at 13:33
  • public String[,] array = new String[5, 2]; public String username = ""; public String password=""; int row; public String Username { get { return array[row,0]; } set { array[row,0]=username; } } public String Password { get { return array[row,1]; } set { array[row, 1] = password; } } is this right? – Learner Aug 12 '14 at 13:37