Menu

Generic Type error

Help
fpdeguzman
2007-11-14
2013-04-09
  • fpdeguzman

    fpdeguzman - 2007-11-14

    hi, its me again.
    i would like to ask why there is an error when i use an "int" type in Fmt.Serialize? The detail error is """The type 'int' must be a reference type in order to use it as parameter 'T' in the generic type or method 'Kds.Serialization.Formatter<F>.Serialize<T>(T)""".
    the same error also occurs when using Fmt.Deserialize.
    my code goes like this.

      public class Sample
      {
        internal int id;
        internal string code;

        public int Id {
          get { return id; }
          set { id = value; }
        }

        public string Code {
          get { return code; }
          set { code = value; }
        }

       }

      public class SampleField: ReferenceField<Sample>
      {
        public SampleField(Formatter fmt, bool isDefault) : base(fmt, isDefault) { }

        protected override void SerializeValue(Sample value) {
          Fmt.Serialize<int>(value.Id);         <----- Error -----
          Fmt.Serialize<string>(value.Code);
        }

        protected override void DeserializeInstance(ref Sample instance) {
          if (instance == null)
            instance = new Sample();
        }

        protected override void DeserializeMembers(Sample instance) {
          Fmt.Deserialize<string>(ref instance.id); <----- Error -----
          Fmt.Deserialize<string>(ref instance.code);
        }

        protected override void SkipValue() {
          if (Fmt.Skip<int>())
          Fmt.Skip<string>();
        }
      }

    thank you very much!

     
    • Karl Waclawek

      Karl Waclawek - 2007-11-14

      If you look at the Serialize overloads in the source code, you will see that there are two ways to serialize value types:

      a) Formatter.Serialize<T>(T value) where T is a *nullable" value type (e.g. int? in C#)
      b) Formatter.Serialize<T>(ref T value) where T is a regular value type.

      The serializer is meant to deal with nulls even for structs.
      You can just assign your int to a temporary nullable int, or change your int declaration to int?
      Have a look at the getting started demo.

      Karl

       
    • fpdeguzman

      fpdeguzman - 2007-11-15

      thank you very much. have a nice day! =)

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.