Click here to Skip to main content
15,389,177 members
Home / Discussions / C#
   

C#

 
AnswerRe: bind a gridview and a listbox to a database Pin
Gerry Schmitz5-Jan-22 5:26
mveGerry Schmitz5-Jan-22 5:26 
AnswerRe: bind a gridview and a listbox to a database Pin
RobertSF7-Jan-22 10:47
professionalRobertSF7-Jan-22 10:47 
GeneralRe: bind a gridview and a listbox to a database Pin
steven_noppe11-Jan-22 22:31
Membersteven_noppe11-Jan-22 22:31 
GeneralRe: bind a gridview and a listbox to a database Pin
RobertSF12-Jan-22 12:37
professionalRobertSF12-Jan-22 12:37 
GeneralRe: bind a gridview and a listbox to a database Pin
RobertSF12-Jan-22 13:33
professionalRobertSF12-Jan-22 13:33 
QuestionIs there a way to improve this code? Pin
Richard A Knox4-Jan-22 8:06
MemberRichard A Knox4-Jan-22 8:06 
AnswerRe: Is there a way to improve this code? Pin
Gerry Schmitz4-Jan-22 12:25
mveGerry Schmitz4-Jan-22 12:25 
AnswerRe: Is there a way to improve this code? Pin
Richard Deeming4-Jan-22 22:20
mveRichard Deeming4-Jan-22 22:20 
I'd start with something like this:
C#
private static DateTime NextWeekDay(DateTime date, string expectedDayOfWeek, DateTimeFormatInfo dateFormat)
{
    int expectedDayIndex = Array.IndexOf(dateFormat.AbbreviatedDayNames, expectedDayOfWeek);
    if (expectedDayIndex != -1)
    {
        int dayOfWeek = (int)date.DayOfWeek;
        if (dayOfWeek != expectedDayIndex)
        {
            int daysToAdd = expectedDayIndex - dayOfWeek;
            if (daysToAdd < 0) daysToAdd += 7;
            date = date.AddDays(daysToAdd);
        }
    }
    
    return date;
}
C#
DateTime today = DateTime.Today;
DataTable dt = ds.Tables[0];
DateTimeFormatInfo dateFormat = DateTimeFormatInfo.GetInstance(CultureInfo.CurrentCulture);

foreach (DataRow row in dt.Rows)
{
    string budgetType = row[0].ToString();
    switch (budgetType)
    {
        ...
        case "Weekly (52)":
        {
            DateTime date = today.AddDays(1 - today.Day);
            
            string expectedDayOfWeek = row[1].ToString();
            date = NextWeekDay(date, expectedDayOfWeek, dateFormat);
            
            int max = (4 * f);
            for (int n = 0; n < max; n++)
            {
                list.Add(date);
                date = date.AddDays(7);
            }
            break;
        }
        ...
    }
}
Further optimisation may be possible depending on what the rest of the code looks like.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

AnswerRe: Is there a way to improve this code? Pin
Pete O'Hanlon4-Jan-22 22:49
mvaPete O'Hanlon4-Jan-22 22:49 
QuestionCSV File Access - Error Msg : the process cannot access the file used by another process Pin
The Golden Man4-Jan-22 7:26
MemberThe Golden Man4-Jan-22 7:26 
AnswerRe: CSV File Access - Error Msg : the process cannot access the file used by another process Pin
Gerry Schmitz4-Jan-22 12:16
mveGerry Schmitz4-Jan-22 12:16 
QuestionAbout records Pin
Super Lloyd3-Jan-22 9:12
MemberSuper Lloyd3-Jan-22 9:12 
AnswerRe: About records Pin
OriginalGriff3-Jan-22 20:28
mveOriginalGriff3-Jan-22 20:28 
GeneralRe: About records Pin
Super Lloyd3-Jan-22 20:31
MemberSuper Lloyd3-Jan-22 20:31 
GeneralRe: About records Pin
Super Lloyd3-Jan-22 20:33
MemberSuper Lloyd3-Jan-22 20:33 
GeneralRe: About records Pin
Super Lloyd3-Jan-22 21:07
MemberSuper Lloyd3-Jan-22 21:07 
GeneralRe: About records Pin
BillWoodruff4-Jan-22 4:49
mveBillWoodruff4-Jan-22 4:49 
AnswerRe: About records Pin
Richard Deeming3-Jan-22 23:14
mveRichard Deeming3-Jan-22 23:14 
GeneralRe: About records Pin
Super Lloyd3-Jan-22 23:17
MemberSuper Lloyd3-Jan-22 23:17 
AnswerRe: About records Pin
BillWoodruff4-Jan-22 5:38
mveBillWoodruff4-Jan-22 5:38 
GeneralRe: About records Pin
Super Lloyd4-Jan-22 5:49
MemberSuper Lloyd4-Jan-22 5:49 
AnswerRe: About records Pin
endo funk7-Jan-22 1:55
Memberendo funk7-Jan-22 1:55 
GeneralRe: About records Pin
Super Lloyd7-Jan-22 8:49
MemberSuper Lloyd7-Jan-22 8:49 
GeneralRe: About records Pin
endo funk7-Jan-22 10:39
Memberendo funk7-Jan-22 10:39 
QuestionBlazor: IEnumerable ElementAt Pin
chrisb15162-Jan-22 23:49
Memberchrisb15162-Jan-22 23:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.