Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

c# interface

Posted on 2011-02-24
6
Medium Priority
?
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34970125
>>>  (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
ID: 34970216
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 30

Expert Comment

by:anarki_jimbel
ID: 34975961
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
Industry Leaders: 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!

 
LVL 11

Accepted Solution

by:
Sudhakar Pulivarthi earned 2000 total points
ID: 34978754
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
ID: 34988269
Thanks,
JT
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34988321
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 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