asp.net c# class properties stackoverflow exception - c#

I have a very simple C# class:
namespace mybox
{
public class userAccount : IMyInterface
{
public userAccount()
{
}
private int _userId;
private string _userName;
public int userId
{
get { return _userId; }
set { userId = value; }
}
public string userName
{
get { return _userName; }
set { userName = value; }
}
public string list(int myUserId)
{
...
myOutPut = string.Format("{0} {1} {2}", u.userId, u.userName);
return myOutPut.ToString();
}
public void add()
{
pillboxDataContext db = new pillboxDataContext();
userAccount newUser = new userAccount();
newUser.userName = "test123";
db.SubmitChanges();
}
}
}
In my default.aspx.cs in the Page_Load event I'm trying to call the list method:
protected void Page_Load(object sender, EventArgs e)
{
pillbox.userAccount myUA = new pillbox.userAccount();
myUA.add();
// Console.WriteLine(myUA.list(1));
}
When I call the add method I can see that it is trying to assign the value test123 to the property but I get the following message:
An unhandled exception of type 'System.StackOverflowException' occurred in App_Code.1zm0trtk.dll
Any ideas of what I'm doing incorrectly?

The problem is with the way you defined your properties.
You are trying to refer to the property to which you are assigning the value in the setter
which is resulting in an infinte recursion (to be specific this line is triggering it newUser.userName = "test123";).
Change them to:
public int userId
{
get { return _userId; }
set { _userId = value; }
}
public string userName
{
get { return _userName; }
set { _userName = value; }
}

It is because userName calls itself. You probably meant to assign the field:
This line is wrong:
set { userName = value; }
You meant to write:
set { _userName = value; }

You need to set the private backing field, not the property. Otherwise you are just going into infinite recursion as you call set on yourself the entire time.
private int _userId;
private string _userName;
public int userId
{
get { return _userId; }
set { _userId = value; }
}
public string userName
{
get { return _userName; }
set { _userName = value; }
}
In your case, you could just use auto implemented properties (I changed the casing to match the guidelines):
public int UserId { get; set; }
public string UserName { get; set; }

If you re-write your setters a bit, it's easy to see what's happening. The get and set on a property are actually compiled down to methods (PropType get_PropName() and void set_PropName(PropType value)), and any references to the property are compiled to calls to the appropriate method. So this code:
int i = myObj.MyIntProp;
myObj.MyIntProp = 6;
compiles to
int i = myObj.get_MyIntProp();
myObj.set_MyIntProp(6);
So your setter
set
{
username = value;
}
actually compiles to
public void set_username(string value)
{
set_username(value);
}
And now the cause of the stack overflow is obvious.

Related

c# error Inconsistent accessibility: parameter type 'HRDMSV1.User' is less accessible than method

I am having a problem with an error...
Error 1 Inconsistent accessibility: parameter type 'HRDMSV1.User' is less accessible than method 'HRDMSV1.FrmAddDoc.FrmAddDoc(HRDMSV1.User)'
All help appreciated?
namespace HRDMSV1
{
public partial class FrmAddDoc : Form
{
User _user;
private ConnStr connStr = new ConnStr();
public FrmAddDoc(User user) /* error here */
{
InitializeComponent();
_user = user;
}
/*...*/
}
class User
{
private String _userName;
private String _password;
private bool _readOnly;
private int _userID;
public String userName {
get { return _userName; }
set { _userName = value; }
}
public String password {
get { return _password; }
set { _password = value; }
}
public bool readOnly {
get { return _readOnly; }
set { _readOnly = value; }
}
public int userID {
get { return _userID; }
set { _userID = value; }
}
}
}
Your User class is less accesable than the public constructor FrmAddDoc which is not allowed. For more reference see CS0051

Neo4jClient Node/Relationship Class conventions

