none
How to fix "Unable to cast object of type 'System.Collections.Generic.List`1[System.Array]' to type 'System.IConvertible'." RRS feed

  • Question

  • Hi,

    I am getting an error "Unable to cast object of type 'System.Collections.Generic.List`1[System.Array]' to type 'System.IConvertible'." I am not sure what mistake I made, can somebody help?

    privatestaticbool GetPorts()         {             bool portavail = true;             string[] ports = SerialPort.GetPortNames();             IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();             TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();             List<Array> Ports = newList<Array>();             Ports.Add(ports);             foreach (TcpConnectionInformation tcpi in tcpConnInfoArray)             {                 try                 {                     if (tcpi.RemoteEndPoint.Port == Convert.ToInt32(Ports.ToList()))                     {                         MessageBox.Show("Your port is busy right now. Please try again");                         portavail = false;                         break;                     }                     else                     {                         Console.WriteLine("PC is connected to the port number" + tcpi.RemoteEndPoint.Port);                     }                 }                 catch (SocketException ex)                 {                     MessageBox.Show(ex.Message, "No port is detected from your PC");                 }             }             return portavail;         }

    I am getting the error in below line

    if (tcpi.RemoteEndPoint.Port == Convert.ToInt32(Ports.ToList()))
       

    Exception in Detail:

    System.InvalidCastException was unhandled by user code
      HResult=-2147467262
      Message=Unable to cast object of type 'System.Collections.Generic.List`1[System.Array]' to type 'System.IConvertible'.
      Source=mscorlib
      StackTrace:
           at System.Convert.ToInt32(Object value)
           at WizardApplication.AddPDF.GetPorts() in C:\Galileo Print Manager\AddDeviceWizard\WizardApplication\WizardApplication\WizardSteps\AddPDF.xaml.cs:line 271
           at WizardApplication.AddPDF.OnAddFormCodeClick(Object sender, RoutedEventArgs e) in C:\Galileo Print Manager\AddDeviceWizard\WizardApplication\WizardApplication\WizardSteps\AddPDF.xaml.cs:line 181
           at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
           at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
           at System.Windows.Controls.Primitives.ButtonBase.OnClick()
           at System.Windows.Controls.Button.OnClick()
           at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
           at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
           at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
           at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
           at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
           at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
           at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
           at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
           at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
           at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
           at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
           at System.Windows.Input.InputManager.ProcessStagingArea()
           at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
           at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
           at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
           at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
           at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
           at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
           at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
           at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
           at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
           at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
           at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
           at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
           at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
           at System.Windows.Window.ShowHelper(Object booleanBox)
           at System.Windows.Window.ShowDialog()
      InnerException: 

    Thanks


    Tuesday, February 20, 2018 4:30 PM

Answers

  • Tcpi.RemoteEndPoint.Port is a number (example: 443).

    Ports is a list, and Ports.ToList() is also a list. It includes a single element, which is an array of serial ports, such as “COM1”, “COM2”, etc. A list cannot be converted to integer using Convert, since such conversion does not have an obvious sense. Convert refuses to do it.

    Maybe you should give more details about the purpose of this if and of this fragment.

    Tuesday, February 20, 2018 6:45 PM

All replies

  • Tcpi.RemoteEndPoint.Port is a number (example: 443).

    Ports is a list, and Ports.ToList() is also a list. It includes a single element, which is an array of serial ports, such as “COM1”, “COM2”, etc. A list cannot be converted to integer using Convert, since such conversion does not have an obvious sense. Convert refuses to do it.

    Maybe you should give more details about the purpose of this if and of this fragment.

    Tuesday, February 20, 2018 6:45 PM
  • Thanks for your briefing
    Thursday, February 22, 2018 7:26 AM