Getting the Values from Dictionary [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 8 years ago.
Improve this question
I am going Round-Robin Algorithm. I am doing in that everything must be created dynamically and Randomly. When I want to go second time to check if I have and value the is more than 0 in the checking point it says that "The given key was not present in the dictionary". How can I Fix this problem.
private int GetNextNodeToProcesssRandom()
{
int NextNodeIndex = -1;
if (NextNodeToProcess >= this.waytosave.Count)
{
NextNodeToProcess = 0;
}
for (int i = NextNodeToProcess; i < this.waytosave.Count; i++)
{
if (this.waytosave[i] > 0)//the problem appears here when the cod goes for the second time.
{
NextNodeIndex = i;
break;
}
}
NextNodeToProcess++;
return NextNodeIndex;
}

It is somehow unclear what is your exact goal. However if you want to loop through a Dict you can use:
foreach(KeyValuePair<TKey, TValue> entry in MyDict)
{
// do something with entry.Value or entry.Key
}
Now if you want to add a check in your program you can try:
if (MyDict.ContainsKey(this.waytosave[i]))
///continue execution...
A look here might help you

Related

How can I change the value returned in a C# get, set? [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 2 years ago.
Improve this question
I have this code that gets the value of a setting:
public static BTN Btns {
get => (BTN)App.DB.GetSet(SET.Btns, 2, typeof(BTN));
set => App.DB.UpdSet(SET.Btns, (int)value);
}
How can I change this so that if the value returned from GetSet is 1, then the value returned from the get will be 1 and there will be a call to App.DB.UpdSet(SET.Btns, 2) issued to change it in the database to a 2?
Can I use {} or something like that after the => ?
Firstly, if your property value is going to change after every get, you should perhaps change it to a method call - keeps things more explicit.
Secondly, you can achieve what you want by replacing your expression bodied get with a regular get, like so:
public static BTN Btns
{
get
{
var value = (BTN)App.DB.GetSet(SET.Btns, 2, typeof(BTN));
if ((int)value == 1)
{
App.DB.UpdSet(SET.Btns, (int)value + 1);
}
return value;
}
set
{
App.DB.UpdSet(SET.Btns, (int)value);
}
}

Define if statement for property 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 4 years ago.
Improve this question
public class hamid
{
private int[] arr = new int[10];
public int[] Arr
{
get => arr;
set
{
if (value < 0)
Environment.Exit(1);
else arr = value;
}
} // If in error.
}
I want have a if statement. For example if (values < 0)
But I have an error, please help me.
value is an array of integer, it can't be zero. Any element in the array could be zero. If you are trying to determine if the array is null, then you could change the check to be:
if (value == null)
{
Environment.Exit(1);
}

I want my if loop to do nothing if certain conditions are met [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 6 years ago.
Improve this question
i want my if loop to do nothing when the conditions that i gave it are there
i'm new to C# and winform so i searched the internet but didnt find an answer that seems to work and right now i have no idea what to do.
,Mo
screenshot of the loop
If I understand your question correctly, you want to "cancel" all operations in the current method, right? You can use return; to do that:
if(value2 == null) return;
There is just one other thing wrong with your code: value2 will never be null.
decimal value2;
if(!decimal.TryParse(result[1], out value2)) return;
should work a lot better ;)
I can't see any loop in the code provided. You want to change Parse to TryParse and use return in order to return from the method (== do nothing):
public void button14_Click(Object sender, EventArgs e) {
string[] result = input1.Text.Split(Oprator);
//TODO: it may appear, that you want TryParse here as well
decimal value1 = decimal.Parse(result[0]);
decimal value2;
// If you have too few items, and thus you have no "value2" - do nothing
if (result.Length < 2)
return;
// Try parse result[1] to decimal; if parse fails (e.g. result[1] == "zzz") - do nothing
if (!decimal.TryParse(result[1], out value2))
return;
// you have value1, value2, Oprator; put required logic here
switch (Oprator) {
...
}
}
Have you thought about a "While" loop?
int n = 1;
while (n < 6)
{
Console.WriteLine("Current value of n is {0}", n);
n++;
}

C# Defining the size of an array contained in an arraylist [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 6 years ago.
Improve this question
I define the arraylist on my form2, sending it using the constructor to form 3, where it is filled. However, I want the internal array's size to be user defined.
How would i go about doing this?
Looks like this for now, but it doesn't work.
private void bCapturar2_Click(object sender, EventArgs e)
{
int k=0;
k=int.Parse(textBox1.Text);
((paciente)Datos[i]).num_asist = k;
lAsistentes.Visible = true;
tbNom_Asist.Visible = true;
((paciente)Datos[i]).asistentes = new string[((paciente)Datos[i]).num_asist];
bCapturar2.Visible = false;
}
You can set the capacity for your ArrayList on declaration
var tenItemArrayList = new ArrayList(10);
If asistentes is an ArrayList, you still can change the value for capacity that way...
((paciente)Datos[i]).asistentes.Capacity = ((paciente)Datos[i]).num_asist;
However the new capacity cannot be less than the current. Otherwise you'll get an exception.

How to loop through a multidimensional list [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 7 years ago.
Improve this question
Must be a simple question, and I'm again close to a nervous breakdown because I can't find it:
I have a multimensional List that I defined as an own class (Id, Title, Desc, Start, Length, URL) and that I filled in one function
hyperlist.Add(
new ListElement
{
Id = n,
Title = title,
Desc = desc,
Start = OffsetTotal,
Length = TagLength,
URL = LinkURL
});
I pass it to another function where I have to loop through it and compare each entry of the list to a parameter.
void BuildGList(List<ListElement> LinkList)
{
int startIndex = 5;
foreach (int Id in LinkList)
{
if(startIndex < Start)
{
....
}
}
}
I don't see how to address each single column and googling it I get the impression that nobody uses lists to do what I want here.
-update-
I am asked to clarify my question. Lucky enough it had been clear to the ones that answered it: I didn't know how to refer to a special parameter in a List. Now I know that you can do it with item.parameter.I'm really grateful for the help received in Stackoverflow but sometimes I get the impression that many of you experienced coders have little empathy and understanding for the problems a beginner faces and the effort it takes to google through a jungle of posts. Especially if you are a beginner and therefore sometimes miss the correct keywords. On this one I was busy for an hour and close to a breakdown as I knew I was catching really simple. If you know it then it's always easy. Cheers
You can use foreach like this:
foreach (ListElement item in LinkList)
{
if (item.Length < startIndex)
{
//Do something
}
}
You can filter the list using Linq e.g. to return an IEnumerable as the subset you could do:
private IEnumerable<ListElement> BuildGList(List<ListElement> linkList)
{
int startIndex = 5;
return linkList.Where(element => startIndex < element.Start);
}
You can use takewhile with a foreach if you want to use the list index:
foreach(var item in LinkList.TakeWhile((item, index) => index < startIndex))
{
//enter your code here
}
Also, if you want to compare with a element value inside the list, you can use where with the foreach:
foreach(var item in LinkList.Where(item => item.Start < startIndex))
{
//enter your code here
}

Categories

Resources