Solved

Code Transfer

Posted on 2006-06-27
4
214 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 250 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

680 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