0

Say I have a struct like

public struct pair{ float x,y;}

I want to create a constant lookup array of pairs inside a class, its also fixed number. Something like

public class MyClass{
    static readonly fixed pair[7] _lookup;
}

I dont know how to declare nor initialize it(where do I set the values for each one?).

3
  • can you explain what you mean: how to declare nor initialize it? Commented Nov 6, 2015 at 17:12
  • I dont know the correct syntax, and I dont know how to initialize it with the values I want. Commented Nov 6, 2015 at 17:14
  • you can start from guide Commented Nov 6, 2015 at 17:16

2 Answers 2

4

You can also use a static constructor

public struct pair
{
    float x, y;

    public pair(float x, float y)
    {
        this.x = x;
        this.y = y;
    }
}

public class MyClass
{
    public static readonly pair[] lookup;

    static MyClass()
    {
        lookup = new pair[7] { new pair(1, 2), new pair(2, 3), new pair(3, 4), new pair(4, 5), new pair(5, 6), new pair(6, 7), new pair(7, 8) };
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Using structures similar using classes, so you can assign value on definition

public struct Pair {public float x, y;}

public class MyClass
{
    public static readonly Pair[] _lookup = new Pair[]{ 
        new Pair(){x=1, y=2}, 
        new Pair(){x=1, y=2},
        new Pair(){x=1, y=2},
        new Pair(){x=1, y=2},
        new Pair(){x=1, y=2},
        new Pair(){x=1, y=2},
        new Pair(){x=1, y=2}
    };
}

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.