How to fix 'System.StackOverflowException? - c#

I have a project programmed in C# NET 3.5 in WPF which start very well in debug. However for example, in my TemplateColor.cs class I have this:
using System;
using System.Windows.Media;
namespace EWIN_THEME_MAKER.ThemeMaker
{
public class TemplateColor
{
private int _id;
private string _name;
private string _toneName;
public int ID
{
get
{
return this._id;
}
set
{
this._id = value;
}
}
public int AllVerticalPos
{
get;
set;
}
public int AllHorizontalPos
{
get;
set;
}
public int HuePageNum
{
get;
set;
}
public int HuePos
{
get;
set;
}
public int TonePaneNum
{
get;
set;
}
public int TonePos
{
get;
set;
}
public string Name
{
get
{
return this._name;
}
set
{
this._name = value;
string[] array = this._name.Split(new char[]
{
'_'
});
this._toneName = array[0];
if (array.Length > 1)
{
this.HuePageNum = int.Parse(array[1]);
}
}
}
public string ToneName
{
get
{
return this._toneName;
}
set
{
this._toneName = value;
}
}
public Color DarkColor
{
get;
set;
}
public Color MainColor
{
get;
set;
}
public Color LightColor
{
get;
set;
}
public Color ShadowColor
{
get;
set;
}
public byte ShadowA
{
get;
set;
}
public Color GrowColor
{
get;
set;
}
public Color TextShadowColor
{
get;
set;
}
public Color TextMainColor
{
get;
set;
}
public Color TextSelectColor
{
get;
set;
}
public double TextShadowPosition
{
get;
set;
}
public Brush DarkColorBrush
{
get
{
return new SolidColorBrush(this.DarkColor);
}
}
public Brush MainColorBrush
{
get
{
return new SolidColorBrush(this.MainColor);
}
}
public Brush LightColorBrush
{
get
{
return new SolidColorBrush(this.LightColor);
}
}
public Brush ShadowColorBrush
{
get
{
return new SolidColorBrush(this.ShadowColor);
}
}
public Brush GrowColorBrush
{
get
{
return new SolidColorBrush(this.GrowColor);
}
}
public Brush TextShadowColorBrush
{
get
{
return new SolidColorBrush(this.TextShadowColor);
}
}
public Brush TextMainColorBrush
{
get
{
return new SolidColorBrush(this.TextMainColor);
}
}
public Brush TextSelectColorBrush
{
get
{
return new SolidColorBrush(this.TextSelectColor);
}
}
public TemplateColor()
{
this.ID = -1;
this.AllVerticalPos = -1;
this.AllHorizontalPos = -1;
this.HuePageNum = -1;
this.HuePos = -1;
this.TonePaneNum = -1;
this.TonePos = -1;
this.Name = "---";
this.DarkColor = default(Color);
this.MainColor = default(Color);
this.LightColor = default(Color);
this.ShadowColor = default(Color);
this.ShadowA = 0;
this.GrowColor = default(Color);
this.TextShadowColor = default(Color);
this.TextMainColor = default(Color);
this.TextSelectColor = default(Color);
this.TextShadowPosition = 0.0;
}
}
}
In this code, there are multiple errors of this kind:
An exception of type 'System.StackOverflowException
For example I take this part of the code there:
public string Name
{
get
{
return this._name;
}
set
{
this._name = value;
string[] array = this._name.Split(new char[]
{
'_'
});
this._toneName = array[0];
if (array.Length > 1)
{
this.HuePageNum = int.Parse(array[1]);
}
}
}
So, in the next lines of this code there is an infinite call? I do not know how to correct this error.
string[] array = this._name.Split(new char[]
And...
this.HuePageNum = int.Parse(array[1]);
How do we predict and correct these errors?

My problem is now solved.
Solution: Increasing the size of the stack
Thanks to all!

Related

Best way to write application data to a file in WPF

I am using the Caliburn.Micro MVVM pattern.
I am writing an application that has 2 DataGrids, one holds a BindableCollection of RepairOrder, the other a BindableCollection WriteOff.
BindableCollection_WriteOff is property of BindableCollection_RepairOrder. (See the below code).
I need to find a way to write all the RepairOrder including the WriteOff associated with each RO. Besides RepairOrder class holding the WriteOff class, the WriteOff class does not have a way to tie the WriteOff to a RepairOrder.
Repair Order Class:
public class RepairOrder
{
public string Schedule { get; set; }
public string ControlNumber { get; set; }
public int Age { get; set; }
public double Value { get; set; }
public string Note { get; set; }
public double OrgValue { get; set; }
private List<WriteOff> _myWriteOffs;
public List<WriteOff> GetMyWriteOffs()
{
return _myWriteOffs;
}
public void AddMyWriteOff(WriteOff value)
{ _myWriteOffs.Add(value); }
public void DeleteMyWriteOff(WriteOff value)
{ _myWriteOffs.Remove(value); }
public RepairOrder(string CN, string SC, double VL)
{
ControlNumber = CN;
Schedule = SC;
Value = Math.Round(VL, 2);
Note = null;
_myWriteOffs = new List<WriteOff>();
}
public RepairOrder()
{
_myWriteOffs = new List<WriteOff>();
}
public static RepairOrder FromCSV(string CSVLine, string Sched)
{
string[] values = CSVLine.Split(',');
RepairOrder rep = new RepairOrder();
rep.ControlNumber = values[2];
rep.Value = Math.Round(double.Parse(values[5]),2);
rep.Age = int.Parse(values[4]);
rep.Schedule = Sched;
return rep;
}
}
Write Off Class:
public class WriteOff
{
private string _store;
public string Account { get; set; }
public string Description { get; set; }
public double WriteOffAmount { get; set; }
public string Schedule { get; set; }
public string Store
{
get {
if (String.IsNullOrEmpty(_store)) return "";
string temp = _store.Substring(0, 3);
return temp;
}
set { _store = value; }
}
public string Note { get; set; }
public WriteOff(string Acct, string Desc, double Amount, string _store)
{
Account = Acct;
Description = Desc;
WriteOffAmount = Amount;
Store = _store;
}
public string GetWOAccount() {
string SchedAccountNumber = "";
//{ "Navistar", "Cummins", "Misc", "Kenworth", "Mack/Volvo" }
switch (Account)
{
case "Navistar":
SchedAccountNumber = "222000";
break;
case "Cummins":
SchedAccountNumber = "223000";
break;
case "Misc":
SchedAccountNumber = "224500";
break;
default:
SchedAccountNumber = "";
break;
}
return SchedAccountNumber;
}
}

LINQ query does not return child fields

I think I'm getting stupid because I can't get my LINQ query to work as I expect. I have one class that has 3 relationships to other classes.
This is the main class
[Table(Name = "scanResult")]
public class SniffResult
{
public SniffResult()
{
}
public SniffResult(Address address)
{
this.address = address;
}
private int _pk_SniffResult;
[Column(IsPrimaryKey = true, IsDbGenerated = true, Storage = "_pk_SniffResult", Name ="pk_scanResult")]
public int pk_SniffResult { get { return _pk_SniffResult; } set { this._pk_SniffResult = value; } }
private int _fk_scan;
[Column(Storage = "_fk_scan", Name = "scan")]
public int fk_scan { get { return _fk_scan; } set { this._fk_scan = value; } }
private Scan _scan;
[Association(Storage = "_scan", IsForeignKey = true, ThisKey = "fk_scan", OtherKey = "pk_scan")]
public Scan scan { get { return _scan; } set { this._scan = value; } }
private int _fk_address;
[Column(Storage = "_fk_address", Name = "address")]
public int fk_adress { get { return _fk_address; } set { this._fk_address = value; } }
private Address _address;
[Association(Storage ="_address", IsForeignKey = true, ThisKey = "fk_adress", OtherKey = "pk_address")]
public Address address { get { return _address; } set { this._address = value; } }
private string _rawResult;
[Column(Storage = "_rawResult", Name = "raw")]
public string rawResult { get { return _rawResult; } set { this._rawResult = value; } }
private int _code = -5;
[Column(Storage = "_code")]
public int code { get { return _code; } set { this._code = value; } }
private DateTime _scanDate = DateTime.Now;
[Column(Storage = "_scanDate")]
public DateTime scanDate { get { return _scanDate; } set { this._scanDate = value; } }
private int? _fk_proxy;
[Column(Storage = "_fk_proxy", Name = "usedProxy", CanBeNull = true)]
public int? fk_proxy { get { return _fk_proxy; } set { this._fk_proxy = value; } }
private ProxyData _usedProxy;
[Association(Storage = "_usedProxy", IsForeignKey = true, ThisKey = "fk_proxy", OtherKey = "pk_proxy")]
public ProxyData usedProxy { get { return _usedProxy; } set { this._usedProxy = value; } }
public string message { get; set; } = "";
public bool availability { get; set; }
public int planCode { get; set; }
public string planning { get; set; }
public override string ToString()
{
return string.Format("availability={0}, code={1}, message={2}", availability, code, message);
}
}
This is one of the child classes
[Table(Name = "address")]
public class Address
{
private int _pk_address;
[Column(IsPrimaryKey = true, IsDbGenerated = true, Storage = "_pk_address")]
public int pk_address { get { return _pk_address; } set { this._pk_address = value; } }
private string _provId;
[Column(Storage = "_provId")]
public string provId { get { return _provId; } set { this._provId = value; } }
private string _zipcode;
[Column(Storage = "_zipcode")]
public string zipcode { get { return _zipcode; } set { this._zipcode = value; } }
private int _houseNumber;
[Column(Storage = "_houseNumber")]
public int houseNumber { get { return _houseNumber; } set { this._houseNumber = value; } }
private string _addressAddition;
[Column(Storage = "_addressAddition")]
public string addressAddition { get { return _addressAddition; } set { this._addressAddition = value; } }
public string ToAddresString()
{
return string.Format("{0} {1}{2}", this.provId, zipcode, houseNumber, addressAddition);
}
public override string ToString()
{
return string.Format("{0}:\t{1}\t{2}{3}", this.provId, zipcode, houseNumber, addressAddition);
}
}
I want to get SniffResult including the associated fields. But they are all null. This is the code I use:
SniffDAO dao = new SniffDAO();
var test = from sr in dao.SniffResults
where sr.fk_scan == 3
select sr;
foreach (var one in test)
{
Console.WriteLine(one.pk_SniffResult);
Console.WriteLine(one.address);
}
one.address gives me a null, what am I doing wrong?
This is the Dao class
public class SniffDAO
{
private string currentDir;
private DataContext db;
public Table<Scan> Scans { get; set; }
public Table<SniffResult> SniffResults { get; set; }
public Table<Address> Addresses { get; set; }
public SniffDAO()
{
db = new DataContext(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=********;Integrated Security=True;Asynchronous Processing=True");
db.ObjectTrackingEnabled = true;
Scans = db.GetTable<Scan>();
SniffResults = db.GetTable<SniffResult>();
Addresses = db.GetTable<Address>();
currentDir = Directory.GetCurrentDirectory();
}
public void save()
{
db.SubmitChanges();
}
}
You need to Include related entities like this:
SniffDAO dao = new SniffDAO();
var test = dao.SniffResults.Include(x=>x.address).Where(sr.fk_scan == 3);
foreach (var one in test)
{
Console.WriteLine(one.pk_SniffResult);
Console.WriteLine(one.address);
}
I found the solution thanks to #Kris. I now use:
DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<SniffResult>(sr => sr.address);
dlo.LoadWith<SniffResult>(sr => sr.usedProxy);
dlo.LoadWith<SniffResult>(sr => sr.scan);
db.LoadOptions = dlo; //db is the DataContext
See https://msdn.microsoft.com/en-us/library/bb548760(v=vs.110).aspx for more info

XMLAttribute is not working

I am Deserializing my xml into C# class.
<?xml version="1.0" encoding="UTF-8"?>
<Applications>
<Application Name = "name1" MakerCheckerType="Operational" SanctionCheckNeeded="N" PreExistCheckNeeded="N" >
<InstrumentTypes>
<InstrumentType Version="1.0" PrimaryKeyExcel="Security Name" Name="Equity">
</InstrumentType>
<InstrumentType Name="Bond" Version="1.0" PrimaryKeyExcel="Security Name">
</InstrumentType>
</InstrumentTypes>
<ProcessSteps>
<ProcessStep VBAFunction="xyz" ExcelName="xyz" Name="Upload" />
<ProcessStep Name ="Approve_Reject" VBAFunction="xyz" ExcelName="xyz"/>
</ProcessSteps>
</Application>
<Application Name = "name2" MakerCheckerType="Real" SanctionCheckNeeded="Y" PreExistCheckNeeded="Y">
<InstrumentTypes>
<InstrumentType Version="1.0" PrimaryKeyExcel="Security Name" Name="Equity">
</InstrumentType>
<InstrumentType Name="Bond" Version="1.0" PrimaryKeyExcel="Security Name">
</InstrumentType>
</InstrumentTypes>
<ProcessSteps>
<ProcessStep VBAFunction="xyz" ExcelName="xyz" Name="Upload" />
<ProcessStep Name ="Approve_Reject" VBAFunction="xyz" ExcelName="xyz"/>
</ProcessSteps>
</Application>
</Applications>
classes are:
[XmlType("ProcessStep")]
public class IMAProcessStep
{
private string name;
private string vbaFunction;
private string excelName;
[XmlAttribute("Name")]
public string Name
{
get { return name; }
set { name = value; }
}
[XmlAttribute("VBAFunction")]
public string VBAFunction
{
get { return vbaFunction; }
set { vbaFunction = value; }
}
[XmlAttribute("ExcelName")]
public string ExcelName
{
get { return excelName; }
set { excelName = value; }
}
}
[XmlType("InstrumentType")]
public class IMAInstrumentType
{
[XmlAttribute("Name")]
public string Name
{
get;
set;
}
[XmlAttribute("Version")]
public string Version
{
get;
set;
}
[XmlAttribute("PrimaryKeyExcel")]
public string PrimaryKeyExcel
{
get;
set;
}
}
[XmlType("Application")]
public class IMAApplication
{
[XmlAttribute("Name")]
public string Name { get; set; }
[XmlAttribute("MakerCheckerType")]
public string MakerCheckerType { get; set; }
public bool IsMakerCheckerType
{
get
{
if (MakerCheckerType == "Real")
return true;
return false;
}
set
{
if (value)
MakerCheckerType = "Real";
else
MakerCheckerType = "Operational";
}
}
[XmlAttribute("SanctionCheckNeeded")]
public string SanctionCheckNeeded { get; set; }
[XmlAttribute("PreExistCheckNeeded")]
public string PreExistCheckNeeded { get; set; }
public bool IsSanctionCheckNeeded
{
get
{
return SanctionCheckNeeded == "Y";
}
set
{
SanctionCheckNeeded = value ? "Y" : "N";
}
}
public bool IsPreExistCheckNeeded
{
get
{
if (PreExistCheckNeeded == "Y")
return true;
return false;
}
set
{
if (value)
PreExistCheckNeeded = "Y";
else
PreExistCheckNeeded = "N";
}
}
[XmlArray("ProcessSteps")]
[XmlArrayItem(ElementName = "ProcessStep")]
public List<IMAInstrumentType> SupportedInstrumentTypes { get; set; }
[XmlArray("InstrumentTypes")]
[XmlArrayItem(ElementName = "InstrumentType")]
public List<IMAProcessStep> ProcessSteps { get; set; }
}
Here How I am De serializing it...
List<IMAApplication> appConfig = null;
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFilePath);
var xRoot = new XmlRootAttribute();
xRoot.ElementName = "Applications";
xRoot.IsNullable = true;
var serializer = new XmlSerializer(typeof(List<IMAApplication>), xRoot);
using (var stream = File.OpenRead(path))
appConfig = (List<IMAApplication>)serializer.Deserialize(stream);
return appConfig;
IMAApplication Deserialize successfully but processSteps and InstrumentTypes get only their Name attribute values. Rest of attributes are null.
Can anyone tell me what wrong here.
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
IMAApplications applications = Deserialize(FILENAME);
}
static IMAApplications Deserialize(string configFilePath)
{
IMAApplications appConfig = null;
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFilePath);
var xRoot = new XmlRootAttribute();
xRoot.ElementName = "Applications";
xRoot.IsNullable = true;
var serializer = new XmlSerializer(typeof(IMAApplications), xRoot);
using (var stream = File.OpenRead(path))
appConfig = (IMAApplications)serializer.Deserialize(stream);
return appConfig;
}
}
[XmlRoot("ProcessStep")]
public class IMAProcessStep
{
private string name;
private string vbaFunction;
private string excelName;
[XmlAttribute("Name")]
public string Name
{
get { return name; }
set { name = value; }
}
[XmlAttribute("VBAFunction")]
public string VBAFunction
{
get { return vbaFunction; }
set { vbaFunction = value; }
}
[XmlAttribute("ExcelName")]
public string ExcelName
{
get { return excelName; }
set { excelName = value; }
}
}
[XmlRoot("InstrumentType")]
public class IMAInstrumentType
{
[XmlAttribute("Name")]
public string Name
{
get;
set;
}
[XmlAttribute("Version")]
public string Version
{
get;
set;
}
[XmlAttribute("PrimaryKeyExcel")]
public string PrimaryKeyExcel
{
get;
set;
}
}
[XmlRoot("Applications")]
public class IMAApplications
{
[XmlElement("Application")]
public List<IMAApplication> applications { get; set; }
}
[XmlRoot("Application")]
public class IMAApplication
{
[XmlAttribute("Name")]
public string Name { get; set; }
[XmlAttribute("MakerCheckerType")]
public string MakerCheckerType { get; set; }
public bool IsMakerCheckerType
{
get
{
if (MakerCheckerType == "Real")
return true;
return false;
}
set
{
if (value)
MakerCheckerType = "Real";
else
MakerCheckerType = "Operational";
}
}
[XmlAttribute("SanctionCheckNeeded")]
public string SanctionCheckNeeded { get; set; }
[XmlAttribute("PreExistCheckNeeded")]
public string PreExistCheckNeeded { get; set; }
public bool IsSanctionCheckNeeded
{
get
{
return SanctionCheckNeeded == "Y";
}
set
{
SanctionCheckNeeded = value ? "Y" : "N";
}
}
public bool IsPreExistCheckNeeded
{
get
{
if (PreExistCheckNeeded == "Y")
return true;
return false;
}
set
{
if (value)
PreExistCheckNeeded = "Y";
else
PreExistCheckNeeded = "N";
}
}
[XmlArray("InstrumentTypes")]
[XmlArrayItem(ElementName = "InstrumentType")]
public List<IMAInstrumentType> SupportedInstrumentTypes { get; set; }
[XmlArray("ProcessSteps")]
[XmlArrayItem(ElementName = "ProcessStep")]
public List<IMAProcessStep> ProcessSteps { get; set; }
}
}

