0

I have a function (function1) in my winforms app(c#) that return an array

inside the function1

public array function1(string value)
{
string[] array = new string[12];
     //assigning values to the array elements....
retrun array;
}

and i call this function like this

string id="Some id";
string[] array2 = new string[12];
array2=this.function1(id);

but it gives me error

Cannot implicitly convert type 'System.Array' to 'string[]'. An explicit conversion exists (are you missing a cast?)

please experts help me !

2
  • I can't get how it even came to this: error CS0246: The type or namespace name `array' could not be found. Commented Mar 3, 2013 at 16:21
  • An "array" as returned by function can be an array of anyuthing; however array2 can ONLY hold references to an array of string. Hence the error. Change the return type of function to string[]. Commented Mar 3, 2013 at 16:21

3 Answers 3

7

Change

public array function1(string value)

to

public string[] function1(string value)

And in your example of usage, you do not need to initialize your array to new string[12] before hand, as your function returns a new array anyway.

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

Comments

2

The signature of function1 should be public string[] function1(string value). The return type array doesn't match the variable of type string[]

Comments

1

This works, but not sure what the problem is:

public Form1()
{
    InitializeComponent();
    string id = "Some id"; 
    string[] array2 = new string[12]; 
    array2 = this.function1(id);
}

public string[] function1(string value)
{
    string[] array = new string[12];
    array[0] = value; // for example
    return array;
}

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.