Solved

c# interface

Posted on 2011-02-24
6
265 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
Independent Software Vendors: 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 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
T-SQL and CLR parameter strings 9 63
c#  FTP ftpwebrequest URI invalid 6 47
How to input barcode scanner data into textbox in C#  ? 13 61
What are MicroServices? 4 67
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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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