C# help!!!

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
 
R

rowe_newsgroups

Why not just compile this into a class library and add it to your app?

Also here's a great site for converting c# to vb and vice-versa:

http://www.harding.edu/USER/fmccown/WWW/vbnet_csharp_comparison.html

If that doesn't work then the converted code is below

Thanks,

Seth Rowe

' Watch for typos - I typed in a hurry and didn't test anything

Imports System

Namespace CustomWebControls

<Serializable()> _
Public Structure DateRange
Public Shared ReadOnly EMPTY As DateRange = New DateRange
Private ReadOnly m_from As DateTime
Private ReadOnly m_to As DateTime

Public Sub New(ByVal from As DateTime, ByVal [to] As DateTime)
Me.m_from = from
Me.m_to = [to]
End Sub

Public ReadOnly Property From() As DateTime
Get
Return m_from
End Get
End Property

Public ReadOnly Property [To]() As DateTime
Get
Return m_to
End Get
End Property

Public ReadOnly Property TimeSpan() As TimeSpan
Get
Return m_to - m_from
End Get
End Property

Public Function Contains(ByVal time As DateTime) As Boolean
Return (From <= time AndAlso time < [To])
End Function

Public Function Include(ByVal otherRange As DateRange) As
DateRange
Return Include(otherRange.From).Include(otherRange.To)
End Function

Public Function Include(ByVal [date] As DateTime) As DateRange
If ([date] < From) Then
Return New DateRange([date], [m_to])
ElseIf ([date] > [To]) Then
Return New DateRange(From, [date])
Else
Return Me
End If
End Function

''' <summary>
''' Creates a one day (24 hr) long DateRange starting at
DateTime
''' </summary>
Public Shared Function CreateDay(ByVal dateTime As DateTime) As
DateRange
Return New DateRange(dateTime, dateTime.AddDays(1))
End Function

#Region "operators and overrides"
Public Overrides Function GetHashCode() As Int32
Return m_from.GetHashCode() + 29 * m_to.GetHashCode()
End Function

Public Overrides Function Equals(ByVal obj As Object) As
Boolean
If ReferenceEquals(Me, obj) Then Return True
If Not TypeOf (obj) Is DateRange Then Return False
Dim dateRange As DateRange = DirectCast(obj, DateRange)
If Not Equals(m_from, dateRange.m_from) Then Return False
If Not Equals(m_to, dateRange.m_to) Then Return False
Return True
End Function

Public Shared Operator =(ByVal d1 As DateRange, ByVal d2 As
DateRange) As Boolean
Return d1.Equals(d2)
End Operator

Public Shared Operator <>(ByVal d1 As DateRange, ByVal d2 As
DateRange) As Boolean
Return Not d1.Equals(d2)
End Operator

#End Region

End Structure

End Namespace

Imports System.ComponentModel
Imports System.Drawing
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace CustomWebControls

<DefaultProperty("Text"), ToolboxData("<{0}:DateRangePicker
runat=server></{0}:DateRangePicker>")> _
Public Class DateRangePicker
Inherits Calendar

Shared ReadOnly defaultSelectedDateRangeStyle As TableItemStyle
= New TableItemStyle()

Shared Sub DateRangePicker()
'initialise a default colour for
defaultSelectedDateRangeStyle
defaultSelectedDateRangeStyle.BackColor =
Color.LightSteelBlue
End Sub

Dim m_selectedDateRangeStyle As TableItemStyle =
defaultSelectedDateRangeStyle

Protected Overrides Sub OnDayRender(ByVal cell As
System.Web.UI.WebControls.TableCell, ByVal day As
System.Web.UI.WebControls.CalendarDay)
If (SelectedDateRange.Contains(day.Date)) Then
cell.ApplyStyle(m_selectedDateRangeStyle)
End If
End Sub

Protected Overrides Sub OnSelectionChanged()
MyBase.OnSelectionChanged()

Dim emptyDateRange As Boolean = (SelectedDateRange =
DateRange.EMPTY)
Dim dateRangeAlreadyPicked As Boolean =
SelectedDateRange.TimeSpan.TotalDays > 1

If emptyDateRange OrElse dateRangeAlreadyPicked Then
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
End If
End Sub

<Browsable(False)> _
Public Property SelectedDateRange() As DateRange
Get
If Not ViewState("SelectedDateRange") Is Nothing Then
Return DirectCast(ViewState("SelectedDateRange"),
DateRange)
Else
Return DateRange.EMPTY
End If
End Get
Set(ByVal value As DateRange)
ViewState("SelectedDateRange") = value
End Set
End Property

'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 Property SelectedDateRangeStyle() As TableItemStyle
Get
Return m_selectedDateRangeStyle
End Get
Set(ByVal value As TableItemStyle)
m_selectedDateRangeStyle = value
End Set
End Property

End Class
End Namespace


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

The following is from Instant VB. Let me know how it works for you:

Imports System

Namespace CustomWebControls
<Serializable> _
Public Structure DateRange
Public Shared ReadOnly EMPTY As DateRange

Private ReadOnly from_Renamed As DateTime
Private ReadOnly [to] As DateTime


Public Sub New(ByVal from_Renamed As DateTime, ByVal [to] As DateTime)
Me.from_Renamed = from_Renamed
Me.to = [to]
End Sub


Public ReadOnly Property From() As DateTime
Get
Return from_Renamed
End Get
End Property

Public ReadOnly Property [To]() As DateTime
Get
Return [to]
End Get
End Property

Public ReadOnly Property TimeSpan() As TimeSpan
Get
Return [to] - from_Renamed
End Get
End Property

Public Function Contains(ByVal time As DateTime) As Boolean
Return from_Renamed <= time AndAlso time < [to]
End Function

Public Function Include(ByVal otherRange As DateRange) As DateRange
Return Include(otherRange.From).Include(otherRange.To)
End Function

Public Function Include(ByVal [date] As DateTime) As DateRange
If [date] < from_Renamed Then
Return New DateRange([date], [to])
ElseIf [date] > [to] Then
Return New DateRange(from_Renamed, [date])
Else
Return Me
End If
End Function

''' <summary>
''' Creates a one day (24 hr) long DateRange starting at DateTime
''' </summary>
Public Shared Function CreateDay(ByVal dateTime As DateTime) As DateRange
Return New DateRange(dateTime, dateTime.AddDays(1))
End Function

