Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to convert short?[ ] to string[ ]

How to convert short?[ ] to string[ ]

Scheduled Pinned Locked Moved C#
data-structureshelptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    haz13
    wrote on last edited by
    #1

    I have an array full of values of type short?[] and want to display them in a MultiLine Textbox which requires values of type string[]. I can not figure out how to convert between the two value types. Here is my code that I thought would work but I get error - "can not convert type short?[] to string[] " NumArray = new short?[37]; public void SetFormData() { textbox1.Lines = new string[37] ; textbox1.Lines = (string[])NumArray; }

    Haz

    C 1 Reply Last reply
    0
    • H haz13

      I have an array full of values of type short?[] and want to display them in a MultiLine Textbox which requires values of type string[]. I can not figure out how to convert between the two value types. Here is my code that I thought would work but I get error - "can not convert type short?[] to string[] " NumArray = new short?[37]; public void SetFormData() { textbox1.Lines = new string[37] ; textbox1.Lines = (string[])NumArray; }

      Haz

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      You can't convert like that. Try this:

      string[] stringArray = new string[37];
      for(int i=0; i<37; i++)
      {
      short? num = NumArray[i];
      if (num != null)
      stringArray[i] = NumArray[i].ToString();
      else
      stringArray[i] = ""; // Or what ever else you want in place of null
      }
      textbox1.Lines = stringArray;


      Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

      H 1 Reply Last reply
      0
      • C Colin Angus Mackay

        You can't convert like that. Try this:

        string[] stringArray = new string[37];
        for(int i=0; i<37; i++)
        {
        short? num = NumArray[i];
        if (num != null)
        stringArray[i] = NumArray[i].ToString();
        else
        stringArray[i] = ""; // Or what ever else you want in place of null
        }
        textbox1.Lines = stringArray;


        Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

        H Offline
        H Offline
        haz13
        wrote on last edited by
        #3

        Thanks for your speedy response. Your solution works really well in my program. :)

        Haz

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups