Solved

Code Transfer

Posted on 2006-06-27
4
210 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
Comment Utility
You can use this site to convert your code:

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

Expert Comment

by:boethius78
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now