string array - how to remove an item?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: string array - how to remove an item?

  1. #1
    Join Date
    Nov 2008
    Posts
    13

    Unhappy string array - how to remove an item?

    I'm a beginner at C#. I have a simple string array set up where keys is set as:
    string[] keys = null;

    I then have a for loop with a conditional phrase. If the element meets the condition, I want to remove the element. For a one dimensional array, I was hoping to be able to use:
    keys[i].Remove(i,1);

    However it doesn't seem to do anything so that all elements are still included in the array. Is there a better/easier way of doing this?

    Help!

  2. #2
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: string array - how to remove an item?

    Use a List, nut an Array!!!!
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Nov 2008
    Posts
    13

    Re: string array - how to remove an item?

    Well the thing is, I'm taking over code for someone who already wrote everything. I just need to make some updates, and I'd like to do it quickly. If I do keep the code the way it is, is there a way to make it work?

  4. #4
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: string array - how to remove an item?

    There is no "good" way to make it sork, except for replacing the declaration.

    You would have to actually copy all of the elements you wanted into either:

    1) The same array (ie move down) and then track how many elements are "Active"

    2) A new array of a smaller size.

    For dynamicly sized (ie things you insert/delete to/from) you really want to use the collection classes.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: string array - how to remove an item?

    Code:
    Array.Clear(keys, i, 1);
    the Clear method accepts 3 parameters, the first being the array, second being the starting index, and the third being the number of elements to be deleted from the starting index.

    EDITED: However, this doesn't seem to actually remove the element. It seems to basically set the element to an empty string. So this probably won't work for your situation.
    Last edited by eclipsed4utoo; November 11th, 2008 at 02:21 PM.

  6. #6
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: string array - how to remove an item?

    Quote Originally Posted by eclipsed4utoo View Post
    Code:
    Array.Clear(keys, i, 1);
    the Clear method accepts 3 parameters, the first being the array, second being the starting index, and the third being the number of elements to be deleted from the starting index
    Clear does not delete/remove....

    Sets a range of elements in the Array to zero, to false, or to null, depending on the element type.
    Code:
    int [] a = { 1,2,3,4,5,6 }
    a.Clear(a, 2, 3)
    a == { 1,2,0,0,0, 6 }   // NOT: a == {1,2,6}
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    Nov 2008
    Posts
    13

    Re: string array - how to remove an item?

    is clear only a method of string arrays? when I use intellisense, I don't see a clear option.

  8. #8
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: string array - how to remove an item?

    Quote Originally Posted by Bugz View Post
    is clear only a method of string arrays? when I use intellisense, I don't see a clear option.
    Actually is it a static method....

    Code:
    Array.Clear(a,2,3);
    Sorry for the typo....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    Join Date
    Jun 2008
    Posts
    2,477

    Re: string array - how to remove an item?

    If you just refactor a bit and change the array to a list, you can then change the areas of your code that take that array as a parameter to use the List's .ToArray() method. It would probably only take a few minutes.

  10. #10
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: string array - how to remove an item?

    Quote Originally Posted by BigEd781 View Post
    If you just refactor a bit and change the array to a list, you can then change the areas of your code that take that array as a parameter to use the List's .ToArray() method. It would probably only take a few minutes.
    Just ve very carefuly if the element type is a value type. ToArray() will return a COPY and changes to the COPY will NOT be reflected in the ORIGINAL.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  11. #11
    Join Date
    Jun 2008
    Posts
    2,477

    Re: string array - how to remove an item?

    Ahhh, that is very true.

  12. #12
    Join Date
    Nov 2008
    Posts
    13

    Re: string array - how to remove an item?

    I like the way the clear method worked. Once it set my elements to null, I added in code to later ignore the null elements. Thank you so much for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center