1

I am using Mono Develop For Android and would like some help with using an array of structs.

Here is my code:

public struct overlayItem
{
    string stringTestString;
    float floatLongitude;
    float floatLatitude;
}

And when using this struct:

overlayItem[1] items;
items[0].stringTestString = "test";
items[0].floatLongitude = 174.813213f;
items[0].floatLatitude = -41.228162f;

items[1].stringTestString = "test1";
items[1].floatLongitude = 170.813213f;
items[1].floatLatitude = -45.228162f;

I am getting the following error at the line:

overlayItem[1] items;

Unexpected symbol 'items'

Can I please have some help to correctly create an array of the above struct and then populate it with data.

Thanks

1

4 Answers 4

3

Define your struct like:

overlayItem[] items = new overlayItem[2];

Also you need to define your fields in the struct as public, to be able to access them outside the struct

public struct overlayItem
{
    public string stringTestString;
    public float floatLongitude;
    public float floatLatitude;
}

(you may use Pascal case for your structure name)

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

2 Comments

+1 for adding Pascal Case and pointing to the properties to be public.
@Asif: Fields, not properties. While there are times when structures should have properties which behave semantically different from fields, if a structure member is going to behave like a public field, it should be a public field.
1

You need to create your struct array like so:

overlayItem[] items = new overlayItem[2];

Remember to declare it with [2] as it will have 2 elements, not 1! Indexing an array might start at zero, but defining an array size does not.

Comments

1

Your sample code shows you need two items, so you need to declare the array of structs with length 2. This can be done with:

overlayItem[] items = new overlayItem[2];

Comments

1

The right way to declare the struct array for two elements is

overlayItem[] items = new overlayItem[2];

If you do not know the exact no of items you can also use list.

List<overlayItem> items = new List<overlayItem>();

items.Add( new overlayItem {
               stringTestString = "test";
               floatLongitude = 174.813213f;
               floatLatitude = -41.228162f; 
           }
);

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.