Initialization function


#1

Hello everybody,
I am going to consider an object to be declared and initialized. Let us assume there are no other properties, that could lead to create a class.
In such case, I have got to declare the object in the class using it, and initialize it in the constructor, maybe via a property.
In this context I shall consider the main() function as a constructor of Program. Oh well, I see that now, by default, the class is named Solution ?
Let us take an example, to come back to the purpose :


class Solution
{
  static List<int> listTest;
  static void Main(string[] args)
  {
	listTest = new List<int>();
  }
}

and I am not sure this is specific to C#.

Well now, this is why I opened this thread. I wanted to declare a Dictionary<string, int> that way, and an error message alerted me that the object had to be implemented.

So, I had to do it this way :


class Solution
{

	static Dictionary<string, int> DictOri = new Dictionary<string, int>();
	static Dictionary<int, string> DictNb = new Dictionary<int, string>();
}

In the main(), I can directly use DictOri.Add(), the dictionary is already implemented.
Did you already notice any other object with such particularities ?

I have another question concerning dictionaries, perhaps I should create another thread ?
I need to associate E to 90, S to 180, and then back 90 to E, 180 to S. In such case I create two dictionaries, as in the example I gave.
So I shall have to create a function for the creation of elements, this is why I created the dictionaries at the class level, and not in the main :


static void IniDict(string s, int nb)
{
	DictOri.Add(s, nb);
        DictNb.Add(nb, s);
}

So, now the question : is there any functionality of the dictionaries I missed, that would allow all this with only one dictionary ?

Well, at a moment I clicked somewhere, I am no more able to say on what, and it made this appear :
YesNo
Did I do a mess ?
A tooltip on each button would probably have avoided me to ask the question.
Oh sorry, got it : I wanted to modify my message, looked for the pencil button. I found one at the top of the page, but this was to modify the title. So, these buttons I copied were to validate or cancel the modification of the title.
To modify the message it is better to use the pencil down the message.


#2

// coz not inited
static List listTest = null; // assign something

// exchange 2 dictionaries to 2 functions
// 0deg=Right/East, 90deg=Up/North, 180deg=Left/West, 270deg=Down/South
static readonly string[] DIRs = { “E”, “NE”, “N”, “NW”, “W”, “SW”, “S”, “SE” };
static string toDir( int deg ) => DIRs[ (deg % 360 + 360) % 360 / 45 ]; // negative deg too
static int toDeg( string dir ) { var res = Array.IndexOf( DIRs, dir ); return res >= 0 ? res * 45 : throw new Exception( “Wrong dir” ); }


#3

static List listTest = null; // assign something


OK, received.

/ exchange 2 dictionaries to 2 functions
// 0deg=Right/East, 90deg=Up/North, 180deg=Left/West, 270deg=Down/South
static readonly string[] DIRs = { “E”, “NE”, “N”, “NW”, “W”, “SW”, “S”, “SE” };
static string toDir( int deg ) => DIRs[ (deg % 360 + 360) % 360 / 45 ]; // negative deg too
static int toDeg( string dir ) { var res = Array.IndexOf( DIRs, dir ); return res >= 0 ? res * 45 : throw new Exception( “Wrong dir” ); }


Hum, I imagine I have to test that …
Thank you.


#4

Not sure I could do a better choice as two Dictionary, but the elements to determine this are explained there :
http://geekswithblogs.net/BlackRabbitCoder/archive/2011/06/16/c.net-fundamentals-choosing-the-right-collection-class.aspx