CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: How to deal with list's that contain null values?

  1. #1
    Join Date
    Jul 2007
    Posts
    609

    How to deal with list's that contain null values?

    Lets say I have a list of items and store it somewhere. Later on in the program one of those items becomes null but is still in the list. How do I go about removing that? Anything I try to do with that item will just result in a crash. Is there a cleanup function for lists that will automaticly get rid of null stuff?
    http://www.uovalor.com :: Free UO Server

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: How to deal with list's that contain null values?

    Your post makes no real sense. Either the item in the list IS null, or it is a valid object.
    Code:
    Dictionary d = new Dictionary()
    d.Add("Key", null);
    d.Add("Key", new object());
    NOTHING will cause a value to "magically become" null, unless you explicitly assign null to the value in question.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; 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
    Jul 2007
    Posts
    609

    Re: How to deal with list's that contain null values?

    Well thats what I meant, somewhere else in the program it gets set as null, but its still in that list. (in this case a dictionary, but could be a List or ArrayList).

    Basically I run a MMO server, in a tournement system I keep a list of arenas that are available for use by the system, so there is a list that keeps track of it. When the world saves it saves the list, and the actual arenas are also saved seperately. But lets say one of them gets deleted, it will go null, but that list still gets saved and still gets loaded on startup. So when later on it tries to find an arena it will crash in the foreach when it gets to that null one. I can check against null, but it will still be in that list, so I need a way to remove it from that list. Basically something like:

    foreach (Arena a in arenas)
    {
    if(a==null)dosomething();
    }
    http://www.uovalor.com :: Free UO Server

  4. #4
    Join Date
    Jan 2007
    Posts
    491

    Re: How to deal with list's that contain null values?

    There should be a Remove method. Try it.

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

    Re: How to deal with list's that contain null values?

    if, at some point, you update the value to a null value, remove it instead of updating to a null

  6. #6
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: How to deal with list's that contain null values?

    Quote Originally Posted by eclipsed4utoo
    if, at some point, you update the value to a null value, remove it instead of updating to a null
    The only problem is its a Dictionary. If he wants to maintain the key value, but assign the actual value as null, it would be beneficial to keep the object in the Dictionary.

    However, if you don't care about the null object, just remove it entirely from the list. Itll keep things cleaner =D
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: How to deal with list's that contain null values?

    Quote Originally Posted by RaleTheBlade
    The only problem is its a Dictionary. If he wants to maintain the key value, but assign the actual value as null, it would be beneficial to keep the object in the Dictionary.

    However, if you don't care about the null object, just remove it entirely from the list. Itll keep things cleaner =D
    In many cases it may be eaiser to put a "marker" object inside the dictionary rather than null.

    If all of the objects in the dictionary support some interface, then a marker object which does nothing for each exposed method will allow for iteration through the dictionary without special consideration.

    This makes things much easier if the dictionary is (oor may be in the future) itrated through in many locations throughout the application.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; 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

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




On-Demand Webinars (sponsored)