Solved

Code Transfer

Posted on 2006-06-27
4
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Suggested Courses

738 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