Forum BigDB Get Array from BigDB

Discussion and help relating to the PlayerIO database solution, BigDB.

Get Array from BigDB

Postby jak322 » November 26th, 2010, 7:27 pm

Hi, sorry for all the recent questions but I'm a total newbie to c#. Can someone tell me (if you can with a bit of example code) how to get an Array in c# from BigDB (as data is being stored as an array) then add another element or two to the Array and modify the existing Array in BigDB with the new one?

Thanks so much you guys here have already been so helpful!

James
jak322
 
Posts: 13
Joined: July 2nd, 2010, 3:24 pm

Re: Get Array from BigDB

Postby jak322 » December 1st, 2010, 3:10 pm

Can anyone help with this?
jak322
 
Posts: 13
Joined: July 2nd, 2010, 3:24 pm

Re: Get Array from BigDB

Postby Henrik » December 1st, 2010, 4:27 pm

The easiest thing should be to work directly with the DatabaseArray object directly, because it has methods for adding elements at the end of it, unlike regular C# arrays. Regular C# arrays are fixed size, so you can't easily add elements to them, instead you usually use ArrayList or the generic List<> when you have arrays that change in size.

Code: Select all
PlayerIO.BigDB.LoadOrCreate("MyTable", "MyKey", delegate (DatabaseObject result){
      DatabaseArray arr = result.GetArray("MyArray"); //Get the array in the object
      arr.Add("foo"); //Add "foo"
      arr.Add("bar"); //Add "bar"
      result.Save(); //Save back
   }
);


If you absolutely want to copy that array out into a List<string>, you would do it like this:

Code: Select all
PlayerIO.BigDB.LoadOrCreate("MyTable", "MyKey", delegate (DatabaseObject result){
      DatabaseArray arr = result.GetArray("MyArray"); //Get the array in the object
      List<string> strings = new List<string>(); //Make new array
      for(int i = 0; i++; i < arr.Count) { //Loop over all existing items
         strings.Add(arr.GetString(i)); //Copy to new array
      }

      //...

      strings.Add("foo"); //Add "foo"
      strings.Add("bar"); //Add "bar"

      //...

      DatabaseArray newArr = new DatabaseArray(); //Make new DB-array
      foreach(string str in strings) {
         newArr.Add(str); //Copy back
      }

      result.Set("MyArray", newArr);
      result.Save(); //Save back
   }
);
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Get Array from BigDB

Postby jak322 » December 1st, 2010, 6:27 pm

Just what I wanted thanks very much
jak322
 
Posts: 13
Joined: July 2nd, 2010, 3:24 pm


Return to BigDB



cron