JSON to List<class> & List<class> to JSON?

Hi guys how do I get this to work? I searched in SO and this was the most promissing but it doesnt work either.
errormessage:
The deserialized type should be a normal .NET type (i.e. not a
primitive type like integer, not a collection type like an array or
List) or a dictionary type (i.e. Dictionary).
so how do i split the individual objects from my json?
List<class> a = JsonConvert.DeserializeObject<List<class>>(JSON_String)
the JSON string:
{
"SPALTEN": [{
"NUMMER": 1,
"NAME": "BREITE",
"TYP": "Double",
"LAENGE": 0,
"EINHEIT": "m",
"EDITIERBAR": true,
"OPTIONAL": true,
"LAYER": null,
"LAYER_SPALTE": null,
"D_SPAL_NAME": null,
"D_SPAL_MIN": 0,
"D_SPAL_MAX": null,
"D_SPAL_VAL": null
}, {
"NUMMER": 2,
"NAME": "KOMMENTAR",
"TYP": "String",
"LAENGE": 255,
"EINHEIT": null,
"EDITIERBAR": true,
"OPTIONAL": true,
"LAYER": null,
"LAYER_SPALTE": null,
"D_SPAL_NAME": null,
"D_SPAL_MIN": null,
"D_SPAL_MAX": null,
"D_SPAL_VAL": null
}]
}
here is my class:
public class CONFIG_CLASS
{
private int _NUMMER;
public int NUMMER
{
get { return _NUMMER; }
set { _NUMMER = value; }
}
private string _NAME;
public string NAME
{
get { return _NAME; }
set { _NAME = value; }
}
private string _TYP;
public string TYP
{
get { return _TYP; }
set { _TYP = value; }
}
private double _LAENGE;
public double LAENGE
{
get { return _LAENGE; }
set { _LAENGE = value; }
}
private string _EINHEIT;
public string EINHEIT
{
get { return _EINHEIT; }
set { _EINHEIT = value; }
}
private bool _EDITIERBAR;
public bool EDITIERBAR
{
get { return _EDITIERBAR; }
set { _EDITIERBAR = value; }
}
private bool _OPTIONAL;
public bool OPTIONAL
{
get { return _OPTIONAL; }
set { _OPTIONAL = value; }
}
private string _LAYER;
public string LAYER
{
get { return _LAYER; }
set { _LAYER = value; }
}
private int _LAYER_SPALTE;
public int LAYER_SPALTE
{
get { return _LAYER_SPALTE; }
set { _LAYER_SPALTE = value; }
}
private string _D_SPAL_NAME;
public string D_SPAL_NAME
{
get { return _D_SPAL_NAME; }
set { _D_SPAL_NAME = value; }
}
private int _D_SPAL_MIN;
public int D_SPAL_MIN
{
get { return _D_SPAL_MIN; }
set { _D_SPAL_MIN = value; }
}
private int _D_SPAL_MAX;
public int D_SPAL_MAX
{
get { return _D_SPAL_MAX; }
set { _D_SPAL_MAX = value; }
}
private string _D_SPAL_VAL;
public string D_SPAL_VAL
{
get { return _D_SPAL_VAL; }
set { _D_SPAL_VAL = value; }
}
}
(I would also like to encode it again later)
thank you!
You should specify the type of thing you want to deserialise, I don't think object will work.
List<MyClass> a = JsonConvert.DeserializeObject<List<MyClass>>("[{some json}]")
Sorry I cannot put comments yet
First,you have some type mismatch between your data and your convert class:
LAYER_SPALTE, D_SPAL_MIN and D_SPAL_MAX are null in your data.
put try putting your array inside another class
this code has to work for you:
public class MyClass
{
public SPALTEN[] SPALTEN { get; set; }
}
public class SPALTEN
{
public int NUMMER { get; set; }
public string NAME { get; set; }
public string TYP { get; set; }
public int LAENGE { get; set; }
public string EINHEIT { get; set; }
public bool EDITIERBAR { get; set; }
public bool OPTIONAL { get; set; }
public string LAYER { get; set; }
public int? LAYER_SPALTE { get; set; }
public string D_SPAL_NAME { get; set; }
public int? D_SPAL_MIN { get; set; }
public int? D_SPAL_MAX { get; set; }
public string D_SPAL_VAL { get; set; }
}
and then
MyClass a = JsonConvert.DeserializeObject<MyClass>(JSON_String)
To convert it on Internet, you can use the below URL for JSON to C#. I have been using from so long.
http://json2csharp.com/

Convert two classes in VB.net to C# but get object reference error

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
}
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:
http://www.developerfusion.com/tools/convert/csharp-to-vb/
http://codeconverter.sharpdevelop.net/SnippetConverter.aspx

Categories

Resources