this exception makes no sense to me:
Cannot implicitly convert type 'object' to 'char[]'. An explicit conversion exists (are you missing a cast?)

as you can see charQuery is char array and not an object:

string searchedBand = this.txtsearchBand.Text;
            char[] charQuery = searchedBand.ToCharArray();
            String[] dbQuery = new String[(charQuery.Length)];
            for (int i = 0; i < charQuery.Length; i++)
                char[] option = charQuery.Clone();
                option[i] = '*';
                dbQuery[i] = option.ToString();

any reason what the problem is?


Recommended Answers

All 6 Replies

Exception accrues in line 6:
char[] option = charQuery.Clone();

The Clone method always returns an object of type object.
A char[] is in fact also an object, but of type char[].
C# is very picky about this.
With reason, you cannot compare apples with pears.
So if you are absolutly sure your pear is in fact an apple, do the following : (use a cast)
char[] option = (char[])charQuery.Clone();

commented: Excellent analogy! +2

You can also use the as operator like:
char[] option = charQuery.Clone() as char[];

char[] option = charQuery.Clone();

In a loop u are declaring an array. Please its so wrong.

Declare it outside the for loop and enter the values inside the loop that should solve the problem for itself.

And your logical concept is not clear to me. You want to put the values element by element into this new array right ? So
do this
if u want to copy items of charQuery in option.

and remember everything is of object type since every data type derives from System.object class.

Read more books.

You are right lonelyrider.
Your suggestion is what I would use too.
I was just anwsering his/her question, Clone works but it copies charQuery and assigns it to option.
And OP's logic is indeed weird:
-->copy charQuery and assign it to option
-->set char i of option to '*'
-->assign option.ToString to string i of the dbQuery string array, wich would fill the array with "System.Char[]" strings.

There is more.
Because the option variable is defined INSIDE the for loop, it does not exist ouside the loop, but perhaps that is the intention...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.