Solved

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

Posted on 2008-10-18
10
6,918 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
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

695 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