将VB.net中的两个类转换为C#但得到对象引用错误

时间:2015-10-01 20:55:09

标签: c# vb.net

我已将以下两个类转换为vb.net中的c#,但是获得了引用错误。有人可以帮助或解释为什么它在c#中不起作用,但在vb.net中有效吗?

会员班:

public class Member
{

    #region "Fields"
    private string fPiecemark;
    private string fMemberType;
    private string fSize;
    private string fTotalWeight;
    private int fSheetKey;
    private string fDescription;
    private string fStructType;
    #endregion
    private string fMemberSheetIndex;

    #region "Constructors"

    //Default class Constructor
    public Member()
    {
        fPiecemark = string.Empty;
        fMemberType = string.Empty;
        fSize = string.Empty;
        fTotalWeight = string.Empty;
        fSheetKey = 0;
        fStructType = string.Empty;
    }


    public Member(string Piecemark, string MemberType, string Description, string Size, string TotalWeight, string StructType, string MemberSheetIndex, int SheetID)
    {
        this.Piecemark = Piecemark;
        this.MemberType = MemberType;
        this.Description = Description;
        this.Size = Size;
        this.TotalWeight = TotalWeight;
        this.StructType = StructType;
        this.MemberSheetIndex = MemberSheetIndex;
        this.SheetKey = SheetID;

        if (!MbrSheet.mSheet.ContainsKey(SheetID))
        {
            MbrSheet.mSheet.Add(SheetID, new MbrSheet(SheetID));
        }

        MbrSheet.mSheets[SheetID].Members.Add(this);

    }

    #endregion

    #region "Properties"

    public string Piecemark
    {
        get { return fPiecemark; }
        set { fPiecemark = value; }
    }

    public string MemberType
    {
        get { return fMemberType; }
        set { fMemberType = value; }
    }

    public string TotalWeight
    {
        get { return fTotalWeight; }
        set { fTotalWeight = value; }
    }

    public string Size
    {
        get { return fSize; }
        set { fSize = value; }
    }
    public int SheetKey
    {
        get { return fSheetKey; }
        set { fSheetKey = value; }
    }

    public string Description
    {
        get { return fDescription; }
        set { fDescription = value; }
    }

    public string StructType
    {
        get { return fStructType; }
        set { fStructType = value; }
    }

    public string MemberSheetIndex
    {
        get { return fMemberSheetIndex; }
        set { fMemberSheetIndex = value; }
    }

    #endregion

}

MbrSheet类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;


public class MbrSheet
{
    public static Dictionary<int, MbrSheet> mSheets = new Dictionary<int, MbrSheet>();
    public int mSheet { get; set; }
    public List<Member> Members { get; set; }

    public MbrSheet(int MbrSheet)
    {
        Members = new List<Member>();
        this.mSheet = MbrSheet;
    }

    public static decimal WeightByType(string MemberType)
    {
        var subset = mSheets.Where(kvp => kvp.Value.Members.Where(m => m.MemberType == MemberType).Count() > 0);
        decimal wbt = 0;
        wbt += mSheets
         .Where(kvp => kvp.Value.Members.Where(m => m.MemberType == MemberType).Count() > 0)
         .Sum(kvp => kvp.Value.Members.Sum(m => Convert.ToDecimal(m.TotalWeight, CultureInfo.InvariantCulture)));
        return wbt;
    }
}

我收到错误但不知道为什么

MbrSheet.mSheet的非静态字段,方法或属性需要对象引用,但两者都在VB.net中工作

if (!MbrSheet.mSheet.ContainsKey(SheetID)) // Error on !MbrSheet.mSheet
{
    MbrSheet.mSheet.Add(SheetID, new MbrSheet(SheetID)); // Error on MbrSheet.mSheet
}

1 个答案:

答案 0 :(得分:0)

我认为你应该使用它:

if (!MbrSheet.mSheets.ContainsKey(SheetID))
{
    MbrSheet.mSheets.Add(SheetID, new MbrSheet(SheetID));
}

注意 mSheets 您正在使用 mSheet

您还可以使用工具转换代码: