2

I have a c# class that looks like this:

   public class MemberData
{
    public int meme_ck;
    public string meme_name;
    public bool meme_active;

    public MemberData(int ck2, string name2, bool active2)
    {
        meme_ck = ck2;
        meme_name = name2;
        meme_active = active2;
    }
}

I have made two arrays out of that class:

    private MemberData[] memarray1 = new MemberData[10000];
    private MemberData[] memarray2 = new Memberdata[10000];

Over the course of my application I do a bunch of stuff with these two arrays and values change, etc. Member's name or active status may change which results in the ararys becoming different.

Eventually I need to compare them in order to do things to the other one based on what results are kicked out in the first one.

For example, member is de-activated in the first array based on something application does, I need to update array 2 to de-activate that same member.

I am trying to use some database design philosphy with the int CK (contrived-key) to be able to rapidly look up the entry in the other array based on the CK.

Since I can't figure it out I've had to resort to using nested for loops like this, which sucks:

        foreach (Memberdata md in memarray1)
    {
        foreach (Memberdatamd2 in memarray2)
        {
            if (md.ck = md2.ck)
            {
                //de-activate member
            }
        }
    }

Is there a better way to do this? I just want to find the index in the second array based on CK when I have the CK value from the first array.

Any other tips or advice you have about structure would be appreciated as well. Should I be using something other than arrays? How would I accomplish this same thing with Lists?

Thanks!

1

2 Answers 2

7

Should I be using something other than arrays?

Yes. Don't use arrays; they are seldom the right data structure to use.

How would I accomplish this same thing with Lists?

Lists are only marginally better. They don't support an efficient lookup-by-key operation which is what you need.

It sounds like what you want is instead of two arrays, two Dictionary<int, MemberData> where the key is the ck.

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

2 Comments

Hey, thanks! Just out of curiosity is there a way to do the lookup within an array structure as well? I just want to learn. Thanks!
@johnwilliams: Array.IndexOf
1

I totally agree with Eric Lippert's answer above. It is better you do not use Array. Same thing can be achieved using List<MemberData>. You can use LINQ as well to query your DataStructure.

Following is one of the way just to achieve your result using array

class Program
{
    static MemberData[] memarray1 = new MemberData[10000];
    static MemberData[] memarray2 = new MemberData[10000];
    static void Main(string[] args)
    {
        for (int i = 0; i < memarray1.Length; i++)
        {
            memarray1[i] = new MemberData(i + 1, "MemName" + i + 1, true);
            memarray2[i] = new MemberData(i + 1, "MemName" + i + 1, true);
        }

        // SIMULATING YOUR APP OPERATION OF CHANGING A RANDOM ARRAY VALUE IN memarray1

        int tempIndex = new Random().Next(0, 9999);

        memarray1[tempIndex].meme_name = "ChangedName";
        memarray1[tempIndex].meme_active = false;

        //FOR YOUR UDERSTADNING TAKING meme_ck IN AN INTEGER VARIABLE

        int ck_in_mem1 = memarray1[tempIndex].meme_ck;

        //FINDING ITEM IN ARRAY2

        MemberData tempData = memarray2.Where(val => val.meme_ck == ck_in_mem1).FirstOrDefault();

        // THIS IS YOUR ITEM.

        Console.ReadLine();
    }
}

1 Comment

Thanks! I asked Eric above as well, but is there a way to do this within the array also? Just for learning purposes. Can I use Linq in Arrays using that same code? Also I know how to find an index within an array if it matches exactly (int index = Array.IndexOf(memarray1, memarray2[i]);). Is it possible to use that somehow to look for specific fields that match instead of the entire entry having to match? Thanks again

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.