My first post ever anywhere! :)

As a novice I may be asking a stupid question here. Although arraysize exists, postcode array doesn't when I try to work with it in the addToArray method (and yes I know there are some other issues but I'll sort those later):

namespace Postcodes
{
    public partial class _Default : System.Web.UI.Page
    {
        string[] PostcodeArray = new string[] { "  " };
        int ArraySize = 0;
        
        protected void Page_Load(object sender, EventArgs e)
       
        {
            if (!IsPostBack)                
            ltrListofCodes.Text = "Codes you enter will appear here";
        }

        protected void btnAddCode_Click(object sender, EventArgs e)
        {            
            string postCodeRegEx =
@"(^((([A-PR-UWYZ])([0-9][0-9A-HJKS-UW]?))|(([A-PR-UWYZ][A-HK-Y])([0-9][0-9ABEHMNPRV-Y]?))\s{0,2}(([0-9])([ABD-HJLNP-UW-Z])([ABD-HJLNP-UW-Z])))|(((GI)(R))\s{0,2}((0)(A)(A)))$)|(^
*$)";
            //Validate the postcode and if its good then add it to the optimisation problem
            if (System.Text.RegularExpressions.Regex.IsMatch(txtPostcode.Text, postCodeRegEx) ==
            false)                          
                ltrListofCodes.Text = "Bad postcode:" + "  " + txtPostcode.Text;
            else
                addToArray(txtPostcode.Text); 
        }

        protected void addToArray(string nextPostcode)
        {
           // Build the string array of valid postcodes
            PostcodeArray(ArraySize) = nextPostcode;
            ArraySize++;
            ;
        }

        protected void btnOptimise_Click(object sender, EventArgs e)
        {
            Response.Redirect("Results.aspx");
        }
           
    }
}
PostcodeArray(ArraySize) = nextPostcode;

That's not how you work work with an array. Use square brackets instead of parentheses. However, for this particular exercise, the array simply is not going to suit your needs. List<T> of the System.Collections.Generic namespace would be more appropriate. An example usage is below.

List<string> myStrings = new List<string>();
myStrings.Add("String 1");
myStrings.Add("String 2");

Also, how are you maintaining these values on postback? As far as I can tell, the prior entries are not being held on each submission.

Thank you, I feel v stupid :$, although I was completely fooled by the error caption. I take your point about Collections as I came across them while researching how to use an array. My plan was simply to hold the current array in a temp array, redim the current array then add the latest member.

The postback problem is still waiting for me, I guess it means my array won't live long enough to be of use. When complete it should for part of a problem that gets passed to an optimisation engine. On the web page the user enters postcodes until done then hits the Optimise button, at which point the array gets passed to the Optimiser (which I bought by the way).

Thanks again for your help.

PostcodeArray(ArraySize) = nextPostcode;

That's not how you work work with an array. Use square brackets instead of parentheses. However, for this particular exercise, the array simply is not going to suit your needs. List<T> of the System.Collections.Generic namespace would be more appropriate. An example usage is below.

List<string> myStrings = new List<string>();
myStrings.Add("String 1");
myStrings.Add("String 2");

Also, how are you maintaining these values on postback? As far as I can tell, the prior entries are not being held on each submission.

This article has been dead for over six months. Start a new discussion instead.