Question regarding and array looping which includes bool,string?

Here is the code

I will get the current mouth into string Date_Picker_Month and trying to find the index of it and my plane is to find it and after the clicking of the next button the month should be change and should display the month name instead of simple number.
I tried for and if but it is either showing boll to string implicit or string to int conversion problem.
if anybody have idea regarding this so please share it.

Thanks in advance.

Okay, let’s start over.

You have an array of months, the index for January will be 0, the index for December will be 11.

The user will enter a month? And will enter the number representation? So 01 (January), 02, 03, 04, to 12 (December)?

Am I close?

If so, you don’t need any loops. Well regardless, you will never need any loops.

// you don't need an array of month names
string selectedMonth = Date_Picker_Mounth;
int numericalMonth = -1;
Int32.TryParse(selectedMonth, out numericalMonth); // converts the month to an int

// Read the Year
string selectedYear = Date_Picker_Year;
int numericalYear = -1;
Int32.TryParse(selectedYear, out numericalYear); // converts the year to an int

// Setup a DateTime object
DateTime currentMonthYear = new DateTime(numericalYear, numericalMonth, 01);

// Add a month
DatePickerMounth.Text = currentMonthYear.ToString("MMMM, yyyy"); // will display as month, year

Now if Date_Picker_Mounth or Date_Picker_Year contain non-numerical numbers, this will blow up. As it will try to establish a DateTime object using -1 for the Year or Month, which obviously isn’t right, so be sure to validate your inputs.

Actually my code won’t work by any user input. i am just trying to create a date picker for my project since the calendar is very limited to created a date picker. I know i can have a plugin but i am more interested to use my own code by the way i am new to c# so i am unable to understand some line of code which are

Int32.TryParse(selectedMonth, out numericalMonth);
DateTime currentMonthYear = new DateTime(numericalYear, numericalMonth, 01);

And thanks for trying to help.

The Int32.TryParse(selectedMonth, out numericalMonth) takes the value of selectedMonth (a string) and tries to convert it to an integer. The value gets stored into the variable numericalMonth.

DateTime currentMonthYear = new DateTime(numericalYear, numericalMonth, 01); is establishing a DateTime object in .NET, it makes doing things like adding a month, adding days, years, really really easy. It has multiple constructors, but the one I utilized accepts a year, month, and day. Since you only care about Year and Month, I hard coded day to 01.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.