我在记忆丧失的路上走了一段路,发现自己陷入了一小段代码的僵局。整个星期都在研究它,没有任何东西出现,以解释我做错了什么。我试图将其转换为C#。原始的VB代码是 -
Public Class CustomFormData
Private ReadOnly Context As System.Web.HttpContext
Private ReadOnly l_QSItems As New System.Collections.ArrayList
Private ReadOnly Property CookieName() As String
Get
Return "EmberCookie"
End Get
End Property
Private ReadOnly Property MaxStringSize() As Integer
Get
Return 10
End Get
End Property
Default Protected Property Item(ByVal index As enu.QS) As String
Get
Try
Return l_QSItems.Item(index).ToString
Catch ex As Exception
Return ""
End Try
End Get
Set(ByVal Value As String)
ValidateStringSize(index.ToString, Value)
l_QSItems.Item(index) = Value
SaveCookie(index, Value)
End Set
End Property
Private Sub ValidateStringSize(ByVal [name] As String, ByVal value As String)
If value IsNot Nothing AndAlso value.Length > Me.MaxStringSize Then
Throw New System.Security.SecurityException( _
String.Format("{0} contains value larger than allowed length of {1}. Size was {2} char(s).", [name], Me.MaxStringSize, value.Length))
End If
End Sub
到目前为止我所取得的成就是 -
class CustomFormData
{
private readonly System.Collections.ArrayList l_QSItems = new System.Collections.ArrayList();
private readonly int MaxStringSize
{
get
{
return 10;
}
}
protected string Item(enu.QS index)
{
get
{
try
{
return Item(index).ToString();
}
catch(Exception ex)
{
return "";
}
}
set (string Value)
{
ValidateStringSize(index.ToString(), Value);
l_QSItems.Item(index) == Value;
}
}
private string ValidateStringSize(string[] name, string value)
{
if (value != null && value.Length > MaxStringSize)
{
throw new System.Security.SecurityException(string.Format("{0} contains value larger than allowed length of {1}. Size was {2} char(s).", name, MaxStringSize, value.Length));
}
else
{
return "";
}
}
}
}
导致多个错误 -
Error 1 } expected c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 31 14 EMBER-Win
Error 2 Method must have a return type c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 32 13 EMBER-Win
Error 3 Expected class, delegate, enum, interface, or struct c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 39 17 EMBER-Win
Error 4 Identifier expected c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 39 50 EMBER-Win
Error 5 Expected class, delegate, enum, interface, or struct c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 39 52 EMBER-Win
Error 6 Expected class, delegate, enum, interface, or struct c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 43 27 EMBER-Win
Error 7 A namespace cannot directly contain members such as fields or methods c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 45 13 EMBER-Win
Error 8 Type or namespace definition, or end-of-file expected c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 49 9 EMBER-Win
Error 9 ; expected c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 21 16 EMBER-Win
Error 10 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 21 13 EMBER-Win
Error 11 The name 'get' does not exist in the current context c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 21 13 EMBER-Win
Error 12 The name 'ValidateStringSize' does not exist in the current context c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 34 17 EMBER-Win
Error 13 The name 'index' does not exist in the current context c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 34 36 EMBER-Win
Error 14 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 35 17 EMBER-Win
Error 15 'System.Collections.ArrayList' does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument of type 'System.Collections.ArrayList' could be found (are you missing a using directive or an assembly reference?) c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 35 27 EMBER-Win
Error 16 The name 'index' does not exist in the current context c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 35 32 EMBER-Win
Error 17 The type or namespace name '?Attribute' could not be found (are you missing a using directive or an assembly reference?) c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 39 49 EMBER-Win
非常感谢您的协助。如果您了解专注于VB到VC#转换的期刊或网站,那将非常有帮助。多年来,我的许多技术手册都没有回到我的立方体,所以我错过了C#的源信息。
谢谢你的朋友,周末愉快。
:d
根据建议,这就是它现在的位置 -
class CustomFormData
{
private readonly System.Collections.ArrayList l_QSItems = new System.Collections.ArrayList();
private readonly int MaxStringSize
{
get
{
return 10;
}
}
protected string this[enu.QS index]
{
get
{
try
{
return l_QSItems[index].ToString();
}
catch(Exception ex)
{
return "";
}
}
set
{
ValidateStringSize(index.ToString(), value);
l_QSItems[index] = value;
}
}
我有这个错误:
Error 1 The best overloaded method match for 'System.Collections.ArrayList.this[int]' has some invalid arguments c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 25 28 EMBER-Win
Error 2 Argument 1: cannot convert from 'enu.QS' to 'int' c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 25 38 EMBER-Win
Error 3 The best overloaded method match for 'System.Collections.ArrayList.this[int]' has some invalid arguments c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 35 17 EMBER-Win
Error 4 Argument 1: cannot convert from 'enu.QS' to 'int' c:\Projects\EMBER-Win\EMBER-Win\_common\CustomFormData.cs 35 27 EMBER-Win
对于那些提交过代码的人来说,这是我遗漏的一部分。
public enum QS
{
//'For Policy
policy_class,
fiscal_ref_id,
cycle_fy,
cycle_month,
policy_version,
policyReadOnly,
//'For Base Estimate
mode_code,
dvar_id,
node_id,
aids_code,
county_code,
care_code,
plan_code,
ffs_code,
service_code,
errorOnly,
additional_actuals_month_id,
//'For policy change search
search_cycle_version,
search_analyst_id,
search_category_code,
search_change_number,
search_fiscal_ref,
secondary_sort,
sort_descending,
//'To identify the BO narrative report layout for selected policy
narrative_option,
//'To identify version selected for Federal spread
selected_version
};
再次感谢你的帮助。
答案 0 :(得分:2)
您的索引器存在一些问题。
在C#中,我们使用方括号,“this”索引器为this
,而不是Item
。 set
永远不会获得明确的参数;这是隐式定义的,它总是value
,都是小写的。 C#区分大小写。
另外,您使用==
进行分配。在C#中,与C中一样,double equals用于比较,single equals用于赋值。
我不知道在Item
get
中该怎么做:
return Item(index).ToString();
这看起来无限递归我。那是什么捕获的? (j / k)或者它应该是l_QSItems.Item(index).ToString()
吗?我将假设后者。如果这只是私有字段的验证包装索引器,那将是有意义的。
public class CustomFormData
{
private System.Web.HttpContext Context;
private String CookieName
{
get { return "EmberCookie"; }
}
private readonly System.Collections.ArrayList l_QSItems
// Initialize with enough nulls to accomodate the largest enum value as an index.
= new System.Collections.ArrayList(new object[(int)enu.QS.additional_actuals_month_id + 1]);
// readonly modifier not allowed here. Not needed either.
// Same as on CookieName
private int MaxStringSize
{
get
{
return 10;
}
}
protected string this[enu.QS index]
{
get
{
try
{
// index is an enum. It won't implicitly cast to int as in
// C/C++ (or, I infer, VB.NET), but an explicit cast is fine.
return l_QSItems[(int)index].ToString();
}
catch (Exception ex)
{
return "";
}
}
set
{
ValidateStringSize(index.ToString(), value);
l_QSItems[(int)index] = value;
}
}
// name[] would be an array of strings, we only want just one string.
private void ValidateStringSize(string name, string value)
{
if (value != null && value.Length > this.MaxStringSize)
{
throw new System.Security.SecurityException(string.Format("{0} contains value larger than allowed length of {1}. Size was {2} char(s).", name, this.MaxStringSize, value.Length));
}
}
}
这也有问题:
private string ValidateStringSize(string[] name, string value)
string[]
是一个字符串数组;单个字符串只是string
。从使用情况和方法本身可以清楚地看出name
应该是一个字符串,而不是数组:
private string ValidateStringSize(string name, string value)
很久以前你在某个时候接触过C?
答案 1 :(得分:2)
除了没有enu.QS类型或SaveCookie方法之外,这个编译。也许它有用。
using System;
public class CustomFormData
{
private readonly System.Web.HttpContext Context;
private readonly System.Collections.ArrayList l_QSItems = new System.Collections.ArrayList();
private string CookieName
{
get { return "EmberCookie"; }
}
private int MaxStringSize
{
get { return 10; }
}
protected string this[enu.QS index]
{
get
{
try
{
return l_QSItems[Convert.ToInt32(index)].ToString();
}
catch (Exception ex)
{
return "";
}
}
set
{
ValidateStringSize(index.ToString(), value);
l_QSItems[Convert.ToInt32(index)] = value;
SaveCookie(index, value);
}
}
private void ValidateStringSize(string name, string value)
{
if (value != null && value.Length > this.MaxStringSize)
{
throw new System.Security.SecurityException(string.Format("{0} contains value larger than allowed length of {1}. Size was {2} char(s).", name, this.MaxStringSize, value.Length));
}
}
}