Store Strings In An Array C#


Recommended Posts

is there a way to store strings in an array in c# (so I would have an array of strings)

this way I can take a line of text and store it in a position of an array and then push that line in order to an xmlfile.

if this is not clear then please let me know.

Link to post
Share on other sites

got it, I tried with no success arrayList but I just needed

static string[] stringarray = new string[200];

I wanted a dynamic array size so if anyone knows that one.. please let me know

Link to post
Share on other sites

so, this is what I think I have learned..

I need to create an object with my strings for each part.

then store these sets of strings as a string object in the ArrayList

is this correct

Link to post
Share on other sites

What are you trying to do? If you just want a collection of strings, you can dump the strings in a list and be done with it. [Edit: That sounded impolite. What I meant was that if you need a collection of strings there's no need to complexify the program by introducing extra objects.]

$ cat s.cs
using System;
using System.Collections;

public class Foo {
public static void Main(string[] args) {
IList l = new ArrayList();
l.Add("foo");
l.Add("bar");
Console.WriteLine("{0}{1}", l[0], l[1]);
}
}
$ mcs s.cs
$ ./s.exe
foobar

If you're using .NET 2.0 you can save yourself a lot of needless casts by using the aforementioned generic List.

using System.Collections.Generic;
...
List<string> l = new List<string>();
l.Add("foo")
string s = l[0];
...

Edited by jcl
Link to post
Share on other sites
What are you trying to do?
hehe, I ask my self the same thing all the time..

but in this case I am tring to learn, its my last semester of collage and work is starting to let me work on code (so I can learn because collage has tought me almost nothing real world.)

so as a proof of concept I am build a program that creates a xml framework for a old type adventure game (the only thing I could think of)

This creates a xml file based on input (early stages).

here is what I have so far, I am tring to make it better, and test out new things. I am reading in a book abought about ArrayList, But I don't really understand what they wrote.

here is the code

/*
* Created by SharpDevelop.
* User: huskeyw
* Date: 7/19/2006
* Time: 10:05 AM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
//using System.Xml.XPath;

namespace AdventureBuild
{
class adventureBuild
{

//varables
static string[] roomDesc = new string[200];// = new ArrayList();//holds descriptions of rooms in logical order
ArrayList roomData = new ArrayList();


//method to build xml file from questions asked user
public static void buildFile(int room, string filename)
{
System.Console.WriteLine("start loop");
XmlTextWriter gameFile = new XmlTextWriter(filename,null);
gameFile.Formatting = Formatting.Indented;
gameFile.Indentation = 3;
gameFile.WriteStartDocument();
gameFile.WriteComment("Test XML write");

System.Console.WriteLine("start loop1");
for (int x = 1; x <= room;x++)
{

System.Console.WriteLine("start loop2");
string decription = "";
System.Console.WriteLine("enter Description of room");
decription = System.Console.ReadLine();
roomDesc[x] = decription;
System.Console.WriteLine("start loop"+2+x);

}//end forloop1

gameFile.WriteStartElement("adventuregame");
for (int x =1; x <= room;x++)
{
System.Console.WriteLine("start loop4");
gameFile.WriteStartElement("room"+x);
gameFile.WriteElementString("Description", Convert.ToString(roomDesc[x]));
gameFile.WriteElementString("hiding", "Gold Key");
gameFile.WriteElementString("direction", "N S");
gameFile.WriteEndElement();
}//end forloop2

gameFile.WriteEndElement();
gameFile.Flush();
gameFile.Close();
}//end build fild method





public static int Main(string[] args)
{

//create list to store information

//store name of game file
string filename = "";

//store number of rooms to be built
int rooms = 0;

//example is for output to user
string example = "\"c:\\myfile.xml\"";

if ((args.Length == 0) || (args.Length > 2))
{
System.Console.WriteLine("adventureBuild must be ran with the following arguments.");
System.Console.WriteLine("Usage: adventureBuilder.exe <filename> <number of rooms> ");
System.Console.WriteLine("NOTE: file name needs to be in quotes if it \nincludes the path and end file with .xml");
System.Console.WriteLine("Example: adventureBuild.exe " + example + " 3");


return 1;
}//end if on args length check

try
{
filename = args[0];
rooms = Convert.ToInt16(args[1]);
}//end try




catch (System.FormatException)
{
System.Console.WriteLine("the argument format is incorrect.");
System.Console.WriteLine("Command adventureBuild must be ran with the following arguments.");
System.Console.WriteLine("Usage: adventureBuilder.exe <filename> <number of rooms> ");
System.Console.WriteLine("NOTE: file name needs to be in quotes if it includes the path and end file with .xml");

return 1;
}//end catch


try
{
buildFile(rooms,filename);
}
catch (System.InvalidOperationException)
{
System.Console.WriteLine("error at method");
}

System.Console.WriteLine("name of file to be written = " + filename);
System.Console.WriteLine("number of rooms = " + rooms);

return 0;
}//end main


}//end class
}//end Namespace

its a mess, but the list stuff is cool, I did not know about that..

I also have a class I am creating to hold the Room infromation, so I can put them in the ArrayList (just to learn), but the

List<string> l = new List<string>();

looks better

edit..

there are some writelines I added just to trouble shoot problems I was having, I don't know how to use a debuger so I have the program send me messages.

thanks..

Edited by iccaros
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...