Solved

c# interface

Posted on 2011-02-24
6
255 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
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 29

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 11

Accepted Solution

by:
Sudhakar Pulivarthi earned 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

12 Experts available now in Live!

Get 1:1 Help Now