#Region "operators and overrides"
Public Overrides Function GetHashCode() As Integer
Return from_Renamed.GetHashCode() + 29*[to].GetHashCode()
End Function

Public Overrides Overloads Function Equals(ByVal obj As Object) As Boolean
If ReferenceEquals(Me, obj) Then
Return True
End If
If Not(TypeOf obj Is DateRange) Then
Return False
End If
Dim dateRange As DateRange = CType(obj, Me.DateRange)
If (Not Equals(from_Renamed, dateRange.from)) Then
Return False
End If
If (Not Equals([to], dateRange.to)) Then
Return False
End If
Return True
End Function


Public Shared Operator =(ByVal d1 As DateRange, ByVal d2 As DateRange) As
Boolean
Return d1.Equals(d2)
End Operator

Public Shared Operator <>(ByVal d1 As DateRange, ByVal d2 As DateRange) As
Boolean
Return Not d1.Equals(d2)
End Operator
#End Region

Shared Sub New()
EMPTY = New DateRange()
End Sub
End Structure
End Namespace

'CLASS 2

Imports System.ComponentModel
Imports System.Drawing
Imports System.Web.UI
Imports 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
Inherits Calendar
Private Shared ReadOnly defaultSelectedDateRangeStyle As TableItemStyle =
New TableItemStyle()

Shared Sub New()
'initialise a default colour for defaultSelectedDateRangeStyle
defaultSelectedDateRangeStyle.BackColor = Color.LightSteelBlue
End Sub

Private selectedDateRangeStyle_Renamed As TableItemStyle =
defaultSelectedDateRangeStyle

Protected Overrides Sub OnDayRender(ByVal cell As TableCell, ByVal day As
CalendarDay)
If SelectedDateRange.Contains(day.Date) Then
cell.ApplyStyle(selectedDateRangeStyle_Renamed)
End If
End Sub

Protected Overrides Sub OnSelectionChanged()
MyBase.OnSelectionChanged()

Dim emptyDateRange As Boolean = SelectedDateRange = DateRange.EMPTY
Dim dateRangeAlreadyPicked As Boolean =
SelectedDateRange.TimeSpan.TotalDays > 1

If emptyDateRange OrElse dateRangeAlreadyPicked Then
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
End If
End Sub

'DateRange gets stored in the viewstate since it's a property that needs
to persist across page requests.
<Browsable(False)> _
Public Property SelectedDateRange() As DateRange
Get
If (Not (ViewState("SelectedDateRange")) Is Nothing) Then
Return CType(ViewState("SelectedDateRange"), DateRange)
Else
Return CType(DateRange.EMPTY, DateRange)
End If
End Get
Set
ViewState("SelectedDateRange") = Value
End Set
End Property

'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 Property SelectedDateRangeStyle() As TableItemStyle
Get
Return selectedDateRangeStyle_Renamed
End Get
Set
selectedDateRangeStyle_Renamed = Value
End Set
End Property
End Class
End Namespace

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter


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
 
S

si_owen

Hi David,

Yeah its worked great thanks very much.

Very 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