Answered by:
unable to set value for property

Question
-
Hi all,
When i try to set the value for the property, Stack overflow error is occured,
public DataRow[] EscalationComments { get { return AllEscalationComments.Select("CommentType=4"); } set { EscalationComments = value; } }
what am i doing wrong in this ??
Tuesday, July 24, 2012 2:37 PM
Answers
-
- Proposed as answer by jtorrecilla Tuesday, July 24, 2012 3:33 PM
- Marked as answer by chezhian_in05 Tuesday, July 24, 2012 5:00 PM
Tuesday, July 24, 2012 3:30 PM -
As Sergio said,
You problem is that in the Setter You get the same variable name as your Property.
public DataRow[] EscalationComments { get { return AllEscalationComments.Select("CommentType=4"); } set { EscalationComments = value; } }
Replace for:
private DataRow[] _EscalationComments; public DataRow[] EscalationComments { get { return AllEscalationComments.Select("CommentType=4"); } set { _EscalationComments = value; } }
Regards!
Javier Torrecilla
Para el correcto funcionamiento, y que otros usuarios se puedan beneficiar de la solucion de esta pregunta por favor marca las respuestas que te hayan ayudado como "Respuesta".
Si la respuesta te ha sido util Votala.
Mi Blog: Jtorrecilla
Enlace a Faq de Winforms en Ingles Muy bueno
TabControl con Mejoras- Marked as answer by chezhian_in05 Tuesday, July 24, 2012 5:00 PM
Tuesday, July 24, 2012 3:34 PM -
Glad I'm not the only one who was confused by that.
Can you walk us logically through what you're trying to do? Not in code, but what is the actual goal you're trying to accomplish with your code? Maybe we can suggest a better way to get where you're trying to go. I think it's safe to say the code you have now is not correct. My first guess is still that a read-only property is what you want, but we can't really give you a lot of details without knowing what you ultimately want to happen.
- Marked as answer by chezhian_in05 Tuesday, July 24, 2012 5:00 PM
Tuesday, July 24, 2012 3:56 PM -
the problem occurs, only during setting the value for rows.
Sure it does.
Why you use the same name value to be set, and the propertyname?
Change on of them to:
private DataRow[] _rows; public DataRow[] EscalationComments { get { return AllEscalationComments.Select("CommentType=4"); } set { _rows = value; } }
But this still doesnt make any sence at all.
You set some object, and get another one.
Or, simple create a property with a Getter (remove setter), because its pointless. Else, you should convert the dataTable to Datarow array, which has no point.
Mitja
- Edited by Mitja Bonca Tuesday, July 24, 2012 4:06 PM
- Marked as answer by chezhian_in05 Tuesday, July 24, 2012 5:00 PM
Tuesday, July 24, 2012 4:00 PM
All replies
-
If you think logically about what you're doing, it makes sense. You have a 'set' handler that gets called every time someone sets EscalationComments equal to something. Inside it, you set EscalationComments equal to something - which is a problem!
In this case, it looks like you might want to just leave the set off. It doesn't seem to make any sense to set the EscalationComments, if your get method just returns a subset of another property. Just delete the whole set section and you should be good. If you need the ability to set that property, you may need to give us some more details on what you expect to happen. Should it overwrite AllEscalationComments?
Check out My Blog. Now updated to actually work!
Tuesday, July 24, 2012 2:46 PM -
Yes, I would like to overwrite the AllEscalationComments (FYI :AllEscalationComments is a DataTable, from which i'm selecting the comment type 4)
Tuesday, July 24, 2012 2:55 PM -
- Proposed as answer by jtorrecilla Tuesday, July 24, 2012 3:33 PM
- Marked as answer by chezhian_in05 Tuesday, July 24, 2012 5:00 PM
Tuesday, July 24, 2012 3:30 PM -
As Sergio said,
You problem is that in the Setter You get the same variable name as your Property.
public DataRow[] EscalationComments { get { return AllEscalationComments.Select("CommentType=4"); } set { EscalationComments = value; } }
Replace for:
private DataRow[] _EscalationComments; public DataRow[] EscalationComments { get { return AllEscalationComments.Select("CommentType=4"); } set { _EscalationComments = value; } }
Regards!
Javier Torrecilla
Para el correcto funcionamiento, y que otros usuarios se puedan beneficiar de la solucion de esta pregunta por favor marca las respuestas que te hayan ayudado como "Respuesta".
Si la respuesta te ha sido util Votala.
Mi Blog: Jtorrecilla
Enlace a Faq de Winforms en Ingles Muy bueno
TabControl con Mejoras- Marked as answer by chezhian_in05 Tuesday, July 24, 2012 5:00 PM
Tuesday, July 24, 2012 3:34 PM -
There sure will be an stackoverflow,
since oney you SEt with DataTable, and GET with DataRow array. This will just not do.
But I dont get it why you should Set it, you can only get it, if DataTable is a class variable. Here is an example of how you can do it:
private DataTable dt; void CreateAndFillDataTable() { dt = new DataTable(); //... } private void GetFilteredDataRowsArray() { DataRow[] rows = EscalationComments; } public DataRow[] EscalationComments { get { DataRow[] rows = dt.Select("CommentType=4"); return rows; } }
Mitja
- Proposed as answer by jtorrecilla Tuesday, July 24, 2012 3:40 PM
Tuesday, July 24, 2012 3:36 PM -
Yes, I would like to overwrite the AllEscalationComments (FYI :AllEscalationComments is a DataTable, from which i'm selecting the comment type 4)
That doesn't really make sense. You want a property which, through the getter, returns just a subset of the rows but, in the getter, sets every single row of the table?
I have to agree with Tim, it seems that this property makes sense as just a read-only property. If you want to be able to set the entire datatable then you should either have another property with a getter/setter that sets AllEscalationComments, or you should create a method that just sets AllEscalationComments. That said, it looks like AllEscalationComments is already a property, so I assume it has a setter? If so, just leave EscalationComments as read only and you'll be done.
Tuesday, July 24, 2012 3:51 PM -
the problem occurs, only during setting the value for rows.Tuesday, July 24, 2012 3:53 PM
-
Thanks for point the mistake of having the same property name in setter, let me change itTuesday, July 24, 2012 3:55 PM
-
Glad I'm not the only one who was confused by that.
Can you walk us logically through what you're trying to do? Not in code, but what is the actual goal you're trying to accomplish with your code? Maybe we can suggest a better way to get where you're trying to go. I think it's safe to say the code you have now is not correct. My first guess is still that a read-only property is what you want, but we can't really give you a lot of details without knowing what you ultimately want to happen.
- Marked as answer by chezhian_in05 Tuesday, July 24, 2012 5:00 PM
Tuesday, July 24, 2012 3:56 PM -
the problem occurs, only during setting the value for rows.
Sure it does.
Why you use the same name value to be set, and the propertyname?
Change on of them to:
private DataRow[] _rows; public DataRow[] EscalationComments { get { return AllEscalationComments.Select("CommentType=4"); } set { _rows = value; } }
But this still doesnt make any sence at all.
You set some object, and get another one.
Or, simple create a property with a Getter (remove setter), because its pointless. Else, you should convert the dataTable to Datarow array, which has no point.
Mitja
- Edited by Mitja Bonca Tuesday, July 24, 2012 4:06 PM
- Marked as answer by chezhian_in05 Tuesday, July 24, 2012 5:00 PM
Tuesday, July 24, 2012 4:00 PM -
Thanks Tim for your reply,
I'm trying to get the list of comments from a DataTable and at some point of time i like to update those retrieved datarows present in the DataTable,
Tuesday, July 24, 2012 4:50 PM -
Thanks Mitja,
I would like to set the value for the rows retrived in the Table
var x =AllEscalationComments.Select("CommentType=4"); // now i need to update the selected rows present in x with some values
Tuesday, July 24, 2012 4:53 PM -
now i need to update the selected rows present in x with some values
You have two options, to update a sinlge row, or to update all in a loop:
DataTable table = new DataTable(); table.Columns.Add("ID", typeof(int)); //add some example rows: table.Rows.Add(1); table.Rows.Add(2); table.Rows.Add(3); //1. update single row: table.Rows[0]["ID"] = 100;
//2. update all rows: //lets say we have these values to update rows: int[] newRows = { 101, 102, 103 }; for (int i = 0; i < table.Rows.Count; i++) { table.Rows[i]["ID"] = newRows[i]; }
NOTE: These are example data, all you have to do is to "convert" this code of mine to yours.
Mitja
Tuesday, July 24, 2012 5:29 PM -
Updating is actually easier than you would think. Classes in C# are passed by reference, which basically means when you change an instance of a class, it changes everywhere that instance is used.
So, for example, try something like this:
class MyTestClass { public string Label {get;set;} } void Main() { var instance1 = new MyTestClass(); instance1.Label = "Starting Label"; Console.WriteLine(instance1.Label); var instance2 = instance1; instance2.Label = "Changed label"; Console.WriteLine(instance1.Label); }
On the last Console.WriteLine, you'll notice that even though we changed the label property on instance2, it was updated for instance1 too. The same principle applies when you're updating items out of a collection. If you get an item out of a data table, and you change the value in one of the fields, the value will be updated on the original item in the list as well.
So, long story short, you probably don't actually need a setter. If you want to update the values in the original data table it's easy - get the item, and set the properties you want on that item. That's it, no need to set it back again.
Tuesday, July 24, 2012 5:52 PM