Jump to content


Photo

Store Strings In An Array C#


  • Please log in to reply
5 replies to this topic

#1 iccaros

iccaros

    UberTechie

  • Linux Experts
  • 1292 posts
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 08 September 2006 - 07:23 PM

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.

#2 iccaros

iccaros

    UberTechie

  • Linux Experts
  • 1292 posts
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 08 September 2006 - 08:06 PM

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

#3 jcl

jcl

    UberTechie

  • Linux Experts
  • 1304 posts
  • Location:The Internet
  • Operating System:Arch

Posted 08 September 2006 - 08:35 PM

ArrayList and List<string> (from System.Collections.Generic) will work. StringCollection (from System.Collections.Specialized) might work but I haven't used it.

#4 iccaros

iccaros

    UberTechie

  • Linux Experts
  • 1292 posts
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 08 September 2006 - 11:00 PM

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

#5 jcl

jcl

    UberTechie

  • Linux Experts
  • 1304 posts
  • Location:The Internet
  • Operating System:Arch

Posted 08 September 2006 - 11:19 PM

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, 08 September 2006 - 11:48 PM.


#6 iccaros

iccaros

    UberTechie

  • Linux Experts
  • 1292 posts
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 09 September 2006 - 12:42 AM

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, 09 September 2006 - 12:46 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users