Solved

c# interface

Posted on 2011-02-24
6
254 Views
Last Modified: 2013-12-17
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
Comment
Question by:jtran007
6 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
>>>  (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
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
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
 
LVL 29

Expert Comment

by:anarki_jimbel
Comment Utility
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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 11

Accepted Solution

by:
Sudhakar Pulivarthi earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:jtran007
Comment Utility
Thanks,
JT
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now