help with arrays  
Author Message
kenniejaydavis





PostPosted: Visual C# Language, help with arrays Top

I have an application where I have two arrays such as:

string[] array1 = { "one", "two", "three", "four" };
string[] array2 = { "two", "four", "six" };

I am hoping that C# offers an easy way to compare the two arrays and return an array of those elements that are different (in this case, I would have a three-element array containing the strings "one", "three" and "six"). In PHP, it's as simple as (I may have the function name wrong, but you get the idea)

differentArray = array_diff(array1, array2);

I don't want to code all of the logic myself and it seems like a basic and useful enough function to be part of the .NET framework.



Visual C#7  
 
 
DMan1





PostPosted: Visual C# Language, help with arrays Top

Create a class for your circle objects and then use a list (of MyCircleClass) to keep track of the collection of circles...(versus an array)

Dim MyListOfCircles as New List (of MyCircleClass)

Add to your list

MyListOfCircles.Add(Circle1)

to iterate through the collection you can

For each mcc as MyCircleClass in MyListOfCircles

console.writeline(mcc.propertyname)

....



 
 
Zhi-Xin Ye - MSFT





PostPosted: Visual C# Language, help with arrays Top

See IList

 
 
Brian Kramer





PostPosted: Visual C# Language, help with arrays Top

First, note that (1,2,3) is an expression that evaluates to 3.  You've effectively created an array with two elements: 3 and 6.  The de**** was telling you the truth also.

So use braces, not parenthesis.

Here's the corrected version of the code.  Note that MSDN documents multidimensional arrays.

array<int,2>^ arrayInt = { {1, 2, 3}, {4, 5, 6} };
arrayInt[0,0] = 2;

I'll research your other question in a bit. Apparently the array implementation of IEnumerator does a "depth-first iteration," giving you individual elements, instead of rows.  I ran into this in C# yesterday and I ended just coding around the problem instead of seeing if there was a proper way to solve it.

 


 
 
Oligarchy





PostPosted: Visual C# Language, help with arrays Top

I don't think there are any built-in functions however, there are several articles and sample code out there that may help you.

Here's one that may help you.

http://www.mathertel.de/Diff/ViewSrc.aspx

 
 
JDee





PostPosted: Visual C# Language, help with arrays Top

Here is an example in C#. Please see if this helps.

List<SqlConnection> sConList = new List<SqlConnection>();

sConList.Add(new SqlConnection("yadayadayada"));

sConList.Add(new SqlConnection("blahblahblah"));

sConList.Add(new SqlConnection("SomethingElse"));


 
 
kenniejaydavis





PostPosted: Visual C# Language, help with arrays Top

Thanks for your reply. I've already seen this and was hoping I wouldn't have to resort to implementing something outside of the .NET Framework. What I'm doing is so simple I'll probably just end up coding it myself unless anyone else may know of a class/method that does what I need hidden somewhere in the framework
 
 
MS Johan Stenberg





PostPosted: Visual C# Language, help with arrays Top

You can also check out http://msdn2.microsoft.com/en-us/library/xk24xdbe(VS.80).aspx for some more info on what an array is and how you can use it...

Best regards,
Johan Stenberg



 
 
Holger Grund





PostPosted: Visual C# Language, help with arrays Top

In addition to what Brian said, .NET arrays are not dynamically sized in that once an array object is created it keeps its size. However, it is perfectly valid to use a dynamic expression for the initial size as argument to the constructor. E.g.:

array<int>^ x = new array<int>( 4711 );

Also there are various other classes that may just do the trick if you need dynamically sized containers (just check out System.Collections.(Generic)).

BTW: There's for each which does some of the IEnumerator traversal work for you.

-hg


 
 
Brian Kramer





PostPosted: Visual C# Language, help with arrays Top

MSDN's docs for C#'s foreach hints that you must use a nested for loop. Here's an example in C++/CLI:

