Is there a C # equivalent of the VB6 Select () function?

Is there a C # equivalent of the VB6 Select () function?

day = Choose(month,31,28,30) 
+4
source share
5 answers

Not really. You can, of course, create an array and use its indexed getter:

 day = new[] { 31, 28, 30 }[month]; 

Alternatively, you could - I would not - import the Microsoft.VisualBasic namespace and do:

 day = Interaction.Choose(month, 31, 28, 30); 

I don’t know how simplified your example is, but in case you are really looking for a way to find the number of days in a particular month, try DateTime.DaysInMonth() :

 day = DateTime.DaysInMonth(2008, 2); // day == 29 
+10
source

If it is really about days a month, I will follow the advice that others have given. However, if you really need a selection function, you can easily create it yourself. For example, for example:

 public static T Choose<T>(int index, params T[] args) { if (index < 1 || index > args.Length) { return default(T); } else { return args[--index]; } } // call it like this var day = Choose<int?>(1, 30, 28, 29); // returns 30 

I did not bother to make the first argument double, but it is easy to do. It is also possible to make a non-standard version ...

+5
source

I will try, you use DateTime.DaysInMonth instead :)

+4
source

My first guess would be

 var days = new[] { 31, 28, 30 }[month]; 

Although the native version does all kinds of crazy things like rounding and level checking.

+1
source

The simple answer is no.

If you want to do only what you do, try DateTime.DaysInMonth (year, month)

+1
source

Source: https://habr.com/ru/post/1304366/


All Articles