0

This could be very easy, but how can I place or convert a string into an array?

The code that I have, is the following:

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string one;
        string[] two;

        one = "Juan";
        two = {one}; // here is the error

        HttpContext.Current.Response.Write(two);
    }
}

And the error is the following: Compiler Error Message: CS0029: Cannot implicitly convert type 'string' to 'string[]'

Thanks for you help!

0

4 Answers 4

5

Replace this:

two = {one}; // here is the error

With

two = new[] { one }; 

OR

two = new string[] { one };

The reason you are getting the error is clear from the error message.

See: Object and Collection Initializers (C# Programming Guide)

Later when you are doing Response.Write, you will get System.String[] as output, since two is an array. I guess you need all array elements separated by some delimiter. You can try:

HttpContext.Current.Response.Write(string.Join(",", two));

Which will produce all the elements in the array separated by comma

Sign up to request clarification or add additional context in comments.

5 Comments

The system is showing the following: Type expected in the same line. Thanks for your quick answer.
@JulianMoreno, try two = new string[] { one };
The system shows the the folowing: The best overloaded method match for 'string.Join(string, string[])' has some invalid arguments.
@JulianMoreno, my bad, it should be string ",", not character ',' updated the answer
It works! Also The HttpContext.Current.Response.Write(string.Join(',', two)**)**; lacks from a parenthesis ")". Thanks for your quick help!
2

It looks like you're trying to use initialization syntax for an assignment. This should work:

two = new string[] {one};

or just

two = new [] {one};

since the compiler will infer that you want a string[]

I think you'll also be surprised what Response.Write(two); produces...

Comments

0

You're using the static initializer syntax to try and add an item to your array. That doesn't work. You can use similar syntax to allocate a new array with the value one - two = new string[] { one }; - or you can allocate the array then add elements through assignment like;

    string[] two = new string[10];
    two[0] = one; // assign value one to index 0

If you do it like this you have to do some bounds checking for example the following will throw an IndexOutOfRangeException at runtime;

    string[] two = new string[10];
    int x = 12;
    two[x] = one; // index out of range, must ensure x < two.Length before trying to assign to two[x]

Comments

0

That syntax ({one}) is only valid if you declare the array variable in the same line. So, this works:

string one;

one = "Juan";
string[] two = {one};

A more common way to initialize an array, which works in more places, is to use the new keyword, and optionally have the type be inferred, e.g.

string one;
string[] two;

one = "Juan";
// type is inferrable, since the compiler knows one is a string
two = new[] {one};
// or, explicitly specify the type
two = new string[] {one};

I usually declare and initialize on the same line, and use var to infer the type, so I'd probably write:

var one = "Juan";
var two = new[] { one };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.