C# help!!! Convert to VB

S

si_owen

Hi folks,

I am looking to convert some C# code for a calendar control into VB. I
am unfamiliar with C# and have seen several free concersion addin tools
to transform the code, how ever I can not get these applications
(addins) to run successfully. And I was hoping that someone in this
forum may be able to help me translate the code from C# to VB.

The code can be found here:
http://www.codeproject.com/useritems/DateRangePicker.asp

CLASS 1

using System;

namespace CustomWebControls
{
[Serializable]
public struct DateRange
{
public static readonly DateRange EMPTY = new DateRange();

readonly DateTime from;
readonly DateTime to;


public DateRange(DateTime from, DateTime to)
{
this.from = from;
this.to = to;
}


public DateTime From
{
get { return from; }
}

public DateTime To
{
get { return to; }
}

public TimeSpan TimeSpan
{
get
{
return to - from;
}
}

public bool Contains(DateTime time)
{
return from <= time && time < to;
}

public DateRange Include(DateRange otherRange)
{
return Include(otherRange.From).Include(otherRange.To);
}

public DateRange Include(DateTime date)
{
if (date < from)
return new DateRange(date, to);
else if (date > to)
return new DateRange(from, date);
else
return this;
}

/// <summary>
/// Creates a one day (24 hr) long DateRange starting at
DateTime
/// </summary>
public static DateRange CreateDay(DateTime dateTime){
return new DateRange(dateTime, dateTime.AddDays(1));
}

#region operators and overrides
public override int GetHashCode()
{
return from.GetHashCode() + 29*to.GetHashCode();
}

public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj)) return true;
if (!(obj is DateRange)) return false;
DateRange dateRange = (DateRange) obj;
if (!Equals(from, dateRange.from)) return false;
if (!Equals(to, dateRange.to)) return false;
return true;
}


public static bool operator == (DateRange d1, DateRange d2)
{
return d1.Equals(d2);
}

public static bool operator !=(DateRange d1, DateRange d2)
{
return !d1.Equals(d2);
}
#endregion

}
}


CLASS 2



