0

I'm new to C# and have been modifying some code a previous employee wrote. If I understand correctly, and I'm sure I dont, I need to add an Add method to the MachineInfoClass. It appears to already have one near the end. Maybe its the scope of the method not being public? and help for this green c# programmer would be greatly appreciated.

namespace AIMS_CMM_Calibration_Reporter
{
    using System;
    using System.Collections;
    using System.Collections.Generic;

    /// <summary>
    /// Class to describe information related the CMM that was calibrated
    /// </summary>
    public class MachineInfoClass:IEnumerator,IEnumerable
    {

        public MachineInfoClass()
        {`enter code here`
            //default empty constructor
        }

        public string SerialNumber { get; set; }
        public string ModelNumber { get; set; }
        public string YearBuilt { get; set; }

        //private double _measUncertMicrons = 0.1;
        //public double MeasUncertMicrons
        //{
        //    get { return _measUncertMicrons; }
        //    set
        //    {//TODO: Measurement Uncertainty maths
        //        _measUncertMicrons = value;
        //        double linearUncert = Math.Round(_measUncertMicrons + (1000 / 250), 1);
        //        double volumnUncert = Math.Round(linearUncert * 2, 1);
        //        _measUncertCalcText = "MPE = " + _measUncertMicrons.ToString() + " um +( L / 250.0 mm) um  Value outside maximum limit (Linear/Volumetric uncertainty to 1000mm " + linearUncert.ToString() + "um/ " + volumnUncert.ToString() + "um)";
        //    }
        //}

        //private string _measUncertCalcText = string.Empty;
        public string MeasUncertText { get; set; } = string.Empty;
        //{
        //    get { return _measUncertCalcText; }
        //}

        public string MachinePerformanceSpec { get; set; } = string.Empty;

        public MeasurementRange MeasRange { get; set; } = new MeasurementRange();
        public ProbeSystemClass ProbeDetails { get; set; } = new ProbeSystemClass();

        object IEnumerator.Current
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        /// <summary>
        /// Class to describe information related to the CMM capabilities
        /// </summary>
        public class MeasurementRange
        {
            public string MachineUnits { get; set; }
            public string Xrange { get; set; }
            public string Yrange { get; set; }
            public string Zrange { get; set; }
        }

        bool IEnumerator.MoveNext()
        {
            throw new NotImplementedException();
        }

        void IEnumerator.Reset()
        {
            throw new NotImplementedException();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        public static implicit operator MachineInfoClass(List<MachineInfoClass> v)
        {
            throw new NotImplementedException();
        }

        internal void Remove(object item)
        {
            throw new NotImplementedException();
        }
        

        
        internal void Add(MachineInfoClass newCMM)
        {
            throw new NotImplementedException();
        }
    }
}

****This is where the error actually pops up in frmMain.CS at the xlmserializer ****

 public bool SaveReport(string filePath)
        {
            try
            {
                //load values from the form into the calreport object
                if (!saveValuesintoCalReportObj()) throw new Exception("Form to CalReportObj failed.");
                //save the calibration object data to xml
                XmlWriterSettings ws = new XmlWriterSettings();
                ws.NewLineHandling = NewLineHandling.Entitize; //<-- needed to keep newlines for addresses, etc

                XmlSerializer xSave = new XmlSerializer(typeof(CalibrationReport));
                using (XmlWriter xWrite = XmlWriter.Create(filePath, ws))
                {
                    xSave.Serialize(xWrite, calReport);
                }

                activeReportXMLFilename = filePath;
                this.Text = Application.ProductName + "  ::  " + activeReportXMLFilename;
                updateMRUlist(activeReportXMLFilename);

                return true;
            }
            catch (Exception ex)
            {
                //String innerMessage = (ex.InnerException != null)
                 //     ? ex.InnerException.Message
                 //     : "";
                MessageBox.Show("Failed to serialize and/or save." + Environment.NewLine + Environment.NewLine + ex.Message );
                return false;
            }
        } 
Bobbert
  • 1
  • 1
  • Have you tried making it `public`? – 500 - Internal Server Error Mar 22 '21 at 21:49
  • From the [docs](https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer?view=netframework-4.8#overriding-default-serialization): *A class that implements `IEnumerable` must implement a public `Add` method that takes a single parameter. The `Add` method's parameter must be of the same type as is returned from the `Current` property on the value returned from `GetEnumerator`* Your enumerator returns an `object`. – dbc Mar 23 '21 at 02:59
  • I also don't recommend implementing both `IEnumerator` and `IEnumerable` on the same class! Doing that means that your `MachineInfoClass` cannot be enumerated more than once at the same time. Keep them separate. See [IEnumerable and IEnumerator in the same class, bad idea?](https://stackoverflow.com/q/2519168/3744182). – dbc Mar 23 '21 at 03:00
  • Also, you should use an implement typed generic enumerators (i.e. `IEnumerable` where `T` is whatever type of thing the enumerable loops over). In this case I reckon it would be `IEnumerable` -- though that is weird. What exactly is this class supposed to do? If you are *modifying some code a previous employee wrote* can you share the original code + explain what you are trying to accomplish? You might have an [xy problem](https://meta.stackexchange.com/q/66377) and should rethink your approach. – dbc Mar 23 '21 at 03:03
  • Welcome to StackOverflow Bobbert! Could you clarify your question a bit? Are you asking if the Add function already there is valid? Or is the code throwing an error that asks for an Add method when compiled? A little more specificity as to the actual problem you are encountering would be helpful. – Logan Kitchen Mar 23 '21 at 03:20
  • If you are thinking of making `MachineInfoClass` be simultaneously an object with machine info properties, and a collection of child `MachineInfoClass` objects, then don't do that. See [Why not inherit from List?](https://stackoverflow.com/q/21692193/3744182) for why this is a bad idea in general. But more specifically, `XmlSerializer` **never** serializes the properties of collections, only the items. See e.g. [XmlSerialize a custom collection with an Attribute](https://stackoverflow.com/q/377486/3744182). Make the children be a separate collection property. – dbc Mar 23 '21 at 04:19
  • Thanks for all the responses. Adding a public Add method and removing :IEnumerator,IEnumberable from the class definitions fixed the problem. Oddly enough, these parameters were added by VS as suggested fixes. I initially only changed a font in a text box which generated 16 errors which I let VS fix according to the suggestions. What a rabbit hole. – Bobbert Mar 24 '21 at 13:21

0 Answers0