[C#] Simple code error storing data to a List

I am pretty new to C# so many things I have never done before, this is one.

I have the following code were I am testing to save data to playerPrefs using BinaryFormatter. However, the problem i have is how to store the “CardData” to the cardSavedData -list.

I do get the following message:

assets/DataSaveTest.cs(49,36): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected

on this line below: “cardSavedData.Add (CardData);”

I am aware that this is a simple problem but can not really figure out what to search on to find the solution. I have tested to put “new” property.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

//For Binary Formatter
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public class DataSaveTest : MonoBehaviour {

    //The card deck
   public class CardData {

       public static bool cardHaveFaceUp;
       public static Vector3 cardsPosition;
       public static float cardsSortingOrder;

 }

   //The Template List
   public List<CardData> cardSavedData = newList<CardData>();


   void SavePosition() {
      //Get binary formatter
      var b = newBinaryFormatter();

      //Create an inmemory stream
      var m = newMemoryStream ();

      //Save the position data
      b.Serialize (m, cardSavedData);

      //Add it to playerPrefs
      PlayerPrefs.SetString ("CardDeckSavedData", Convert.ToBase64String (m.GetBuffer ()));
    }




   voidStart () {

      CardData.cardHaveFaceUp = true;
      CardData.cardsPosition = newVector3 (10f, 10f, 10f);
      CardData.cardsSortingOrder = 10f;
      cardSavedData.Add (CardData);

      CardData.cardHaveFaceUp = true;
      CardData.cardsPosition = newVector3 (20f, 20f, 20f);
      CardData.cardsSortingOrder = 20f;
      cardSavedData.Add (CardData);

      CardData.cardHaveFaceUp = true;
      CardData.cardsPosition = newVector3 (30f, 30f, 30f);
      CardData.cardsSortingOrder = 30f;
      cardSavedData.Add (CardData);

      CardData.cardHaveFaceUp = true;
      CardData.cardsPosition = newVector3 (40f, 40f, 40f);
      CardData.cardsSortingOrder = 40f;
      cardSavedData.Add (CardData);

      CardData.cardHaveFaceUp = true;
      CardData.cardsPosition = newVector3 (50f, 50f, 50f);
      CardData.cardsSortingOrder = 50f;
      cardSavedData.Add (CardData);

      SavePosition ();

      cardSavedData.Clear ();



      //Get the data
      var data = PlayerPrefs.GetString("CardDeckSavedData");

      //If not blank then load it
      if (!string.IsNullOrEmpty (data)) {

      //Binary formatter for loading back
      var b = newBinaryFormatter();

      //Create memory stream with the data
      var m = newMemoryStream(Convert.FromBase64String(data));

      //Load back the position data
      cardSavedData = (List<CardData>)b.Deserialize(m);


      foreach (var xxx in cardSavedData) {
         print (">> " + xxx);
       }
    }


 }


}

I bet there are more error messages. The most important one is always the very first message. Everything else is uninteresting at first.

You’ve defined CardData as a class (or “type”). You’d want to add instances of that class to your list.

So you probably want to declare a local variable of type CardData, like “CardData newCard;” and create instances of CardData (that is “instantiate”) with the “new” keyword. Honestly, I think you just need to learn about what classes and instances are.

1 Like

The problem is you defined your CardData member variable as static. You do not want to do that because their value is shared across all instances of CardData. You actually want:

  • public class CardData {
    • public bool cardHaveFaceUp;
  • public Vector3 cardsPosition;
  • public float cardsSortingOrder;
    • }

Then when creating your deck you instantiate storage for each card:

      CardData onecard = new CardData();
     onecard.cardHaveFaceUp = true;
      onecard.cardsPosition = newVector3 (40f, 40f, 40f);
      onecard cardsSortingOrder = 40f;
      cardSavedData.Add (onecard );

A more shorthand way to do it:

   public class CardData
      {
      public bool cardHaveFaceUp { get;  set; }
      public Vector3 cardsPosition { get; private set; }
      public float cardsSortingOrder { get; private set; }

      public CardData(bool faceup, Vector3 position, float sortingorder)
         {
         cardHaveFaceUp = faceup;
         cardsPosition = position;
         cardsSortingOrder = sortingorder;
         }
      }

Then to create your deck:

cardSavedData.Add (new CardData(true, new Vector3 (10f, 10f, 10f), 10f));
   cardSavedData.Add (new CardData(true, new Vector3 (20f, 20f, 20f), 20f));