-2

I have converted the following two classes to c# from vb.net, but get a reference error. Can someone please help or explain why it does not work in c# but does in vb.net?

Member class:

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 class:

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;
    }
}

I get error but don't know why

An object reference is required for the non-static field, method, or property for MbrSheet.mSheet, but both worked in VB.net

if (!MbrSheet.mSheet.ContainsKey(SheetID)) // Error on !MbrSheet.mSheet
{
    MbrSheet.mSheet.Add(SheetID, new MbrSheet(SheetID)); // Error on MbrSheet.mSheet
}
T. Word
  • 21
  • 4
  • 1
    @DStanley **An object reference is required for the non-static field, method, or property** is completely different from `NullReferenceException`, don't you think? – Racil Hilan Oct 01 '15 at 21:05
  • @DStanley It is not a null reference exception : An object reference is required for the non-static field, method, or property. It's actually a typo in the Member class constructor. Should be `MbrSheet.mSheets`, not `MbrSheet.mSheet` – Jakub Lortz Oct 01 '15 at 21:05
  • How can this be a null reference if I am looking if MbrSheet.mSheet dictionary contains the SheetID key? – T. Word Oct 01 '15 at 21:06
  • @JakubLortz you are correct, it is just a typo. How to I accept your input as correct? – T. Word Oct 01 '15 at 21:08
  • Next time, use a tool: [like this](http://www.developerfusion.com/tools/convert/vb-to-csharp/) or [this](http://converter.telerik.com/). Nobody converts VB.NET to C# manually. – Thomas Weller Oct 01 '15 at 21:13

1 Answers1

0

I think you should use this:

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

Pay attention to mSheets you are using mSheet.


You can also use tools to convert codes:

Reza Aghaei
  • 103,774
  • 12
  • 145
  • 300