0

What I would like to do is expose the contents of an array as properties of a class..

For example:

var x = myclass.array.entryA;
var y = myclass.array.entryB;

if i made a static class such as this

public static myclass
{
    public static array {
        get { 
            return myclass a = (myclass) arraycontents
        }
}

The purpose of this is to have a dictionary of text that i dont have to call with a function, i'd like to know how to expose them as properties

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
magiva
  • 45
  • 6
  • 3
    What is the type of the array, i.e. what is the type of the content of the array? It *seems* like you expect the array to contain instances of `myclass`. But `myclass` is static and thus can't be instantiated. -- this feels like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What actual use case do you want to solve? – Corak Feb 15 '19 at 11:36
  • If you want a "dictionary of text", maybe you're looking for `Dictionary`? As in `var dict = new Dictionary(); dict["Greeting"] = "Hello"; dict["Audience"] = "World";` and then you can use it like `var x = dict["Greeting"]; var y = dict["Audience"];` then `x` would refer to `"Hello"` and `y` would refer to `"World"`. -- (Btw. a property is basically just a convenient way to write one or two (for `get` and `set`) "functions"). – Corak Feb 15 '19 at 11:47

1 Answers1

0

Do you need something like the below code?

EDIT Added properties FirstItem, SecondItem, ThirdItem to illustrate, how could you expose array contents - though to make it safe you need to ensure corresponding size of the array. Also it is questionable, if this is a convenient approach at all, to expose array via separate properties, especially for a large number of items?

public class ArrayPropertyClass
{
    private string[] _sampleArray;

    public string[] SampleArray
    {
        get { return _sampleArray; }
    }

    public string FirstItem
    {
        get { return _sampleArray[0]; }
    }

    public string SecondItem
    {
        get { return _sampleArray[1]; }
    }

    public string ThirdItem
    {
        get { return _sampleArray[2]; }
    }

    public ArrayPropertyClass(int arraySize)
    {
        _sampleArray = new string[arraySize];
    }
}    


static void Main(string[] args)
{

    ArrayPropertyClass apc = new ArrayPropertyClass(5);

    for (int i = 0; i < 5; ++i)
    {
        apc.SampleArray[i] = (i+1).ToString();
    }

    for (int i = 0; i < 5; ++i)
    {
        Console.WriteLine(apc.SampleArray[i]);
    }

    Console.WriteLine(apc.FirstItem);
    Console.WriteLine(apc.SecondItem);
    Console.WriteLine(apc.ThirdItem);
    Console.ReadLine();
}
Pavel
  • 103
  • 9
  • so... this came from some code i saw that made the session elements into properties https://stackoverflow.com/questions/621549/how-to-access-session-variables-from-any-class-in-asp-net I have a text that gets displayed in different languages which is referred to from a enum list Messages.Text( Messages.Type.Description) this returns the 'description' text in the correct language or correct wording, it holds texts in 1 place that can be changed easily I was experitmenting with if i could have the enum names as dynamic properties returning text instead of calling functions – magiva Feb 15 '19 at 13:32
  • i should note that the enum list is then used against an array but my focus here was how to take those enum names and consume them as properties instead of passing into a function. more of an education of how would i – magiva Feb 15 '19 at 13:36
  • @magiva - it's probably not impossible to do... but if it's done at runtime, all you can do while writing code is probably using `dynamic` and loosing much convenience (like intellisense). If the "properties" are related to an enum, i.e. values you know while writing code, you might be able to use [T4](https://learn.microsoft.com/visualstudio/modeling/code-generation-and-t4-text-templates) to auto-generate properties that can then be used as if you wrote them all by hand. – Corak Feb 15 '19 at 14:23
  • @magiva - to clarify your question: you would like to define `enum` and expect somehow to have several properties, each one corresponding to one enum value? – Pavel Feb 15 '19 at 18:00