I am just doing a test/comparsion between the different ways of getting a similar code syntax to work in different languages for a similar project idea. The main project idea is wordsearch grid with words inserted onto the grid, I have already done a basic php version and here is the word class in php word. Below there is a full class version of whole class, but also some parts that I have expanded to try and explain in more detail.
This code will return a String array from a ArrayList type. The ToArray changes the ArrayList to type of string then the “as string[]” returns the list of string as a string array.
return (arrayL.ToArray(typeof(string)) as string[]);
Here is the word class in c#, I have added allot of code comments to give details of what is happening.
using System;
using System.Xml;
using System.Collections;
namespace wordsearchcsharp
{
class Word
{
private String[] _words;
public Word() : this("words.xml", 5) {}
public Word(string wordsOrFilename, int maxSearchNum)
{
this._words = new String[maxSearchNum];
// just load from a file the words to search, instead of input as well.
String[] loadedWords = loadWords(wordsOrFilename);
// create the searchable words from the loadedwords array.
this._words = this.searchableWords(loadedWords, maxSearchNum);
}
private String[] loadWords(string filename)
{
XmlTextReader fileReader = new XmlTextReader(filename);
ArrayList fileArray = new ArrayList();
while (fileReader.Read())
{
// get the NodeType from the XML reader, different types
// Element = name of element
// Text = the actual text/word
// End Element = end of the element name
switch (fileReader.NodeType)
{
// if there is a word in the file e.g. not a end/element
case XmlNodeType.Text:
fileArray.Add(fileReader.Value);
break;
}
}
// change the ArrayList into a String[] array using the ToArray method
return (fileArray.ToArray(typeof(string)) as string[]);
}
// create a list of words from the words passed in and also
// the maximum number is the maxSearchNum.
private String[] searchableWords(String[] words, int maxSearchNum)
{
ArrayList returnWords = new ArrayList();
// if the maxsearch value is greater or equal to the number of words to search for
// just return the words string[].
if (words.Length <= maxSearchNum)
{
return words;
}
else
{
// create a good seed for the random generator.
Random randGen = new Random((int)DateTime.Now.Ticks);
int randomNum;
// randomly pick out words from the array
for (int i = 0; i < maxSearchNum; i++)
{
// pick a random number
randomNum = randGen.Next(0, words.Length);
// add to the array list to return
returnWords.Add(words[randomNum]);
// rebuild the array with removing the random number generated.
words = rebuildArray(words, randomNum);
}
}
// convert back to the String[]
return (returnWords.ToArray(typeof(string)) as string[]);
}
private String[] rebuildArray(String[] rebuildSt, int numberToTakeOut)
{
ArrayList arrayL = new ArrayList();
// out of range error.
if (rebuildSt.Length < numberToTakeOut)
{
return rebuildSt;
}
else
{
for (int i =0; i < rebuildSt.Length; i++)
{
// only add in the words that are not the
// numberToTakeOut word from the array
if (i != numberToTakeOut)
{
arrayL.Add(rebuildSt[i]);
}
}
}
return (arrayL.ToArray(typeof(string)) as string[]);
}
public void printOutWords()
{
Console.Write("Words : (");
for (int i = 0; i < this._words.Length; i++)
{
Console.Write(" " + i + " = " + this._words[i]);
}
Console.Write(")\n");
}
public String[] returnWords()
{
return this._words;
}
}
class MainClass
{
public static void Main(string[] args)
{
Word word = new Word("words.xml",3);
word.printOutWords();
}
}
}
and a output would be
Words : ( 0 = he 1 = old 2 = sole)