?
Solved

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

Posted on 2008-10-18
10
Medium Priority
?
6,926 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
[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
  • 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
Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

 

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
 

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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

762 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