Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Code Transfer

Posted on 2006-06-27
4
Medium Priority
?
219 Views
Last Modified: 2010-04-16
Hello Experts,

Could anyone pls tell me how I could convert the following VB code to C#


VB.NET:
         Dim astrHolidays(12, 31) As String
         astrHolidays(1, 1) = "New Year's Day"
        astrHolidays(2, 2) = "Groundhog Day"
        astrHolidays(2, 14) = "St. Valentine's Day"
        astrHolidays(3, 17) = "St. Patrick's Day"
        astrHolidays(4, 1) = "April Fool's Day"
        astrHolidays(5, 1) = "May Day"
        astrHolidays(7, 4) = "Independence Day"
        astrHolidays(10, 31) = "Halloween"
        astrHolidays(12, 25) = "Christmas Day"

 Private Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender

  Dim strHol As String = astrHolidays(e.Day.Date.Month, e.Day.Date.Day)
        If strHol <> String.Empty Then
            e.Cell.BackColor = Color.Beige
            e.Cell.ForeColor = Color.Black
            e.Cell.Font.Bold = True
        End If
End Sub

MY C# ATTEMPT AND IT DOESNT SEEMS TO WORK:

                        public string[,] astrHolidays = new string[13,32];
                        astrHolidays[1, 1] = "New Year's Day";
               astrHolidays[2, 2] = "Groundhog Day";
               astrHolidays[2, 14] = "St. Valentine's Day";
               astrHolidays[3, 17] = "St. Patrick's Day";
               astrHolidays[4, 1] = "April Fool's Day";
               astrHolidays[5, 1] = "May Day";
               astrHolidays[7, 4] = "Independence Day";
               astrHolidays[10, 31] = "Halloween";
               astrHolidays[12, 25] = "Christmas Day";

private void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
{
                        string strHol = this.astrHolidays[e.Day.Date.Month,e.Day.Date.Day];
               if (strHol!=string.Empty)
               {
                    e.Cell.BackColor = Color.Beige;
                    e.Cell.ForeColor = Color.Black;
                    e.Cell.Font.Bold = true;
               }
}

Any help would be appreciated.
0
Comment
Question by:radhakrishan
4 Comments
 
LVL 12

Expert Comment

by:Proactivation
ID: 16991085
You can use this site to convert your code:

http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx
0
 
LVL 3

Expert Comment

by:boethius78
ID: 16991136
Try this:

public string[,] astrHolidays;

static Calendar1()
{
               astrHolidays = new string[13,32];
               astrHolidays[1, 1] = "New Year's Day";
               astrHolidays[2, 2] = "Groundhog Day";
               astrHolidays[2, 14] = "St. Valentine's Day";
               astrHolidays[3, 17] = "St. Patrick's Day";
               astrHolidays[4, 1] = "April Fool's Day";
               astrHolidays[5, 1] = "May Day";
               astrHolidays[7, 4] = "Independence Day";
               astrHolidays[10, 31] = "Halloween";
               astrHolidays[12, 25] = "Christmas Day";
}


Bear in mind that all the other strings will initialise to null, so using 'if( strHol != null && strHol.Length > 0 )' might be better than 'if( strHol != String.Empty )'.  I can't see any problems with the DayRender method.
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 1000 total points
ID: 16991195
Your algorithm is not very efficient (in VB OR C#) you are creating a 13*32 item array which allocates 416 elements.  Then you initialize only 9 elements.  This is a better implementation (also encapsilated in a class):

class Holidays
{
  private static Hashtable astrHolidays=new Hashtable();
  static Holidays()
  {
     astrHolidays["1, 1"] = "New Year's Day";
     astrHolidays["2, 2"] = "Groundhog Day";
     astrHolidays["2, 14"] = "St. Valentine's Day";
     astrHolidays["3, 17"] = "St. Patrick's Day";
     astrHolidays["4, 1"] = "April Fool's Day";
     astrHolidays["5, 1"] = "May Day";
     astrHolidays["7, 4"] = "Independence Day";
     astrHolidays["10, 31"] = "Halloween";
     astrHolidays["12, 25"] = "Christmas Day";
 }
 public static string GetHoliday(int Month, int day)
 {
    string hol=astrHolidays[month.ToString()+", "+day.ToString()] as string;
    if (hol==null)  return "";
    else return hol;
  }
}

Now your method would be:
private void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
{
     string strHol = Holidays.GetHoliday(e.Day.Date.Month,e.Day.Date.Day);
     if (strHol!=string.Empty)
     {
       e.Cell.BackColor = Color.Beige;
       e.Cell.ForeColor = Color.Black;
       e.Cell.Font.Bold = true;
     }
}
0
 

Author Comment

by:radhakrishan
ID: 16991848
Hi MogalManic,

Thanks for the reply.
It is surely much more efficient then wat i been doing.

(but thanks to boethius78 for the initial help)

Cheers.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month7 days, 4 hours left to enroll

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question