14
/// <summary>
/// Returns an array of all ArtworkData filtered by User ID
/// </summary>
/// <param name="UsersID">User ID to filter on</param>
/// <returns></returns>
public static Array[] GetDataRecords(int UsersID)
{
    ArtworkData[] Labels;
    Labels = new ArtworkData[3];

    return Labels[];
}

I get a syntax error, ; expected after return Labels[].

Am I doing this right?

6 Answers 6

32

You're trying to return variable Labels of type ArtworkData instead of array, therefore this needs to be in the method signature as its return type. You need to modify your code as such:

public static ArtworkData[] GetDataRecords(int UsersID)
{
    ArtworkData[] Labels;
    Labels = new ArtworkData[3];

    return Labels;
}

Array[] is actually an array of Array, if that makes sense.

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

1 Comment

public static ArtworkData[] GetDataRecords(int UserID) { List<ArtworkData> records = Database.GetArtworkByUser(userId); return records.ToArray(); }
7

return Labels; should do the trick!

public static ArtworkData[] GetDataRecords(int UsersID)
{
    ArtworkData[] Labels;
    Labels = new ArtworkData[3];

    return Labels;
}

Comments

5
public static ArtworkData[] GetDataRecords(int UsersID)
{
    ArtworkData[] Labels;
    Labels = new ArtworkData[3];

    return Labels;
}

This should work.

You only use the brackets when creating an array or accessing an array. Also, Array[] is returning an array of array. You need to return the typed array ArtworkData[].

Comments

2

Two changes are needed:

  1. Change the return type of the method from Array[] to ArtWorkData[]
  2. Change Labels[] in the return statement to Labels

Comments

0

You should return the variable withouth the brackets

Return Labels

Comments

-4
 static void Main()
     {
             for (int i=0; i<GetNames().Length; i++)
               {
                    Console.WriteLine (GetNames()[i]);
                }
     }

  static string[] GetNames()
   {
         string[] ret = {"Answer", "by", "Anonymous", "Pakistani"};
         return ret;
   }

3 Comments

Format the code and add some explanation, this looks terrible.
Other than the obvious fault in that you hardcoded in an array, there is a huge inefficiency in your code. Every time you code GetNames(), you have to enter the function call, create a new string array, and return the reference to that array. While each array is theoretically the same, every time you do so, it creates an entirely new instance, which is space inefficient. It is also time inefficient, as you can store the array in the Main, and even store the length separately (calling .Length is also time consuming).
Does not answer debugging question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.