int main(array<System::String ^> ^args)
{
array<int,2>^ arrayInt = { {1, 2, 3}, {4, 5, 6} };
for( int row = 0; row < arrayInt->GetLength(0); row++ )
{
//
// At this point, is it possible to retrieve a row, of type array<int,1>
// from arrayInt without copying I don't think so... so we just go ahead and
// put in a nested loop.

//
for (int col = 0; col < arrayInt->GetLength(1); col++)
{
Console::WriteLine(
"cell={0}", arrayInt[row,col]);
}
}
return 0;
}

You could use jagged arrays (arrays of arrays), e.g. array<array<int>^>^ arrayInt;

int main(array<System::String ^> ^args)
{
array<array<int>^>^ arrayInt = { {1, 2, 3}, {4, 5, 6} };
for each( array<int>^ row in arrayInt )
{
for each( int cell in row )
{
Console::WriteLine(
"cell={0}", cell );
}
}
return 0;
}


 
 
RizwanSharp





PostPosted: Visual C# Language, help with arrays Top

There is no built in function in .Net to compare 2 arrays and return the difference to its caller so you have to write your own...

Best Regards,

Rizwan



 
 
spotty





PostPosted: Visual C# Language, help with arrays Top

OK, her are a few thoughts on this as it sounds a bit like an assignment.

You'll want to have some form of random number generator. Each color will have a number associated with it. So if your going to be using say 6 different colors then you can create function which will generate a random number between 1 and 6

The next part is how many numbers you want to have in the sequence. This is where you can use an array or another collection like an arraylist or list which would allow you to store as many numbers in a sequence as you want. So potentially each time you get it correct it would generate a new sequence of random numbers - slightly longer (ie one more to remember).

I would look at the arraylist myself.

You will then have to loop however many items you want to put in this collection (array/arraylist) and generate a random number and place it in the collection elements. This would generate you sequence.

Then you would iterate through the collection using a looping construct (say a for each or for next loop) and for each number that you find in that element would display something on the screen. (I would start by showing the number and waiting for a little while before showing the next number - then I would translate these numbers into a color circle)

Then you allow the user to click controls and each control would add compare to an item in the collection. If they matched then they could enter the next, if they didnt then it would be a wrong sequence.

To start off I would work on the logic of the application rather than the appearance. I would have it generate numbers instead of colors and allow them to click on buttons corresponding to those numbers. Once you get the logic working then you can map the numbers to colors and the buttons to Picturebox controls. The reason I would do it using numbers is that the randomness of the sequence is as a result of numbers not colors. You use a randon number generator.

That should be some thoughts/ideas to get you going. If you have specific issues with commands/keywords etc. then do ask but please dont expect us to do the application for you - we dont do assignments. :-)


 
 
kenniejaydavis





PostPosted: Visual C# Language, help with arrays Top

That's disappointing news indeed.
 
 
Mousy





PostPosted: Visual C# Language, help with arrays Top

if you don't mind the additional coding in your source code, you can try the one below:

It was written and tested by me, if you want to give it a try u can

public string[] getDiff(string[] ary1, string[] ary2)

{

string[] arySame=new string[ary1.Length+ary2.Length];

string[] aryDiff=new string[ary1.Length+ary2.Length];

int arrayrecord = 1;

for (int count1 = 0; count1 < ary1.Length; count1++)

{

for (int count2 = 0; count2 < ary2.Length; count2++)

{

if (ary1[ count1 ] == ary2[ count2 ])

{

bool found = false;

for (int arraycount = 0; arraycount < arrayrecord; arraycount++)

{

if (arySame[ arraycount ] == ary2[ count2 ])

found = true;

}

if (found == false)

{

arySame[ arrayrecord-1 ] = ary2[ count2 ];

arrayrecord += 1;

}

}

}

}

string[] arySam = new string[ arrayrecord-1 ];

for (int a = 0; a < arySam.Length; a++)

arySam[ a ] = arySame[ a ];

arrayrecord=1;

for (int count1 = 0; count1 < ary1.Length; count1++)

{

bool found=false;

for (int a = 0; a < arySam.Length; a++)

{

if (ary1[ count1 ] == arySam[ a ])

found = true;

}

if (found == false)

{

aryDiff[ arrayrecord-1 ] = ary1[ count1 ];

arrayrecord += 1;

}

}

for (int count2 = 0; count2 < ary2.Length; count2++)

{

bool found = false;

for (int a = 0; a < arySam.Length; a++)

{

if (ary2[ count2 ] == arySam[ a ])

found = true;

}

if (found == false)

{

aryDiff[ arrayrecord-1 ] = ary2[ count2 ];

arrayrecord += 1;

}

}

string[] aryDif = new string[arrayrecord-1];

for (int a = 0; a < aryDif.Length; a++)

aryDif[ a ] = aryDiff[ a ];

return aryDif;

}


 
 
