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
Solved

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

Posted on 2008-10-18
10
6,900 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
How our DevOps Teams Maximize Uptime

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

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

829 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