3 Replies - 1052 Views - Last Post: 06 April 2009 - 07:04 PM Rate Topic: -----

#1 FireStorm000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-April 09

load data from multiple files and hold onto it for later use

Posted 05 April 2009 - 10:04 PM

I'm currently working on learning C sharp and decided that I would try to do a clone of a 2D RTS game I liked in XNA. I've been doing pretty well so far, but I've run into a problem I can't seem to get around. Also, let my just apologize in advance if I mix up any terminology.

Here's a simplified example of what I need to do:
public struct myStruct
{
	 public string myString;
	 public int myInt1;
	 public int myInt2;
	 public double myDouble;
	 //etc
}
...

void myFunction()
{
	//Load data from file
	loadData();
	//create a new instance of myStruct to store the newly loaded data in
	//here's the problem
	createNewInstanceOfStruct();
	//code continues...
}



So, the problem is that I could need to load a lot of files, and I may need to keep every one of them for the life of the program, as it's a game and it needs to manipulate the data in the myStruct.

My original plan for createNewInstanceOfStruct() was to have a counter for every time I loaded a file, appent that to the end of a string, and use that as the name of the instance of mystruct, so I could later asses any data in the struct simply by knowing the number I assigned that struct. IE:
//global variables
int counter = 0;
string structName = "dataStruct";

void createNewInstanceOfStruct()
{
	 string newStructName;	  //works
	 newStructName = structName + counter.ToString();   //works
	 counter++;	   //works
	 myStruct newStructName = new myStruct();	 //error; as far as I can tell, you can't use a the value 
													 //stored in a string as a variable name
	 //...
} 



About an hour of goggling for info more or less confirmed what the error said. You can't use a string as a variable name. This functionality is absolutely vital to the program, so I either need to find a way to make the above function work, or find a new way to store and access the data. I had considered using a multidimensional array or a list of multidimensional arrays to store the data, but that would just be a mess, as I have well over 100 values to store in that struct, and to further complicate it, there are some strings I need to store as well. Any help would be greatly appreciated.

*EDIT: fixed title to make it clearer

This post has been edited by FireStorm000: 05 April 2009 - 10:32 PM


Is This A Good Question/Topic? 0
  • +

Replies To: load data from multiple files and hold onto it for later use

#2 SigurdSuhm  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 111
  • Joined: 05-August 08

Re: load data from multiple files and hold onto it for later use

Posted 06 April 2009 - 03:43 AM

The problem with that code specificly seems to be - as you said - that you can't use the string name as the struct name. You could solve it like this:

myStruct myNewStruct = new myStruct();
myNewStruct.myString = newStructName;



But depending on how the data is contained and organized in the files you could maybe consider to instantiate your struct for each file you load. So one instance of the struct holds the data from one file. You could then keep all the instances in an array and have pretty easy access to the data of each file.

Regards
Sigurd
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: load data from multiple files and hold onto it for later use

Posted 06 April 2009 - 04:22 AM

// create a variable of type string named newStructName
string newStructName;

// assign a value to existing variable
newStructName = structName + counter.ToString();

// increment a global counter
counter++;

// create a variable of type myStruct named newStructName
// of course you can't do this, you already used that name for a variable of type string
//myStruct newStructName = new myStruct();



So, um, just use a different name?
string newStructNameValue = structName + counter;
counter++;
myStruct newStructName = new myStruct();



Really, what's in a name? More importantly, why the hell use a struct? ;)
Was This Post Helpful? 0
  • +
  • -

#4 FireStorm000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-April 09

Re: load data from multiple files and hold onto it for later use

Posted 06 April 2009 - 07:04 PM

Thanks both of you for your comments; you got me thinking:
//instead of 
myStruct
{
 int int1;
 string string1;
}

//and then trying to load a new instance of the struct for each file
void loadnewfile()
{
string newStructName;	  
newStructName = structName + counter.ToString();   
counter++;	   
myStruct newStructName = new myStruct()  //FAIL
}
//---------------------------------------------------------------------------------------------

//do this because it works:
myStruct
{			
List<int> int1 = new List<int>();
List<string> string1 = new List<string>();
}
//then I only need one struct
//and can then do this:
myStruct dataStruct = new myStruct;

//and store data like
void loadNewFile()
{
dataStruct.int1.Add(valuefromfile);
fileID++//global counter variable
//...
}



then, just knowing the fileID, and the fixed struct name lets me access the values.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1