Solved

Code Transfer

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB: Convert 2 dates to specific format 24 52
HTML - Color not displaying correctly in EMAIL. 6 49
C# Gridview 1 48
linq, c# 8 23
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

838 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