How to check array for a value in C#? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to help someone out and this isn't my area of expertise so thought maybe someone can help me help someone else.
I have a field called Master that contains an array. I also have a field called Original that contains a string. I want to check if the string in Original is in the array field called Field1 and then with an if statement do something if true / false
"Original":"1234",
"Master":[{"ID":1,"Field1":12345},
{"ID":2,"Field1":123456},
{"ID":3,"Field1":1234},
{"ID":4,"Field1":12344}]
The array can be different each time and have a different amount of records in it.
Can anyone help me?

if Original and Master would be properties of the same class and in a variable named instance then you could use Linq to do:
bool isPresent = instance.Master.Any(entry => entry.Field1 == instance.Original);
Obviously you would need to first serialize the json to an instance of this class.

Related

Dealing with multiple booleans [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 days ago.
This post was edited and submitted for review 2 days ago.
Improve this question
I have a Customer data object. That object has to be evaluated by different functions. Each function will return true or false.
Since each function evaluate different case scenarios, I need to submit the Customer object to each one of them.
The way the functions were written, only one of them is going to return true.
(The functions are used in another part of the code, so I am using them as it is, so I don't have to repeat code).
The WorkStatus is an Enum. So, if the function returns true, then the Enum will be set with the specified value.
So:
WorkStatus result = 0;
if (notStarted) result = WorkStatus.NotStarted;
if (started) result = WorkStatus.Started;
if (inProgress) result = WorkStatus.InProgress;
if (withBacklog) result = WorkStatus.WithBacklog;
if (finalized) result = WorkStatus.Finalized;
return (int)result;
I made this way, but I am sure there is some better way. I just can't figure this out.
Obs.: I wrote the question again. I hope it's more clear. Sorry for the previous lack of details.

Returning a list of objects from a function to a text file C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a big function that returns a list of objects. I basically need the results to be put in to a text file. The list contains objects of a class with attributes string Index and integer count. I would want it to be written in a textfile like:
Index : Count fe.
BOOK_FROM_STORE : 27
Anybody has some guidance?
Parse the data held by your object to string and then write it to a text file.

C# ParentForm.ActiveControl.Name [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
if (Operators.CompareString(this.ParentForm.ActiveControl.Name, this.Name, false) == 0)
{
base.Focus();
}
From What I have experience in VB6,the above code doesn't work because they are never equal if the users didn't change to the same name.
From Example, the UserControl Name is ucCalendar, When I drag to my From, the name will automatically change to ucCalendar1,even though I can change to ucCalendar but usually we won't do that.
I think the coder want to compare whether the UserControl is the only control or ActiveControl on the Form so that he can force to focus it.
I don't know this C# works or not. Please tell me.
There is nothing in the WinForms code saying that two controls can not have the same name. The reason you think that is that you're looking at it from the designer perspective, when you use the designer it won't let you have two controls with the same name just because it uses there as field names for them in the code, and as you probably know there can not be two fields / properties / variables with the same name in the same scope. As a matter of fact there is no need for the Control's Name property to be anything.

Access part of the list in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this two list here:
List<KeyValuePair<TextBox, KeyValuePair<string, Type>>> textbox1
So, i need that to get the Textbox i need to write:
textbox1.Key
What should i type to get the KeyValuePair<string, Type>> Type, like textbox1.Value.Value?
The type contains string or int.What i need is to access it's value so i can assign an if operator but i don't know how to. Then i need to modify it but that's my next step and i can arrange that myself.
I suppose textbox1.Key won´t compile since textbox1 is a List and therefor does not have any member called Key. Having said this the following may work for you:
textbox[i].Value.Value
Where i is the index of your KV-pair within the list (rather then the actual key)

Error -input string was not in a correct format [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I don't understand what is wrong i did the same code with the insert and run well
Entity obj = new Entity();
.
.
.
obj.DEPID = decimal.Parse(((TextBox)GridView1.FooterRow.FindControl("txtDEPID")).Text);
myFactory.UpdateObject(obj);
The value that is returned by the
((TextBox)GridView1.FooterRow.FindControl("txtDEPID")).Text
Is not a decimal-like string. It contains some other Special Characters like alphabets etc.
While passing the values, you need to make sure that the values being passed match the values required. Check what is the value of this.
txtDEPID.Text;
Use it in a MessageBox, to check for the value. I'm sure there is some sort of non decimal part in the string. Which is causing the trouble while converting the String to Decimal.
MessageBox.Show(txtDEPID.Text);

Categories