locked
Reading into a class which is serializable RRS feed

  • Question

  •   [Serializable()]
       public class NavigationData
       {
           [XmlElement("Depth")]
          public double Depth { get; set; }
          [XmlElement("Altitude")]
          public double Altitude { get; set; }
    }

    When I tried to assign to these like so, the code execution fell through to the catch block. What am I doing wrong?

    How I used it

     for(int line= 0; line < num_lines ; ++line)
                        {
                            NavigationData nav = null;
                            nav.Altitude = nav_data[line].altitude;

    The right hand side nav_data[line].altitude has a valid value in it.

    Is my syntax to assign it wrong?

    thanks

    sb

    Wednesday, June 6, 2012 6:34 PM

Answers

  • nav has not been declared yet, it's still NULL when you are trying to assign "nav_data[line].altitude" to the property Altitude of the not-asigned NavigationData reference.

    Regards, MusicDemon

    • Marked as answer by s_b1 Wednesday, June 6, 2012 6:47 PM
    Wednesday, June 6, 2012 6:38 PM
  •  NavigationData nav = null;
     nav.Altitude = nav_data[line].altitude;

    In this case nav is always null. It should be initialized somewhere.

    On this way you can access Altitude property from NavigationData object :

     NavigationData nav = ...initialized or loaded...
     nav.Altitude = nav_data.Altitude;

    • Marked as answer by s_b1 Wednesday, June 6, 2012 6:47 PM
    Wednesday, June 6, 2012 6:42 PM
  • Example:

    using System;
    using System.Collections.Generic;
    using System.Xml.Serialization;
    
    namespace TestConsole
    {
        class Program
        {
            // Create list of navigation data
            static List<NavigationData> dataCollection = new List<NavigationData>();
    
            static void Main(string[] args)
            {
                LoadData();
    
                PrintData();
    
                Console.ReadKey();
            }
    
            static void LoadData()
            {
                // Populate list
                for (int i = 0; i < 10; i++)
                {
                    // Create new instance of the navigation data
                    NavigationData data = new NavigationData();
    
                    // Set navigation data properties
                    data.Altitude = i;
                    data.Depth = i;
    
                    // Add data to list
                    dataCollection.Add(data);
                }
            }
    
            static void PrintData()
            {
                for (int i = 0; i < dataCollection.Count; i++)
                {
                    // Get navigation data from list
                    NavigationData data = dataCollection[i];
    
                    // Print navigation data
                    Console.WriteLine(string.Format("Navigation data {0} - Altitude={1} - Depth={2}", i, data.Altitude, data.Depth)); 
                }
            }
        }
    
    
        [Serializable()]
        public class NavigationData
        {
            [XmlElement("Depth")]
            public double Depth { get; set; }
            [XmlElement("Altitude")]
            public double Altitude { get; set; }
        }
    }
    

    • Marked as answer by s_b1 Thursday, June 7, 2012 2:57 PM
    Wednesday, June 6, 2012 6:56 PM

All replies

  • nav has not been declared yet, it's still NULL when you are trying to assign "nav_data[line].altitude" to the property Altitude of the not-asigned NavigationData reference.

    Regards, MusicDemon

    • Marked as answer by s_b1 Wednesday, June 6, 2012 6:47 PM
    Wednesday, June 6, 2012 6:38 PM
  •  NavigationData nav = null;
     nav.Altitude = nav_data[line].altitude;

    In this case nav is always null. It should be initialized somewhere.

    On this way you can access Altitude property from NavigationData object :

     NavigationData nav = ...initialized or loaded...
     nav.Altitude = nav_data.Altitude;

    • Marked as answer by s_b1 Wednesday, June 6, 2012 6:47 PM
    Wednesday, June 6, 2012 6:42 PM
  • thank you both for helping!

    I didn't realize  I had to do a

    nav = new NavigationData();

    sb

    Wednesday, June 6, 2012 6:48 PM
  • Example:

    using System;
    using System.Collections.Generic;
    using System.Xml.Serialization;
    
    namespace TestConsole
    {
        class Program
        {
            // Create list of navigation data
            static List<NavigationData> dataCollection = new List<NavigationData>();
    
            static void Main(string[] args)
            {
                LoadData();
    
                PrintData();
    
                Console.ReadKey();
            }
    
            static void LoadData()
            {
                // Populate list
                for (int i = 0; i < 10; i++)
                {
                    // Create new instance of the navigation data
                    NavigationData data = new NavigationData();
    
                    // Set navigation data properties
                    data.Altitude = i;
                    data.Depth = i;
    
                    // Add data to list
                    dataCollection.Add(data);
                }
            }
    
            static void PrintData()
            {
                for (int i = 0; i < dataCollection.Count; i++)
                {
                    // Get navigation data from list
                    NavigationData data = dataCollection[i];
    
                    // Print navigation data
                    Console.WriteLine(string.Format("Navigation data {0} - Altitude={1} - Depth={2}", i, data.Altitude, data.Depth)); 
                }
            }
        }
    
    
        [Serializable()]
        public class NavigationData
        {
            [XmlElement("Depth")]
            public double Depth { get; set; }
            [XmlElement("Altitude")]
            public double Altitude { get; set; }
        }
    }
    

    • Marked as answer by s_b1 Thursday, June 7, 2012 2:57 PM
    Wednesday, June 6, 2012 6:56 PM
  • Example:

    using System;
    using System.Collections.Generic;
    using System.Xml.Serialization;
    
    namespace TestConsole
    {
        class Program
        {
            // Create list of navigation data
            static List<NavigationData> dataCollection = new List<NavigationData>();
    
            static void Main(string[] args)
            {
                LoadData();
    
                PrintData();
    
                Console.ReadKey();
            }
    
            static void LoadData()
            {
                // Populate list
                for (int i = 0; i < 10; i++)
                {
                    // Create new instance of the navigation data
                    NavigationData data = new NavigationData();
    
                    // Set navigation data properties
                    data.Altitude = i;
                    data.Depth = i;
    
                    // Add data to list
                    dataCollection.Add(data);
                }
            }
    
            static void PrintData()
            {
                for (int i = 0; i < dataCollection.Count; i++)
                {
                    // Get navigation data from list
                    NavigationData data = dataCollection[i];
    
                    // Print navigation data
                    Console.WriteLine(string.Format("Navigation data {0} - Altitude={1} - Depth={2}", i, data.Altitude, data.Depth)); 
                }
            }
        }
    
    
        [Serializable()]
        public class NavigationData
        {
            [XmlElement("Depth")]
            public double Depth { get; set; }
            [XmlElement("Altitude")]
            public double Altitude { get; set; }
        }
    }
    

    That is so very useful! So now I see how a List is used.

    Thank you Adnan!

    Thursday, June 7, 2012 3:02 PM