0

I'm trying to add new values input by the user on a separate windows form into the following array:

public class NameValue
  {
    public string Name;
    public string Value;
    public NameValue() { Name = null; Value = null; }
    public NameValue(string name, string value) { Name = name; Value = value; }
  }

   public class DefaultSettings
  {
    public static NameValue[] Sites = new NameValue[] 
    {
        new NameValue("los angeles, CA", "http://losangeles.craigslist.org/"),
    };

    public static NameValue[] Categories = new NameValue[] 
    {
        new NameValue("all for sale", "sss"),
    };
   }

How do I add the new values to the array while keeping the values of the old array?

Edit

I tried using Mr. Noren's function:

        static void AddValueToSites(NameValue newValue)
    {
        int size = DefaultSettings.Sites.Length;
        NameValue[] newSites = new NameValue[size + 1];
        Array.Copy(DefaultSettings.Sites, newSites, size);
        newSites[size] = newValue;
        DefaultSettings.Sites = newSites;
    }
    private void button1_Click(object sender, EventArgs e)
    {
        NameValue newSite = new NameValue("Test, OR", "http://portland.craigslist.org/"); 
        AddValueToSites(newSite);
        Close();
    }

But that's not working... The class I am getting data from is:

public partial class Location : Office2007Form
{
    public Location()
    {
        InitializeComponent();
    }
    static void AddValueToSites(NameValue newValue)...
    private void button1_Click(object sender, EventArgs e)...
}
4
  • 3
    Why are you using an array instead of a mutable collection like List<T>? Commented Mar 8, 2012 at 1:18
  • If you want to add elements, an array is not the proper construct. As @M.Babcock suggested, something that implements IList<T> would be a much more appropriate strategy. Commented Mar 8, 2012 at 1:32
  • 1
    What class do you want to add values from? Commented Mar 8, 2012 at 2:09
  • What's the specific problem? Is it that it is too protected? You may need to decorate with protected, internal or public on the static void Add... functions. Commented Mar 8, 2012 at 15:39

4 Answers 4

5

You cannot change the size of an array, ever. You would need to use something like a List for that.

Since you're using name/value pairs you should consider using Dictionary<TKey,TValue>.

Finally, if you're looking to have different classes contribute to the contents of the arrays, then that's not going to happen.

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

Comments

4

You can't add to an array, whether it's in another class or not.

Use List<NameValue> instead of NameValue[] and then you can use Sites.Add(...).

Comments

2

If you're absolutely married to the idea of using an array versus some more flexible collection, you should implement some AddX() methods in the class that is defining the base values. Those methods would take care of inserting the new values in the array. If you're not concerned with multithreading issues, it can be very simple:

(Warning: code is from my head and not tested)

static void AddValueToSites(NameValue newValue)
{
    int size = Sites.Length;
    NameValue[] newSites = new NameValue[size+1];
    Array.Copy(Sites, newSites, size);
    newSites[size] = newValue;
    Sites = newSites;
}

And again for Categories.

Like others suggested, using List<NameValue> or Dictionary<string,string> would be options that would be more fluid. These already have Add, Remove, Contains, etc. - basically all you need when you need to manipulate arrays.

1 Comment

Wow thanks guys! So, how would I use your function? private void OK_Click(object sender, EventArgs e) { NameValue newSite = new NameValue(textBox1.Text,textBox2.Text); AddValueToSites(newSite); Close(); }
1

Arrays are not mutable in place, so if you resize them (using any approach) it'll result in a different instance being created (this is what happens in VB.NET's ReDim but they hide it from you).

The simplest way to resize an array in C# is to use the Concat extension method (requires System.Linq):

string[] myArray = new string[] { "Hello", "World" };
myArray = myArray.Concat(new string[] { "Something", "new" };

myArray will now be 4 elements deep. I don't think this will work between classes though (haven't tried though).

Comments

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.