Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

c# interface

Hi ,
I don't understand why at run time I got error:
class SerialPort
{

        private object             phyObject = null;

 public enum PhyTypes              { SerialPort };
  #region PhyDriver Interface
    internal interface I_PhyDriver
    {
        #region Properties
        bool IsOpen
        {
            get;
        }

        bool UsePacketMode
        {
            get; set;
        }
        #endregion

        #region Methods
        int Config(string Port, int Baud, int Data, Parity P, StopBits S);
        int Open();
        int Read(out byte[] data);
        int RemoveDataFromBuffer(int Size);
        int GetBufferSnapShot(out byte[] buf);
        int Write(byte[] data);
        int Close();
        int DataPending();
        #endregion

        #region Events
        #endregion
 #region method
private void ProcessSeriralPort()
{
..
                         (phyObject as SerialPort).PortName = Port;  <--- compiling ok but at run time Error
                        (phyObject as SerialPort).BaudRate = 9600;
                        (phyObject as SerialPort).DataBits = 8;
                        (phyObject as SerialPort).Parity   = Parity.None;
                        (phyObject as SerialPort).StopBits = StopBits.One;
}
#endregion

    }
}

     public PhyTypes PhyType
        {
            get { return (_phyType); }
            set
            {
                _phyType = value;

                switch (_phyType)
                {
                    case PhyTypes.SerialPort:
                        // serial port phy requested
                        phyObject = new O_Serial();
                        phyDriver = (phyObject as I_PhyDriver);
                        break;
                    default:
                        // unknown (should never happen since using Enum)
                        phyObject = null;
                        break;
                } // switch
                //if (m_PhySettingsNeeded != null)
                //{
                //    // report phyObject
                //    m_PhySettingsNeeded(this, ref phyObject);
                //} // if
            }
        }

Could you help me on this error?

Thanks,
JT
0
jtran007
Asked:
jtran007
1 Solution
 
AndyAinscowCommented:
>>>  (phyObject as SerialPort).PortName = Port;  <--- compiling ok but at run time Error

What error?

Is phyObject really a SerialPort object, if it isn't then that could be why it errors at run time.  Put a breakpoint on this line of code and look.
0
 
Carl TawnSystems and Integration DeveloperCommented:
I would guess it is something to do with "phyObject" not having being initialized. You've only show fragments of code, but it would appear that the object is only initialized in the "PhyType" property; so if you don't set that property at any point then your object will never be initialized.
0
 
anarki_jimbelCommented:
As carl_tawn told you are showing only some code. I can't see where PhyObject comes from etc.

More details would be beneficial. My feeling is that PhyObject has wrong type - so casting will throw an exception.

It would be extremely useful to see entire Exception stack and message. They may provide lot of info.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Sudhakar PulivarthiCommented:
Hi,
 (phyObject as SerialPort).PortName = Port;  <--- compiling ok but at run time Error
as operator will not throw exception. If the phyObject is not a SerialPort object then as will return null . Since u have written .PortName with out checking the cast success causing to throw NullReference exception.

AS many we suspect phyObject is declared wrongly with other object type. as it should be
SerialPort phyObject  = new SerialPort(); // like this

Please verify the cast success using is operator
if (phyObject is SerialPort)
{
SerialPort serialPort = phyObject as SerialPort; // on success return SerialPort casted object
serialPort.PortName = Port;
serialPort.BaudRate = 9600;
serialPort.DataBits = 8;
serialPort.Parity   = Parity.None;
serialPort.StopBits = StopBits.One;
 }
0
 
jtran007Author Commented:
Thanks,
JT
0
 
AndyAinscowCommented:
Note the simultaneous comments by myself and carl_tawn many hours before the comment accepted as an answer saying the same thing.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now