Solved

Cannot implicitly convert type 'string[]' to 'string'

Posted on 2008-10-18
10
6,882 Views
Last Modified: 2013-12-17
I am getting the following compile error message :

Cannot implicitly convert type 'string[]' to 'string'

I think this error relates to   ._inputWords[0].  
public static void removeAllNonAlphaTextFromString(string inputText)

        {

            GlobalVars._inputWords[0].inputItem =

                inputText.Split(new char[] { ' ', ',', ';', '.', '-', '/', '|', '\\',

                                             '%', '#', '@', '!', '~', '$', '^', '(',

                                             ')', '+', '[', ']', '{', '}', '"', ':', 

                                             '<', '>', '?',  '\r', '\n', '*' }, StringSplitOptions.RemoveEmptyEntries);
 
 

________________________________________________________
 

 public struct InputWords

        {

            public string inputItem;

            public bool itemRecognised;

        }
 

        private static InputWords[] inputWords;
 

        public static InputWords[] _inputWords

        {

            get { return inputWords; }

            set { inputWords = value; }

        }

Open in new window

0
Comment
Question by:amillyard
  • 6
  • 4
10 Comments
 
LVL 13

Expert Comment

by:josgood
ID: 22750066
inputText.Split an array of string, a string[], which can't be assigned to inputData because it is a single string.

This example will compile, but you no doubt want to do something more sophisticated

            string[] answer =
                inputText.Split(new char[] { ' ', ',', ';', '.', '-', '/', '|', '\\',
                                             '%', '#', '@', '!', '~', '$', '^', '(',
                                             ')', '+', '[', ']', '{', '}', '"', ':',
                                             '<', '>', '?',  '\r', '\n', '*' }, StringSplitOptions.RemoveEmptyEntries);
            GlobalVars._inputWords[0].inputData = answer[0];
0
 

Author Comment

by:amillyard
ID: 22750095
josgood:  mmm, I see.   just to confirm result expectation --

string inputText --> provide a sentence of multiple words.

removeAllNonAlphaTextFromString  should create either a single word or multiple words back into the InputWords -- which I can later use/list i.e. [0]. [1], [2] etc.
0
 
LVL 13

Expert Comment

by:josgood
ID: 22750108
You could do something like this...
            for (int i = 0; i < Math.Min(answer.Length,GlobalVars._inputWords.Length; i++) {
               GlobalVars._inputWords[i].inputData = answer[i];
            }

Obviously, you'd need to define the handling for answers.Length being less than or greater than GlobalVars._inputWords.Length.  I just took the shorter of the two in the loop I'm showing.
0
 

Author Comment

by:amillyard
ID: 22750204
ok, will look at this a slgihtly different way ... just to understand better how to :

just add additional entries ...[5],[6] etc... do I simply just make reference to the additonal entries ?

GlobalVars._inputWords[7].inputData = "abc";

how do you removed some of these entries... i.e.  [9],[10] -- because those 2 entries are not used anymore -- but still need 0-8 entries  --  or do these extra entries just remain there indefinately?
0
 
LVL 13

Expert Comment

by:josgood
ID: 22750315
An array is a static allocation, so simply referencing [5][6] as additional entries will not create new elements of the array.

However, List<string> is not a static allocation -- it can grow and shrink (at least in concept, I'm not sure what goes on under the hood) in response to calls the Add and Remove methods respectively.  Plus it supports member reference by the [5] kind of notation you're familiar with in arrays.

List<T> is in the System.Collections.Generic namespace.

Is that an approach that would work for you?  I can give an example, if you like.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:amillyard
ID: 22751434
josgood:  yes, that appears to be a perfect fit for the task to hand
0
 

Author Comment

by:amillyard
ID: 22751448
(please forward example of how to implement -- many thanks)
0
 
LVL 13

Accepted Solution

by:
josgood earned 500 total points
ID: 22752125
Here is an example which I hope illustrates List usage.

Some points of interest:
1)  I added a constructor to struct MyData
      public MyData(string word, bool b, int i) { inputData = word; BoolValue = b; IntValue = i; }
2)  I made inputwords a private member
        private static List<MyData> inputWords = new List<MyData>();
     simply because data should be encapsulated rather than made publicly available
3)  removeAllNonAlphaTextFromString clears previous results before adding new ones and uses the MyData constructor to create a new item for the list.
4)  The results are accessed through a new method
   public static List<string> GetWords() {

This code runs on my box.  

using System;
using System.Data;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

public class GlobalVars {
   public struct MyData
   {
      public MyData(string word, bool b, int i) { inputData = word; BoolValue = b; IntValue = i; }
      public string inputData;
      bool BoolValue;
      int IntValue;
   }
   
   private static List<MyData> inputWords = new List<MyData>();

   public static void removeAllNonAlphaTextFromString(string inputText)
   {
      string[] words =
         inputText.Split(new char[] { ' ', ',', ';', '.', '-', '/', '|', '\\',
                                      '%', '#', '@', '!', '~', '$', '^', '(',
                                      ')', '+', '[', ']', '{', '}', '"', ':',
                                      '<', '>', '?',  '\r', '\n', '*' }, StringSplitOptions.RemoveEmptyEntries);
      // Remove data left over from any previous calls
      inputWords.Clear();
      // Copy all individual words into the list
      int count = 1;
      foreach (string word in words) {
         MyData anEntry = new MyData(word,true,count++);
         inputWords.Add(anEntry);
      }
   }

   public static List<string> GetWords() {
      List<string> result = new List<string>();
      foreach (MyData anEntry in inputWords) result.Add(anEntry.inputData);
      return result;
   }
}

class Demo {
   
   static void Main(string[] args) {
      HandleOneSentence("The quick brown fox jumped over the lazy dog");
      HandleOneSentence("76 trombones led the big parade");
   }
   
   static void HandleOneSentence(string aSentence) {
      GlobalVars.removeAllNonAlphaTextFromString(aSentence);
      List<string> words = GlobalVars.GetWords();
      foreach (string aWord in words) Console.WriteLine(aWord);
      Console.WriteLine("There were {0} words in the input string",words.Count);
      Console.WriteLine(" "); // blank line
   }
}

0
 

Author Comment

by:amillyard
ID: 22753371
josgood: 100% spot on :-)  thank you very much for your valued time and efforts with explaining further -- much apprieated.
0
 

Author Comment

by:amillyard
ID: 22753698
Can I please just ask a question?

trying to access/update the Bool variable from the struct.list as follows -- but not working.

GlobalVars.WordPartitions.recognisedWord[segmentedWord - 1] = Dictionary.DiscoverWordItem(aWord);

does the list work as a single list of strings ... or includes all items in the :: public struct WordPartitions
public struct WordPartitions

        {

            public WordPartitions(string word, bool recognisedWord) { inputData = word; inputDataRecognised = recognisedWord;}

            public string inputData;

            public bool inputDataRecognised;

        }
 

        public static List<WordPartitions> inputWords = new List<WordPartitions>();

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

21 Experts available now in Live!

Get 1:1 Help Now