Results 1 to 9 of 9

Thread: Creating a function that deletes data!

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    4

    Creating a function that deletes data!

    Hi Guys. Im really stuck on this at the moment.

    I want to create a function that deletes some specific data that I have entered. The following C# code creates an ArrayList. It then adds a number of “BoxesOfWidgets” to the ArrayList, adding Widgets within each box. I can use VB to create the function, it does not have to be in C#.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                ArrayList colBoxesOfWidgets = new ArrayList();
    
                colBoxesOfWidgets.Add(new BoxOfWidgets("Cardboard"));
                ((BoxOfWidgets)colBoxesOfWidgets[0]).colWidgets.Add(new Widget("The blue widget", 12));
                ((BoxOfWidgets)colBoxesOfWidgets[0]).colWidgets.Add(new Widget("The red widget", 15));
                ((BoxOfWidgets)colBoxesOfWidgets[0]).colWidgets.Add(new Widget("The silver widget", 6));
                ((BoxOfWidgets)colBoxesOfWidgets[0]).colWidgets.Add(new Widget("The green widget", 52));
    
                colBoxesOfWidgets.Add(new BoxOfWidgets("Metal"));
                ((BoxOfWidgets)colBoxesOfWidgets[1]).colWidgets.Add(new Widget("The gold widget", 9));
                ((BoxOfWidgets)colBoxesOfWidgets[1]).colWidgets.Add(new Widget("The orange widget", 115));
                ((BoxOfWidgets)colBoxesOfWidgets[1]).colWidgets.Add(new Widget("The pink widget", 1));
    
                colBoxesOfWidgets.Add(new BoxOfWidgets("Metal"));
                ((BoxOfWidgets)colBoxesOfWidgets[2]).colWidgets.Add(new Widget("The grey widget", 12));
                ((BoxOfWidgets)colBoxesOfWidgets[2]).colWidgets.Add(new Widget("The black widget", 15));
                ((BoxOfWidgets)colBoxesOfWidgets[2]).colWidgets.Add(new Widget("The white widget", 19));
                ((BoxOfWidgets)colBoxesOfWidgets[2]).colWidgets.Add(new Widget("The brown widget", 60));
                ((BoxOfWidgets)colBoxesOfWidgets[2]).colWidgets.Add(new Widget("The peach widget", 16));
    
    
                GetRidOfTheSmallWidgets(colBoxesOfWidgets);
            }
    
    
            public ArrayList GetRidOfTheSmallWidgets(ArrayList colBoxesOfWidgets){
    
                //Place code here
                //It should remove all widgets that have lengths lower than 20.
    
                return colBoxesOfWidgets;
    
            }
        }
    
        class BoxOfWidgets
        {
            public string boxType;
            public ArrayList colWidgets;
    
            public BoxOfWidgets(string newBoxType)
            {
                boxType = newBoxType;
                colWidgets = new ArrayList();
            }
        }
    
        class Widget
        {
            public string name;
            public float length;
    
            public Widget(string newName, float newLength)
            {
                this.name = newName;
                this.length = newLength;
            }
        }
    As you can see I need to get rid of all of the widgets with lengths less than 20. I need a method to search through the ArrayList of boxes, and remove the small widgets from them.

    Please help! Anything will be really appreciated!

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Creating a function that deletes data!

    What version of VS are you using?
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Creating a function that deletes data!

    Isn't is just as simple as looping through the ArrayList items and checking widgets length is less than 20 and removing it.
    Or you are looking for something else?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    4

    Re: Creating a function that deletes data!

    Im not really using a VB software at the moment.. just need to figure this code out on paper and Im really struggling. I need to first add the widgets.. and then delete it.

  5. #5
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Creating a function that deletes data!

    Well, the simple way is what Pradeep suggests. Loop through them and delete the ones < 20.

    If you were using VS2005, I'd suggest using a Generic List rather than an ArrayList since they're much more versatile.

    If you were using VS2008, I'd tell you how you could do it using LINQ.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    4

    Re: Creating a function that deletes data!

    The problem is.. this is not code I have created myself so I have no idea how to loop in VB! ( I coluld do it in PL/SQL, but not in VB!)

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Creating a function that deletes data!

    so why post on a VB forum if your not intending to do it in VB :S
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    4

    Re: Creating a function that deletes data!

    No I meant that I'd be able to do it in PL/SQL.. but I need it done in VB and have no idea how to get it done

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Creating a function that deletes data!

    If you ARE going to get it done, you will have to choose a version of the language first. As long as you choose 2005 or newer, then ArrayList is not the way to go. List (of BoxOfWidgets) would be the right way to do this.

    The loop would look something like this, assuming a List called WidgetList:
    Code:
    Dim x as integer
    
    For x = WidgetList.Count-1 to 0 step -1
     If WidgetList(x).Length < 20 then
       WidgetList.RemoveAt(x)
     End if
    Next
    That snippet makes some pretty broad assumptions, such as the assumption that Length is a property containing the length. The loop runs from the top down to 0 because you will be removing items from the list. This should screw up the loop indexing if the loop went from bottom to top (though somebody will correct me if Lists don't work like arrays in this manner, I've never tested it, but Lists are different animals).
    My usual boring signature: Nothing

Posting Permissions

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



Click Here to Expand Forum to Full Width