PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Check if an array has been modified?-VBForums
Results 1 to 8 of 8

Thread: [RESOLVED] Check if an array has been modified?

  1. #1

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,346

    Resolved [RESOLVED] Check if an array has been modified?

    Hi,

    How would I use a bool to determine whether an array has been modified or not?

    Thanks,


    Nightwalker
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  2. #2
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Check if an array has been modified?

    Think you're either gonna have to loop through each element in the array, or wrap it in a class that has a flag to indicate whether it's modified.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  3. #3
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Check if an array has been modified?

    Or you could implement a changed event for the custom object and fire that to tell you when the array has items added, changed or removed. That is probably one of the best solutions here.
    My Blog.

    Ryan Jones.

  4. #4

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,346

    Re: Check if an array has been modified?

    I take it that it would be simiar to this method?

    c# Code:
    1. string message = "Suburb not found!";
    2.                 int index = 0;
    3.                 bool found = false;
    4.                 while (index < cCounter && !found)
    5.                 {
    6.                     if (txtSuburb.Text.ToUpper() == cSuburbs[index].ToUpper())
    7.                     {
    8.                         found = true;
    9.                         message = cPostcodes[index];
    10.                     }
    11.                     else
    12.                     {
    13.                         index++;
    14.                     }
    15.                 } txtPostcode.Text = message;
    16.                 cmbSuburb.Text = txtSuburb.Text;

    Although, the above example compares the content of a textbox with the contents of an array whereas I want to compare two arrays.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  5. #5
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Check if an array has been modified?

    To check if two arrays are the same you would do something like this:

    Code:
    public bool areArraysIdentical(string[] a1, string[] a2)
    {
        for (int i = 0; i < a1.Length; i++)
        {
            if (!Object.Equals(a1[i], a2[i]))
            {
                return false;
            }
        }
        return true;
    }
    But are you planning to do this check manually or are you planning to run it automatically when a change is made?

    Edit: On second thoughts you'd probably want to add a few more checks to ensure that the lenghts of the two arrays are the same and do an early return if they aren't - the same for if either of them is null. If neither of those can happen then ignore this.
    Last edited by sciguyryan; Jun 18th, 2010 at 06:12 AM.
    My Blog.

    Ryan Jones.

  6. #6

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,346

    Re: Check if an array has been modified?

    The check method is to be preformed just before I close the program. If there are changes it saves the changes to a file.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  7. #7
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Check if an array has been modified?

    Ah. Then the above method should be fine.

    Code:
    public bool hasArrayChanged(string[] a1, string[] a2)
    {
        if (a1.Length != a2.Length)
        {
            return true;
        }
    
        for (int i = 0; i < a1.Length; i++)
        {
            if (!Object.Equals(a1[i], a2[i]))
            {
                return true;
            }
        }
        return false;
    }
    Something like that should work fine in this case.
    My Blog.

    Ryan Jones.

  8. #8

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,346

    Re: Check if an array has been modified?

    The code I ended up using can be found here.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width