none
ToArray for collection of strings with null in it RRS feed

  • Question

  • I have a collection of strings that I'm trying to extract into an array of strings. I use the code below.

    dim values() as string

    values = park.Attributes.Values.ToArray()

    Occasionally, one of the entries in Values is "Nothing", and ToArray throws an exception. I then Join the strings in the array, and it is unaffected by the null entry, but I want to manipulate the values returned by the ToArray before I do that.

    I've tried values = park.Attributes.Values.Select(Function(s) If(s, "")).ToArray() but it has the same problem.

    How can I convert a string collection to an array, and not throw an exception on a nul values, and just convert it to an empty string?

    Friday, March 2, 2018 4:43 AM

Answers

  • It is difficult to find the part of the error details that indicate about a problem with null value. But there is a “Unable to cast” message, that clearly indicates a different situation.

    Sometimes such problems can be identified earlier if you include ‘Option Strict On’ at the beginning.

    If Values is declared as a collection of Objects, not Strings, then try this:

       values = park.Attributes.Values.Select(Function(s) If(s, String.Empty).ToString).ToArray

    Friday, March 2, 2018 7:26 AM

All replies

  • I do not think that the exception in this line is caused by null elements. Probably one of park, Attributes or Values is Nothing. In order to make it work, in modern VB you can do this:

       values = If(park?.Attributes?.Values?.ToArray(), {})

    If you also want to replace null elements with empty strings, then:

       values = If(park?.Attributes?.Values?.Select(Function(s) If(s, String.Empty))?.ToArray(), {})

    • Edited by Viorel_MVP Friday, March 2, 2018 6:00 AM
    Friday, March 2, 2018 5:57 AM
  • All of park, Attributes and Values variables exist. I can view them in the debugger. One of my Values collection definitely has the value "nothing".

    Both of your suggested solutions are crashing with the same exception.

               System.Reflection.TargetInvocationException occurred
      HResult=0x80131604
      Message=Exception has been thrown by the target of an invocation.
      Source=<Cannot evaluate the exception source>
      StackTrace:
       at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
       at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
       at System.Delegate.DynamicInvokeImpl(Object[] args)
       at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
       at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
       at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at Parks.My.MyApplication.Main(String[] Args) in :line 81

    Inner Exception 1:
    InvalidCastException: Unable to cast object of type 'System.Object[]' to type 'System.String[]'.



    Friday, March 2, 2018 6:54 AM
  • It is difficult to find the part of the error details that indicate about a problem with null value. But there is a “Unable to cast” message, that clearly indicates a different situation.

    Sometimes such problems can be identified earlier if you include ‘Option Strict On’ at the beginning.

    If Values is declared as a collection of Objects, not Strings, then try this:

       values = park.Attributes.Values.Select(Function(s) If(s, String.Empty).ToString).ToArray

    Friday, March 2, 2018 7:26 AM