VB到VC#类转换-脑死亡

VB到VC#类转换-脑死亡,c#,vb.net,class,properties,C#,Vb.net,Class,Properties,我在失忆的路上走了一小段路,发现自己因为一点点代码而陷入僵局。我整个星期都在研究这个问题,没有任何东西能解释我做错了什么。我正试图把它转换成C。原来的VB代码是- Public Class CustomFormData Private ReadOnly Context As System.Web.HttpContext Private ReadOnly l_QSItems As New System.Collections.ArrayList Private Read

我在失忆的路上走了一小段路,发现自己因为一点点代码而陷入僵局。我整个星期都在研究这个问题,没有任何东西能解释我做错了什么。我正试图把它转换成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
};

再次感谢您的帮助。

您的索引器有一些问题

在C#中,我们使用方括号,“this”索引器是
this
,而不是
集合
从未获得显式参数;这是隐式定义的,它总是
,都是小写的。C#区分大小写

此外,您还将
==
用于分配。在C#中,与在C中一样,双等用于比较,单等用于赋值

我不太知道在
项中如何理解这一点
get

return Item(index).ToString();
这在我看来是无限递归的。这就是被抓的原因吗?(j/k)还是应该是
l\qsu items.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[]
是字符串数组;单个字符串就是
字符串
。从用法和方法本身来看,
name
应该是一个字符串,而不是一个数组:

private string ValidateStringSize(string name, string value) 

你很久以前接触过C吗?

你的索引器有一些问题

在C#中,我们使用方括号,“this”索引器是
this
,而不是
集合
从未获得显式参数;这是隐式定义的,它总是
,都是小写的。C#区分大小写

此外,您还将
==
用于分配。在C#中,与在C中一样,双等用于比较,单等用于赋值

我不太知道在
项中如何理解这一点
get

return Item(index).ToString();
这在我看来是无限递归的。这就是被抓的原因吗?(j/k)还是应该是
l\qsu items.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[]
是字符串数组;单个字符串就是
字符串
。从用法和方法本身来看,
name
应该是一个字符串,而不是一个数组:

private string ValidateStringSize(string name, string value) 

您很久以前接触过C吗?

除了没有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));
        }
    }
}

除了没有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));
        }
    }
}


你试过通过转换器运行它吗?C#中的索引器使用方括号,而不是parensAs@EdPlunkett提到的,我会看看如何在C#中创建索引器,这就是你的大多数错误似乎来自Popo,Ed,先生们,“它需要一个村庄”的道理。你的两条评论都提供了我所需要的信息。谢谢你润滑了这个古老的大脑。Ed——这一期似乎是一篇将VB类转换为C#类的好文章。我正在开发的特定程序是2002年为Web应用程序编写的,由于某些操作限制和新的客户需求,我正在将其转换为WinForm项目。当我浏览每一个类模块时,VB的一些特殊方面开始显露出来。你试过通过转换器运行它吗?C#中的索引器使用方括号,而不是parensAs@EdPlunkett提到的,我会看看如何在C#中创建索引器,这就是你的大多数错误似乎来自Popo,Ed,先生们,“需要一个村庄”是有道理的。你的两条评论都提供了我所需要的信息。谢谢你润滑了这个古老的大脑。Ed——这一期似乎是一篇将VB类转换为C#类的好文章。我正在开发的特定程序是2002年为Web应用程序编写的,由于某些操作限制和新的客户需求,我正在将其转换为WinForm项目。当我浏览每一个类模块时,VB的几个特殊方面都被揭示了出来。是的,我在80年代做过C,大部分是嵌入式的。错误1与'System.Collections.ArrayList.this[int]匹配的最佳重载方法'具有一些无效参数c:\Projects\EMBER-Win\EMBER-Win\u common\CustomFormData.cs 25 28 EMBER-Win错误2参数1:无法从'enu.QS'转换为'int'c:\Projects\EMBER-Win\EMBER-Win\u common\CustomFormData.cs 25 38 EMBER-Win@DarrellRobertParker我猜VB是隐式地将枚举转换为int。我添加了一个显式转换使整件事都能编译。Popo首先从
MaxStringSize
中删除了
readonly
。@DarrellRobertParker请参阅最终(我希望)更新。我在ArrayList中得到了与任意索引相关的异常,因此我添加了一个修复程序。@EdPlunkett VB对枚举进行隐式转换为Int,我只是对其进行了测试。:)是的,我早在80年代就用过C,大部分是嵌入式的。错误1“System.Collections.ArrayList.this[int]”的最佳重载方法匹配的参数C:\Projects\EM无效