Troy Magennis





PostPosted: Visual C# Language, help with arrays Top

Not that it helps you right now, but this type of functionality is coming.

Next year some-time using LINQ to Objects you get the set operators: Intersect, Union, Except and Distinct and can do all of this stuff with any collection that inherits from IEnumerable (which the basic arrays do).

Troy.


 
 
Matthew Watson





PostPosted: Visual C# Language, help with arrays Top

There is a very useful collections library called "PowerCollections" available (requires .Net 2). This was written by one of the people who worked on the .Net libraries, and it is available for free here:

http://www.wintellect.com/PowerCollections.aspx

Some code to do what you wanted might look like this:



using Wintellect.PowerCollections;

...

string[] array1 = { "one", "two", "three", "four" };
string[] array2 = { "two", "four", "six" };

// Get difference as a generic enumerable collection:

IEnumerable<string> difference = Algorithms.SetSymmetricDifference(array1, array2);

// Convert to array of strings if you want:

string[] result = new List<string>(difference).ToArray();



I cannot overemphasise how useful the PowerCollections library is! It contains many collections and algorithms.


 
 
kenniejaydavis





PostPosted: Visual C# Language, help with arrays Top

This is exactly what I was looking for and I don't understand why it's not part of the .NET Framework by default

I have one other question and I hope this forum is right place to ask. The above code works perfectly with two arrays. I also may have need to compare two custom collections which I can't seem to get to work using the code above. Here is what I have written:

TerritoryCollection col1 = new TerritoryCollection();
TerritoryCollection col2 = new TerritoryCollection();

string[] array1 = { "one", "two", "three", "four" };
string[] array2 = { "two", "four", "six" };

foreach (string zipCode in array1)
{
col1.Add(
new Territory(zipCode, Guid.Empty));
}

foreach (string zipCode in array2)
{
col2.Add(
new Territory(zipCode, Guid.Empty));
}

IEnumerable<Territory> difference = Algorithms.SetSymmetricDifference(col1, col2);
Territory[] result = new List<Territory>(difference).ToArray();

foreach (Territory zipCode in result)
{
Console.WriteLine(zipCode + "\n");
}

TerritoryCollection is just a simple collection that inherits from CollectionBase and Territory is just a simple class and as you can see by the constructor stores a string and Guid. Since CollectionBase inherits from IEnumerable, the PowerCollections algorithms should theoretically work. However, I am recieving the following error:

Error 1 The type arguments for method 'Wintellect.PowerCollections.Algorithms.SetSymmetricDifference<T>(System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IEnumerable<T>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. C:\Documents and Settings\Scheduling\My Documents\Visual Studio 2005\Projects\SynergyProjects\TestingConsole\Program.cs 51 53 TestingConsole

I'm sure I'm just doing something wrong because I don't fully understand how to use the PowerCollections. Again I apologize if this is the wrong forum for this question.


 
 
Matthew Watson





PostPosted: Visual C# Language, help with arrays Top

There are two kinds of IEnumerable: One is from .Net 1.x (which is the one you are trying to use). That kind does NOT support generics.

The other kind is IEnumerable<T>, which is from .Net 2.x, and that DOES support generics.

I think you need to derive your collection class from System.Collections.Generic.ICollection<T> to make it all work.


 
 
kenniejaydavis





PostPosted: Visual C# Language, help with arrays Top

So even though I'm using .NET 2.x, System.Collections.CollectionBase inherits from IEnumerable .Net 1.x
 
 
Matthew Watson





PostPosted: Visual C# Language, help with arrays Top

It was in .Net 1.x, so they couldn't change it without breaking everyone's code. Instead, they introduced a new class to take advantage of generics.