[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

must declare a body because it is not marked abstract or extern

Posted on 2008-11-10
5
Medium Priority
?
3,671 Views
Last Modified: 2012-05-05
Hi I am a newbie and am getting this error when trying to learn xml serialization

Error 1      'XMLSerialize.Movie.Title.get' must declare a body because it is not marked abstract or extern C:\XMLSerializer\XMLSerializer\Program.c

I have attached the code snippet, could anyone please tell me why and what to do to fix?

Thank You
using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
 
namespace XMLSerialize
{
    public class MainClass
    {
 
        static void Main(string[] args)
        {
            Movie movie = new Movie();
            movie.Title = "Starship Troopers";
            movie.ReleaseDate = DateTime.Parse("11/7/1997");
            movie.Rating = 6.9f;
 
            SerializeToXML(movie);
        }
 
        static public void SerializeToXML(Movie movie)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Movie));
            TextWriter textWriter = new StreamWriter(@"C:\movie.xml");
            serializer.Serialize(textWriter, movie);
            textWriter.Close();
        }
    }
 
 
    public class Movie
    {
        public string Title
        {
            get; set;
        }
 
        public int Rating
        { get; set; }
 
        public DateTime ReleaseDate
        { get; set; }
    }
}

Open in new window

0
Comment
Question by:Charles Baldo
5 Comments
 
LVL 23

Accepted Solution

by:
Snarf0001 earned 1600 total points
ID: 22923257
Looks like you're trying to use the auto-implemented properties, which were not released until c# 3.0.

If you're using 2.0, you still need to write out the full functionality of getting and setting the properties:
    public class Movie
    {
        private string _title;
        private int _rating;
        private DateTime _releaseDate;
 
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }
 
        public int Rating
        {
            get { return _rating; }
            set { _rating = value; }
        }
 
        public DateTime ReleaseDate
        {
            get { return _releaseDate; }
            set { _releaseDate = value; }
        }
    }

Open in new window

0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22923302
one more thing for .Net 2.0
initialize all your strings else they will not be serialized on serialization if the members are null
0
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 400 total points
ID: 22923357
Hi charlesbaldo;

I found one error in the code you posted. You have this line in the Main subtoutine:

movie.Rating = 6.9f;

But the Move class you have it like this:

public int Rating  { get; set; }

I changed it like this:

public float Rating { get; set; }

And rand the code and it producted the XML file below.

Fernando

<?xml version="1.0" encoding="utf-8"?>
<Movie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Title>Starship Troopers</Title>
  <Rating>6.9</Rating>
  <ReleaseDate>1997-11-07T00:00:00</ReleaseDate>
</Movie>

Open in new window

0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 22923418
Well that's what I get for using VS .Net 3.5. Snarf0001 is correct about auto-implemented properties. ;==)
0
 

Author Closing Comment

by:Charles Baldo
ID: 31515097
thanks a ton Snarf0001, Thanks fernando as well as catching data type mismatch
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question