Is there a standard naming convention for the properties/methods of a node/relationship class when working with Neo4jClient?
I'm following this link Neo4jClient - Retrieving relationship from Cypher query to create my relationship class
However, there are certain properties of my relationship which i can't get any value despite the relationship having it. While debugging my code, i realized certain properties was not retrieved from the relationship when creating the relationship object.
this is my relationship class
public class Creates
{
private string _raw;
private int _sourcePort;
private string _image;
private int _DestinationPort;
private int _eventcode;
private string _name;
private string _src_ip;
private int _src_port;
private string _dvc;
private int _signature_ID;
private string _dest_ip;
private string _computer;
private string _sourceType;
private int _recordID;
private int _processID;
private DateTime _time;
private int _dest_port;
public string Raw { get { return _raw; } set { _raw = value; } }
public int SourcePort { get { return _sourcePort; } set { _sourcePort = value; } }
public string Image { get { return _image; } set { _image = value; } }
public int DestinationPort { get { return _DestinationPort; } set { _DestinationPort = value; } }
public int Eventcode { get { return _eventcode; } set { _eventcode = value; } }
public string Name { get { return _name; } set { _name = value; } }
public string Src_ip { get { return _src_ip; } set { _src_ip = value; } }
public int Src_port { get { return _src_port; } set { _src_port = value; } }
public string DVC { get { return _dvc; } set { _dvc = value; } }
public int Signature_ID { get { return _signature_ID; } set { _signature_ID = value; } }
public string Dest_ip { get { return _dest_ip; } set { _dest_ip = value; } }
public string Computer { get { return _computer; } set { _computer = value; } }
public string SourceType { get { return _sourceType; } set { _sourceType = value; } }
public int RecordID { get { return _recordID; } set { _recordID = value; } }
public int ProcessID { get { return _processID; } set { _processID = value; } }
public DateTime Indextime { get { return _time; } set { _time = value; } }
public int Dest_port { get { return _dest_port; } set { _dest_port = value; } }
}
This is another class
public class ProcessConnectedIP
{
public Neo4jClient.RelationshipInstance<Pivot> bindto { get; set; }
public Neo4jClient.Node<LogEvent> bindip { get; set; }
public Neo4jClient.RelationshipInstance<Pivot> connectto { get; set; }
public Neo4jClient.Node<LogEvent> connectip { get; set; }
}
This is my neo4jclient query to get the relationship object
public IEnumerable<ProcessConnectedIP> GetConnectedIPs(string nodeName)
{
try
{
var result =
this.client.Cypher.Match("(sourceNode:Process{name:{nameParam}})-[b:Bind_IP]->(bind:IP_Address)-[c:Connect_IP]->(connect:IP_Address)")
.WithParam("nameParam", nodeName)
.Where("b.dest_ip = c.dest_ip")
.AndWhere("c.Image=~{imageParam}")
.WithParam("imageParam", $".*" + nodeName + ".*")
.Return((b, bind, c, connect) => new ProcessConnectedIP
{
bindto = b.As<RelationshipInstance<Creates>>(),
bindip = bind.As<Node<LogEvent>>(),
connectto = c.As<RelationshipInstance<Creates>>(),
connectip = connect.As<Node<LogEvent>>()
})
.Results;
return result;
}catch(Exception ex)
{
Console.WriteLine("GetConnectedIPs: Error Msg: " + ex.Message);
return null;
}
}
This is the method to read the results
public void MyMethod(string name)
{
IEnumerable<ProcessConnectedIP> result = clientDAL.GetConnectedIPs(name);
if(result != null)
{
var results = result.ToList();
Console.WriteLine(results.Count());
foreach (ProcessConnectedIP item in results)
{
Console.WriteLine(item.Data.Src_ip);
Console.WriteLine(item.bindto.StartNodeReference.Id);
Console.WriteLine(item.bindto.EndNodeReference.Id);
Console.WriteLine(item.connectto.StartNodeReference.Id);
Console.WriteLine(item.connectto.EndNodeReference.Id);
Node<LogEvent> ans = item.bindip;
LogEvent log = ans.Data;
Console.WriteLine(log.Name);
Node<LogEvent> ans1 = item.connectip;
LogEvent log1 = ans1.Data;
Console.WriteLine(log1.Name);
}
}
}
Somehow, i'm only able to populate the relationship object with src_ip/src_port/dest_ip/dest_port values. the rest are empty.
Is there any possible reason why? I've played with upper/lower cases on the properties names but it does not seem to work.
This is the section of the graph im working with
This is the relationship properties sample:
_raw: Some XML dataSourcePort: 49767Image: C:\Windows\explorer.exeDestinationPort: 443EventCode: 3Name: Bind
IPsrc_ip: 172.10.10.104dvc: COMPUTER-NAMEsrc_port:
49767signature_id: 3dest_ip: 172.10.10.11Computer:
COMPUTRE-NAME_sourcetype:
XmlWinEventLog:Microsoft-Windows-Sysmon/OperationalRecordID:
13405621ProcessId: 7184_time: 2017-08-28T15:15:39+08:00dest_port: 443
I'm not entirely sure how your Creates class is ever populated, in particular those fields - as your Src_port property doesn't match the src_port in the sample you provided (case wise).
I think it's probably best to go back to a super simple version. Neo4jClient will map your properties to the properties in the Relationship as long as they have the same name (and it is case-sensitive).
So start with a new Creates class (and use auto properties - it'll make your life a lot easier!)
public class Creates
{
public string Computer { get; set; }
}
Run your query with that and see if you get a result, then keep on adding properties that match the name and type you expect to get back (int, string etc)
It seems that i have to give neo4j node/relationship property names in lowercase and without special characters at the start of the property name, in order for the above codes to work.
The graph was not created by me at the start thus i had to work on it with what was given. I had to get the developer who created the graph to create the nodes with lowercases in order for the above to work.

AutoMapper Ignore() Issue

Okay, I'm hoping I am just somehow overlooking the obvious. I have the following code situation below. For some reason, the SequenceNo property is still getting mapped even though I'm calling Ignore(). I'm using the latest. I also had tested it with two different classes in the same project and it seemed to work, so what's wrong with this scenario then?
This is the domain object:
public class CableID
{
private string _panelID1;
public string PanelID1
{
get { return _panelID1; }
private set { _panelID1 = value; }
}
private string _panelID2;
public string PanelID2
{
get { return _panelID2; }
private set { _panelID2 = value; }
}
private int _sequenceNo;
public int SequenceNo
{
get { return _sequenceNo; }
private set { _sequenceNo = value; }
}
private DateTime _inService;
public DateTime InService
{
get { return _inService; }
set { _inService = value; }
}
private string _id;
public string ID
{
get { return _id; }
private set { _id = value; }
}
public CableID(string panelID1, string panelID2, int sequenceNo)
{
this.PanelID1 = panelID1;
this.PanelID2 = panelID2;
this.SequenceNo = sequenceNo;
this.ID = string.Format("({0}-{1}){2}", this.PanelID1, this.PanelID2, this.SequenceNo);
}
public CableID(string id)
{
if (string.IsNullOrEmpty(id))
throw new ArgumentNullException("id");
this.ID = id;
}
}
And here is the DTO Object:
public class CableIDDTO
{
private string _panelID1;
public string PanelID1
{
get { return _panelID1; }
set { _panelID1 = value; }
}
private string _panelID2;
public string PanelID2
{
get { return _panelID2; }
set { _panelID2 = value; }
}
private int _sequenceNo;
public int SequenceNo
{
get { return _sequenceNo; }
set { _sequenceNo = value; }
}
private string _id;
public string ID
{
get { return _id; }
set { _id = value; }
}
public CableIDDTO()
{ }
public CableIDDTO(string panelID1, string panelID2, int sequenceNo)
{
this.PanelID2 = panelID1;
this.PanelID1 = panelID2;
this.SequenceNo = sequenceNo;
this.ID = string.Format("({0}-{1}){2}", this.PanelID2, this.PanelID1, this.SequenceNo);
}
}
And finally the AutoMapper use-case:
CableID cableID = new CableID("A1", "B1", 2);
Mapper.CreateMap<CableID, CableIDDTO>()
.ForMember(dest => dest.SequenceNo, opt => opt.Ignore());
CableIDDTO dto = Mapper.Map<CableID, CableIDDTO>(cableID);
dto.SequenceNo = 2 when since I had set the Ignore() it should be 0.
This is because AutoMapper is finding this CableIDDTO constructor:
public CableIDDTO(string panelID1, string panelID2, int sequenceNo)
and calling it, setting sequenceNo. I'm not exactly sure how or why it's doing that--i'll continue to dig.
You can fix this by calling .ConstructUsing and telling AutoMapper to use the no-args constructor:
Mapper.CreateMap<CableID, CableIDDTO>()
.ConstructUsing((Func<CableID, CableIDDTO>)(src => new CableIDDTO()))
.ForMember(dest => dest.SequenceNo, opt => opt.Ignore());
Upon further research, this looks like a feature in AutoMapper that tries to match up source property names with destination constructors. Since your destination type (CableIDDTO) had a constructor that perfectly matched up with several property names on the source (panelID1, panelID2, sequenceNo), that constructor was called.
Another way to disable this feature is to call DisableConstructorMapping:
Mapper.Configuration.DisableConstructorMapping()

MVVM: Raise PropertyChanged event for a DataContract class members

I want to raise PropertyChanged event for a model with DataContract.
Initially I did this
[DataContract]
public partial class User : INotifyPropertyChanged
{
[DataMember(Name="username")]
public string Username
{
get
{
return this.Username;
}
set
{
this.Username = value;
RaisePropertyChanged("Username");
}
}
}
which gave StackOverflow Exception beacause of Infinite Recursion.
So the solution I come up with is
[DataContract]
public partial class User : INotifyPropertyChanged
{
private string _Username { get; set; }
[DataMember(Name="username")]
public string Username
{
get
{
return this._Username;
}
set
{
this._Username = value;
RaisePropertyChanged("Username");
}
}
}
Although this reflects the Username value to the control binding to "Username", this doesn't look the best way to me. Something is wrong. Also my model has approx 30-40 fields. Is this the right approach or can someone please suggest me a better way.
Thanks
I'd be so tempted to use caller-member-name here (if it is in your target framework):
private string _username;
[DataMember(Name="username")]
public string Username
{
get { return _username; }
set { SetField(ref _username, value); }
}
private void SetField<T>(ref T field, T value,
[CallerMemberName] string memberName = null)
{
if(!EqualityComparer<T>.Default.Equals(field,value))
{
field = value;
RaisePropertyChanged(memberName);
}
}
If caller-member-name isn't supported:
[DataMember(Name="username")]
public string Username
{
get { return this._Username; }
set { SetField(ref _Username, value, "Username"); }
}
[DataContract]
public partial class User : INotifyPropertyChanged
{
private string _Username;
[DataMember(Name="username")]
public string Username
{
get
{
return this._Username;
}
set
{
if(this._Username != value)
{
this._Username = value;
RaisePropertyChanged("Username");
}
}
}
}

System.StackOverflowException was unhandled

Please help, I'm confused I do not know why the error logs
System.StackOverflowException was unhandled.
I keep getting an error on set LekID.
How would I that fix?
Here is the code:
public Lager(long lekID, string lek, string proizvojdac, int kolicina, double cena)
{
LekID = lekID;
Lek = lek;
Proizvodjac = proizvojdac;
Kolicina = kolicina;
Cena = cena;
}
public long LekID
{
get { return LekID; }
set { LekID = value; }
}
public string Lek
{
get { return Lek; }
set { Lek = value; }
}
public string Proizvodjac
{
get { return Proizvodjac; }
set { Proizvodjac = value; }
}
public int Kolicina
{
get { return Kolicina; }
set { Kolicina = value; }
}
public double Cena
{
get { return Cena; }
set { Cena = value; }
}
public long LekID
{
get { return LekID; }
set { LekID = value; }
}
This (and the other properties) cause a StackOverflowException, since you are assigning value to LekID over and over again.
You should add a field to the property and store the value there:
private long _lekID;
public long LekID
{
get { return _lekID; }
set { _lekID = value; }
}
You should give different names to your private variables and to your properties. Otherwise, your property is calling itself when you access it.
Example:
long _lekID;
public long LekID
{
get { return _lekID; }
set { _lekID = value; }
}
Or simply:
public long LekID { get; set; }
The properties are calling themself. Try changing your properties like this:
public string Lek
{
get;
set;
}
You're calling the Lek property recursively in both the setter and the getter
Either introduce a backing field:
private string lek;
public string Lek
{
get { return this.lek; }
set { this.lek = value; }
}
or use an Automatic Property:
public string Lek
{
get; set;
}
Marking this community wiki as it is only an aside, but none of this would have happened if you'd been sufficiently lazy (that is often a virtue in programming, not a vice):
public long LekID {get;set;}
public string Lek {get;set;}
public string Proizvodjac {get;set;}
public int Kolicina {get;set;}
public double Cena {get;set;}
less typing; no errors; and you've correctly exposed the API as properties so you can add validation / side-effects later if you need, and it'll work with binding APIs (which don't usually love fields).
Try using code snippet like prop/ propfull,
the snippets will create the properties code automaticly

Categories

Resources