Solved

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

Posted on 2008-10-18
10
6,887 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 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

16 Experts available now in Live!

Get 1:1 Help Now