using System.ComponentModel;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomWebControls
{
/// <summary>
/// An extended Calendar that can select DateRanges as well as
Dates
/// </summary>
[DefaultProperty("Text")]
[ToolboxData("<{0}:DateRangePicker
runat=server></{0}:DateRangePicker>")]
public class DateRangePicker : Calendar
{
static readonly TableItemStyle defaultSelectedDateRangeStyle =
new TableItemStyle();


static DateRangePicker()
{
//initialise a default colour for
defaultSelectedDateRangeStyle
defaultSelectedDateRangeStyle.BackColor =
Color.LightSteelBlue;
}

TableItemStyle selectedDateRangeStyle =
defaultSelectedDateRangeStyle;

protected override void OnDayRender(TableCell cell, CalendarDay
day)
{
if (SelectedDateRange.Contains(day.Date))
{
cell.ApplyStyle(selectedDateRangeStyle);
}
}

protected override void OnSelectionChanged()
{
base.OnSelectionChanged();

bool emptyDateRange = SelectedDateRange == DateRange.EMPTY;
bool dateRangeAlreadyPicked =
SelectedDateRange.TimeSpan.TotalDays > 1;

if (emptyDateRange || dateRangeAlreadyPicked)
{
SelectedDateRange = DateRange.CreateDay(SelectedDate);
//save this date as the first date in our date range
}
else
{
SelectedDateRange =
SelectedDateRange.Include(DateRange.CreateDay(SelectedDate));
//set the end date in our date range
}
}

//DateRange gets stored in the viewstate since it's a property
that needs to persist across page requests.
[Browsable(false)]
public DateRange SelectedDateRange
{
get { return (DateRange)
(ViewState["SelectedDateRange"]??DateRange.EMPTY); }
set { ViewState["SelectedDateRange"] = value; }
}

//SelectedDateRangeStyle goes into a private variable since
this property is designed.
[Category("Styles")]
[Description("The Style that is aplied to cells within the
selected Date Range")]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[NotifyParentProperty(true)]
[PersistenceMode(PersistenceMode.InnerProperty)]
public TableItemStyle SelectedDateRangeStyle
{
get { return selectedDateRangeStyle; }
set { selectedDateRangeStyle = value; }
}
}
}


any help would be much appreceiated...

Simon
 
O

Olie

Do you actually need to convert it? I could do this for you but it
would be a waste of time if you are just going to simply use the code
without changing it. I would just compile the code into a dll and use
it straight from there without the need for conversion. You can quite
happily mix c# with vb, you can even mix it in the same project but I
would not recomend this.

If you still want it converted then I am happy to give you some
guidance although c# is very similar to vb now so you may find it
easier than you expect.
this
Me

public DateTime From
{
get { return from; }
}

Public ReadOnly Property From As DateTime
Get
Return from
End Get
End Property
readonly DateTime from;

Private from As DateTime
public struct DateRange
{
}

Public Structure DateRange

End Structure
public bool Contains(DateTime time)
{
return from <= time && time < to;
}

Public Function Contains(time As DateTime)
If from <= time And time < to Then
Return True
Else
Return False
End If
End Function

Hope this help!

si_owen said:
Hi folks,

I am looking to convert some C# code for a calendar control into VB. I
am unfamiliar with C# and have seen several free concersion addin tools
to transform the code, how ever I can not get these applications
(addins) to run successfully. And I was hoping that someone in this
forum may be able to help me translate the code from C# to VB.

The code can be found here:
http://www.codeproject.com/useritems/DateRangePicker.asp

CLASS 1

using System;

namespace CustomWebControls
{
[Serializable]
public struct DateRange
{
public static readonly DateRange EMPTY = new DateRange();

readonly DateTime from;
readonly DateTime to;


public DateRange(DateTime from, DateTime to)
{
this.from = from;
this.to = to;
}


public DateTime From
{
get { return from; }
}

public DateTime To
{
get { return to; }
}

public TimeSpan TimeSpan
{
get
{
return to - from;
}
}

public bool Contains(DateTime time)
{
return from <= time && time < to;
}

public DateRange Include(DateRange otherRange)
{
return Include(otherRange.From).Include(otherRange.To);
}

public DateRange Include(DateTime date)
{
if (date < from)
return new DateRange(date, to);
else if (date > to)
return new DateRange(from, date);
else
return this;
}

/// <summary>
/// Creates a one day (24 hr) long DateRange starting at
DateTime
/// </summary>
public static DateRange CreateDay(DateTime dateTime){
return new DateRange(dateTime, dateTime.AddDays(1));
}

#region operators and overrides
public override int GetHashCode()
{
return from.GetHashCode() + 29*to.GetHashCode();
}

public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj)) return true;
if (!(obj is DateRange)) return false;
DateRange dateRange = (DateRange) obj;
if (!Equals(from, dateRange.from)) return false;
if (!Equals(to, dateRange.to)) return false;
return true;
}


public static bool operator == (DateRange d1, DateRange d2)
{
return d1.Equals(d2);
}

public static bool operator !=(DateRange d1, DateRange d2)
{
return !d1.Equals(d2);
}
#endregion

}
}


CLASS 2



using System.ComponentModel;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomWebControls
{
/// <summary>
/// An extended Calendar that can select DateRanges as well as
Dates
/// </summary>
[DefaultProperty("Text")]
[ToolboxData("<{0}:DateRangePicker
runat=server></{0}:DateRangePicker>")]
public class DateRangePicker : Calendar
{
static readonly TableItemStyle defaultSelectedDateRangeStyle =
new TableItemStyle();


static DateRangePicker()
{
//initialise a default colour for
defaultSelectedDateRangeStyle
defaultSelectedDateRangeStyle.BackColor =
Color.LightSteelBlue;
}

TableItemStyle selectedDateRangeStyle =
defaultSelectedDateRangeStyle;

protected override void OnDayRender(TableCell cell, CalendarDay
day)
{
if (SelectedDateRange.Contains(day.Date))
{
cell.ApplyStyle(selectedDateRangeStyle);
}
}

protected override void OnSelectionChanged()
{
base.OnSelectionChanged();

bool emptyDateRange = SelectedDateRange == DateRange.EMPTY;
bool dateRangeAlreadyPicked =
SelectedDateRange.TimeSpan.TotalDays > 1;

if (emptyDateRange || dateRangeAlreadyPicked)
{
SelectedDateRange = DateRange.CreateDay(SelectedDate);
//save this date as the first date in our date range
}
else
{
SelectedDateRange =
SelectedDateRange.Include(DateRange.CreateDay(SelectedDate));
//set the end date in our date range
}
}

//DateRange gets stored in the viewstate since it's a property
that needs to persist across page requests.
[Browsable(false)]
public DateRange SelectedDateRange
{
get { return (DateRange)
(ViewState["SelectedDateRange"]??DateRange.EMPTY); }
set { ViewState["SelectedDateRange"] = value; }
}

//SelectedDateRangeStyle goes into a private variable since
this property is designed.
[Category("Styles")]
[Description("The Style that is aplied to cells within the
selected Date Range")]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[NotifyParentProperty(true)]
[PersistenceMode(PersistenceMode.InnerProperty)]
public TableItemStyle SelectedDateRangeStyle
{
get { return selectedDateRangeStyle; }
set { selectedDateRangeStyle = value; }
}
}
}


any help would be much appreceiated...

Simon
 
G

Guest

Firstly, I would say that it's probably a good idea to be conversant in both
C# and VB.NET.

Failing that, download Lutz Roeder's Reflector (free) and load your compiled
C# assembly into it. You can then decompile it into the language of your
choice. Denis Bauer wrote a nice plug in for it that will actually create a
VB.NET project and all the source files automatically.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




si_owen said:
Hi folks,

I am looking to convert some C# code for a calendar control into VB. I
am unfamiliar with C# and have seen several free concersion addin tools
to transform the code, how ever I can not get these applications
(addins) to run successfully. And I was hoping that someone in this
forum may be able to help me translate the code from C# to VB.

The code can be found here:
http://www.codeproject.com/useritems/DateRangePicker.asp

CLASS 1

using System;

namespace CustomWebControls
{
[Serializable]
public struct DateRange
{
public static readonly DateRange EMPTY = new DateRange();

readonly DateTime from;
readonly DateTime to;


public DateRange(DateTime from, DateTime to)
{
this.from = from;
this.to = to;
}


public DateTime From
{
get { return from; }
}

public DateTime To
{
get { return to; }
}

public TimeSpan TimeSpan
{
get
{
return to - from;
}
}

public bool Contains(DateTime time)
{
return from <= time && time < to;
}

public DateRange Include(DateRange otherRange)
{
return Include(otherRange.From).Include(otherRange.To);
}

public DateRange Include(DateTime date)
{
if (date < from)
return new DateRange(date, to);
else if (date > to)
return new DateRange(from, date);
else
return this;
}

/// <summary>
/// Creates a one day (24 hr) long DateRange starting at
DateTime
/// </summary>
public static DateRange CreateDay(DateTime dateTime){
return new DateRange(dateTime, dateTime.AddDays(1));
}

#region operators and overrides
public override int GetHashCode()
{
return from.GetHashCode() + 29*to.GetHashCode();
}

public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj)) return true;
if (!(obj is DateRange)) return false;
DateRange dateRange = (DateRange) obj;
if (!Equals(from, dateRange.from)) return false;
if (!Equals(to, dateRange.to)) return false;
return true;
}


public static bool operator == (DateRange d1, DateRange d2)
{
return d1.Equals(d2);
}

public static bool operator !=(DateRange d1, DateRange d2)
{
return !d1.Equals(d2);
}
#endregion

}
}


CLASS 2



using System.ComponentModel;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomWebControls
{
/// <summary>
/// An extended Calendar that can select DateRanges as well as
Dates
/// </summary>
[DefaultProperty("Text")]
[ToolboxData("<{0}:DateRangePicker
runat=server></{0}:DateRangePicker>")]
public class DateRangePicker : Calendar
{
static readonly TableItemStyle defaultSelectedDateRangeStyle =
new TableItemStyle();


static DateRangePicker()
{
//initialise a default colour for
defaultSelectedDateRangeStyle
defaultSelectedDateRangeStyle.BackColor =
Color.LightSteelBlue;
}

TableItemStyle selectedDateRangeStyle =
defaultSelectedDateRangeStyle;

protected override void OnDayRender(TableCell cell, CalendarDay
day)
{
if (SelectedDateRange.Contains(day.Date))
{
cell.ApplyStyle(selectedDateRangeStyle);
}
}

protected override void OnSelectionChanged()
{
base.OnSelectionChanged();

bool emptyDateRange = SelectedDateRange == DateRange.EMPTY;
bool dateRangeAlreadyPicked =
SelectedDateRange.TimeSpan.TotalDays > 1;

if (emptyDateRange || dateRangeAlreadyPicked)
{
SelectedDateRange = DateRange.CreateDay(SelectedDate);
//save this date as the first date in our date range
}
else
{
SelectedDateRange =
SelectedDateRange.Include(DateRange.CreateDay(SelectedDate));
//set the end date in our date range
}
}

//DateRange gets stored in the viewstate since it's a property
that needs to persist across page requests.
[Browsable(false)]
public DateRange SelectedDateRange
{
get { return (DateRange)
(ViewState["SelectedDateRange"]??DateRange.EMPTY); }
set { ViewState["SelectedDateRange"] = value; }
}

//SelectedDateRangeStyle goes into a private variable since
this property is designed.
[Category("Styles")]
[Description("The Style that is aplied to cells within the
selected Date Range")]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[NotifyParentProperty(true)]
[PersistenceMode(PersistenceMode.InnerProperty)]
public TableItemStyle SelectedDateRangeStyle
{
get { return selectedDateRangeStyle; }
set { selectedDateRangeStyle = value; }
}
}
}


any help would be much appreceiated...

Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top