小弟初学c#和asp.net编程,遇到一个问题,请教怎么解决!在学习中,我看到一个用VB+Calendar控件编写的一个小小的备忘录,源代码如下:<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Runtime.Serialization" %>
<%@ Import Namespace="System.Runtime.Serialization.Formatters.Binary" %>
<%@ Import Namespace="System.Drawing" %><Script Runat="Server">Dim arrCalendar( 13, 32 ) As String' Get schedule from cache or disk
Sub Page_Load
Dim strmFileStream As FileStream
Dim fmtrBinaryFormatter As BinaryFormatter If Cache( "arrCalendar" ) Is Nothing Then
If File.Exists( "c:\schedule.bin" ) Then
strmFileStream = _
New FileStream( "c:\schedule.bin", FileMode.Open )
fmtrBinaryFormatter = New BinaryFormatter
arrCalendar = _
CType( fmtrBinaryFormatter.Deserialize( strmFileStream ), Array )
strmFileStream.Close()
Cache( "arrCalendar" ) = arrCalendar
End If
Else
arrCalendar = Cache( "arrCalendar" )
End If
End Sub' Save schedule to file
Sub btnSave_Click( s As Object, e As EventArgs )
Dim dtmDate As DateTime
Dim strmFileStream As FileStream
Dim fmtrBinaryFormatter As BinaryFormatter dtmDate = calSchedule.SelectedDate
arrCalendar( dtmDate.Month, dtmDate.Day ) = txtNotes.Text
strmFileStream = _
New FileStream( "c:\schedule.bin", FileMode.Create )
fmtrBinaryFormatter = New BinaryFormatter
fmtrBinaryFormatter.Serialize( strmFileStream, arrCalendar )
strmFileStream.Close()
Cache( "arrCalendar" ) = arrCalendar
End Sub' Pick a date
Sub Calendar_SelectionChanged( s As Object, e As EventArgs )
Dim dtmDate As DateTime dtmDate = calSchedule.SelectedDate
txtNotes.Text = arrCalendar( dtmDate.Month, dtmDate.Day )
End Sub' Display each calendar day
Sub Calendar_RenderDay( s As Object, e As DayRenderEventArgs )
Dim dtmDate As DateTime
Dim ctlCell As TableCell dtmDate = e.Day.Date
ctlCell = e.Cell
If arrCalendar( dtmDate.Month, dtmDate.Day ) <> "" Then
ctlCell.BackColor = Color.FromName( "Orange" )
End If
End Sub</Script><html>
<head><title>CalendarSchedule.aspx</title></head>
<body><form Runat="Server"><asp:Calendar
id="calSchedule"
Width="100%"
ShowGridLines="True"
OnSelectionChanged="Calendar_SelectionChanged"
OnDayRender="Calendar_RenderDay"
Runat="Server" /><p>
<asp:TextBox
id="txtNotes"
TextMode="MultiLIne"
Columns="50"
Rows="10"
Runat="Server" /><p>
<asp:Button
id="btnSave"
Text="Save Changes"
OnClick="btnSave_Click"
Runat="Server" /></form>
</body>
</html> 以上代码生成了一个13*32的string二维数组用来存储日历上每一天的备忘录,并写入
c:\schedule.bin文件中。如果某日备忘录中有内容,则日历上就会显示桔红色。备忘录的输入输出都在txtbox中完成。
我试图将其用c#来改写,遇到了很大的问题,以下是我改写的c#程序:<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Runtime.Serialization" %>
<%@ Import Namespace="System.Runtime.Serialization.Formatters.Binary" %>
<%@ Import Namespace="System.Drawing" %><Script Language="c#" Runat="Server">Object[,] arrCalendar = new Object[13,32];// Get schedule from cache or disk
void Page_Load() {
FileStream strmFileStream = new FileStream();
BinaryFormatter fmtrBinaryFormatter = new BinaryFormatter(); if (Cache["arrCalendar"] == Null)
if File.Exists( "c:\schedule.bin" ) {
strmFileStream = FileStream( "c:\schedule.bin", FileMode.Open );
arrCalendar = Cype( fmtrBinaryFormatter.Deserialize( strmFileStream ), Array );
strmFileStream.Close();
Cache["arrCalendar"] = arrCalendar;
}
else
arrCalendar = Cache["arrCalendar"];
}// Save schedule to file
void btnSave_Click( Object s, EventArgs e ) {
DateTime dtmDate = new DateTime();
FileStream strmFileStream = new FileStream();
BinaryFormatter fmtrBinaryFormatter = new BinaryFormatter(); dtmDate = calSchedule.SelectedDate;
arrCalendar[dtmDate.Month, dtmDate.Day] = txtNotes.Text;
strmFileStream = FileStream( "c:\schedule.bin", FileMode.Create );
fmtrBinaryFormatter.Serialize( strmFileStream, arrCalendar );
strmFileStream.Close();
Cache["arrCalendar"] = arrCalendar;
}// Pick a date
void Calendar_SelectionChanged( Object s, EventArgs e ) {
DateTime dtmDate = new DateTime(); dtmDate = calSchedule.SelectedDate;
txtNotes.Text = arrCalendar[dtmDate.Month, dtmDate.Day].ToString();
}// Display each calendar day
void Calendar_RenderDay( Object s, DayRenderEventArgs e ) {
DateTime dtmDate = new DateTime;
Table ctlCell = new TableCell(); dtmDate = e.Day.Date;
ctlCell = e.Cell;
if (arrCalendar[dtmDate.Month, dtmDate.Day] != "" )
ctlCell.BackColor = Color.FromName( "Orange" );
}
}</Script><html>
<head><title>CalendarSchedule_.aspx</title></head>
<body><form Runat="Server"><asp:Calendar
id="calSchedule"
Width="100%"
ShowGridLines="True"
OnSelectionChanged="Calendar_SelectionChanged"
OnDayRender="Calendar_RenderDay"
Runat="Server" /><p>
<asp:TextBox
id="txtNotes"
TextMode="MultiLIne"
Columns="50"
Rows="10"
Runat="Server" /><p>
<asp:Button
id="btnSave"
Text="Save Changes"
OnClick="btnSave_Click"
Runat="Server" /></form>
</body>
</html>
这个程序的编译统不过,但是我不知道问题怎么解决。
问题一:
VB中检查Cashe中是否有arrcalendar数组存在的语句:
If Cache( "arrCalendar" ) Is Nothing Then
我改写成C#是否就是我程序中所写的:
if (Cache["arrCalendar"] == Null) ???
问题二:
VB中声明二维字符串数组:
Dim arrCalendar( 13, 32 ) As String
由于c#中string类不能只声明而不构造,所以我干脆使用:
Object[,] arrCalendar = new Object[13,32];
采用object类进行封箱操作。这样做不知道可以不可以???
问题三:
VB中的这一句:
arrCalendar = _
CType( fmtrBinaryFormatter.Deserialize( strmFileStream ), Array )
CType到底是个什么东西?完成什么功能?
我改写成c#也是使用了Ctype但是我使用VS.net的检索查到c#中没有CType,到底完成想通功能的c#程序怎么写?由于我对VB不懂,只能大概看懂程序,所以请教VB和c#高手帮忙看看!!!初来乍到,请多多指教!!
<%@ Import Namespace="System.Runtime.Serialization" %>
<%@ Import Namespace="System.Runtime.Serialization.Formatters.Binary" %>
<%@ Import Namespace="System.Drawing" %><Script Runat="Server">Dim arrCalendar( 13, 32 ) As String' Get schedule from cache or disk
Sub Page_Load
Dim strmFileStream As FileStream
Dim fmtrBinaryFormatter As BinaryFormatter If Cache( "arrCalendar" ) Is Nothing Then
If File.Exists( "c:\schedule.bin" ) Then
strmFileStream = _
New FileStream( "c:\schedule.bin", FileMode.Open )
fmtrBinaryFormatter = New BinaryFormatter
arrCalendar = _
CType( fmtrBinaryFormatter.Deserialize( strmFileStream ), Array )
strmFileStream.Close()
Cache( "arrCalendar" ) = arrCalendar
End If
Else
arrCalendar = Cache( "arrCalendar" )
End If
End Sub' Save schedule to file
Sub btnSave_Click( s As Object, e As EventArgs )
Dim dtmDate As DateTime
Dim strmFileStream As FileStream
Dim fmtrBinaryFormatter As BinaryFormatter dtmDate = calSchedule.SelectedDate
arrCalendar( dtmDate.Month, dtmDate.Day ) = txtNotes.Text
strmFileStream = _
New FileStream( "c:\schedule.bin", FileMode.Create )
fmtrBinaryFormatter = New BinaryFormatter
fmtrBinaryFormatter.Serialize( strmFileStream, arrCalendar )
strmFileStream.Close()
Cache( "arrCalendar" ) = arrCalendar
End Sub' Pick a date
Sub Calendar_SelectionChanged( s As Object, e As EventArgs )
Dim dtmDate As DateTime dtmDate = calSchedule.SelectedDate
txtNotes.Text = arrCalendar( dtmDate.Month, dtmDate.Day )
End Sub' Display each calendar day
Sub Calendar_RenderDay( s As Object, e As DayRenderEventArgs )
Dim dtmDate As DateTime
Dim ctlCell As TableCell dtmDate = e.Day.Date
ctlCell = e.Cell
If arrCalendar( dtmDate.Month, dtmDate.Day ) <> "" Then
ctlCell.BackColor = Color.FromName( "Orange" )
End If
End Sub</Script><html>
<head><title>CalendarSchedule.aspx</title></head>
<body><form Runat="Server"><asp:Calendar
id="calSchedule"
Width="100%"
ShowGridLines="True"
OnSelectionChanged="Calendar_SelectionChanged"
OnDayRender="Calendar_RenderDay"
Runat="Server" /><p>
<asp:TextBox
id="txtNotes"
TextMode="MultiLIne"
Columns="50"
Rows="10"
Runat="Server" /><p>
<asp:Button
id="btnSave"
Text="Save Changes"
OnClick="btnSave_Click"
Runat="Server" /></form>
</body>
</html> 以上代码生成了一个13*32的string二维数组用来存储日历上每一天的备忘录,并写入
c:\schedule.bin文件中。如果某日备忘录中有内容,则日历上就会显示桔红色。备忘录的输入输出都在txtbox中完成。
我试图将其用c#来改写,遇到了很大的问题,以下是我改写的c#程序:<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Runtime.Serialization" %>
<%@ Import Namespace="System.Runtime.Serialization.Formatters.Binary" %>
<%@ Import Namespace="System.Drawing" %><Script Language="c#" Runat="Server">Object[,] arrCalendar = new Object[13,32];// Get schedule from cache or disk
void Page_Load() {
FileStream strmFileStream = new FileStream();
BinaryFormatter fmtrBinaryFormatter = new BinaryFormatter(); if (Cache["arrCalendar"] == Null)
if File.Exists( "c:\schedule.bin" ) {
strmFileStream = FileStream( "c:\schedule.bin", FileMode.Open );
arrCalendar = Cype( fmtrBinaryFormatter.Deserialize( strmFileStream ), Array );
strmFileStream.Close();
Cache["arrCalendar"] = arrCalendar;
}
else
arrCalendar = Cache["arrCalendar"];
}// Save schedule to file
void btnSave_Click( Object s, EventArgs e ) {
DateTime dtmDate = new DateTime();
FileStream strmFileStream = new FileStream();
BinaryFormatter fmtrBinaryFormatter = new BinaryFormatter(); dtmDate = calSchedule.SelectedDate;
arrCalendar[dtmDate.Month, dtmDate.Day] = txtNotes.Text;
strmFileStream = FileStream( "c:\schedule.bin", FileMode.Create );
fmtrBinaryFormatter.Serialize( strmFileStream, arrCalendar );
strmFileStream.Close();
Cache["arrCalendar"] = arrCalendar;
}// Pick a date
void Calendar_SelectionChanged( Object s, EventArgs e ) {
DateTime dtmDate = new DateTime(); dtmDate = calSchedule.SelectedDate;
txtNotes.Text = arrCalendar[dtmDate.Month, dtmDate.Day].ToString();
}// Display each calendar day
void Calendar_RenderDay( Object s, DayRenderEventArgs e ) {
DateTime dtmDate = new DateTime;
Table ctlCell = new TableCell(); dtmDate = e.Day.Date;
ctlCell = e.Cell;
if (arrCalendar[dtmDate.Month, dtmDate.Day] != "" )
ctlCell.BackColor = Color.FromName( "Orange" );
}
}</Script><html>
<head><title>CalendarSchedule_.aspx</title></head>
<body><form Runat="Server"><asp:Calendar
id="calSchedule"
Width="100%"
ShowGridLines="True"
OnSelectionChanged="Calendar_SelectionChanged"
OnDayRender="Calendar_RenderDay"
Runat="Server" /><p>
<asp:TextBox
id="txtNotes"
TextMode="MultiLIne"
Columns="50"
Rows="10"
Runat="Server" /><p>
<asp:Button
id="btnSave"
Text="Save Changes"
OnClick="btnSave_Click"
Runat="Server" /></form>
</body>
</html>
这个程序的编译统不过,但是我不知道问题怎么解决。
问题一:
VB中检查Cashe中是否有arrcalendar数组存在的语句:
If Cache( "arrCalendar" ) Is Nothing Then
我改写成C#是否就是我程序中所写的:
if (Cache["arrCalendar"] == Null) ???
问题二:
VB中声明二维字符串数组:
Dim arrCalendar( 13, 32 ) As String
由于c#中string类不能只声明而不构造,所以我干脆使用:
Object[,] arrCalendar = new Object[13,32];
采用object类进行封箱操作。这样做不知道可以不可以???
问题三:
VB中的这一句:
arrCalendar = _
CType( fmtrBinaryFormatter.Deserialize( strmFileStream ), Array )
CType到底是个什么东西?完成什么功能?
我改写成c#也是使用了Ctype但是我使用VS.net的检索查到c#中没有CType,到底完成想通功能的c#程序怎么写?由于我对VB不懂,只能大概看懂程序,所以请教VB和c#高手帮忙看看!!!初来乍到,请多多指教!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货