Define if statement for property 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 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);
}

Related

i would show a list<> in a windows form how i can do this ? with dataGridView? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am creating a form that displays all the elements of the list and can not figure out how to do it
class Prestiti : List<OggettoPrestito>
{
public new int IndexOf(OggettoPrestito item)
{
for (int i = 0; i < this.Count; i++)
{
if (this[i].NumeroPrestito == item.NumeroPrestito)
{
return i;
}
}
return -1;
}
public new void Add(OggettoPrestito item)
{
if (this.IndexOf(item) < 0)
{
base.Add(item);
}
else
{
throw new Exception("Prestito giĆ  inserito");
}
}
You can choose one of the following default controls:
ListBox, if the items can be represented by a single string (no columns)
ListView, if you want the list to look like the folder/files view
DataGridView, if you want a more excel-like view

C# Convert string to name of bool variable [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
I trying to make something like that:
// int counter; - this is changing in ther other parts of program
bool abc1; bool abc2; bool abc3; bool abc4;
if("abc" + counter == true)
{
//some code
}
Anyway, I need to convert string and int to bool name. How can I do this?
Use an array instead:
bool[] abc;
// ...
if (abc[counter] == true) {
{
// some code.
}

Getting the Values from Dictionary [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 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

How to allow average to be null [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
how do i allow this get to be null?
public double? GetSignalAverage
{
get
{
var signalaverage = Gsmdata.Select(x => x.SignalStrength)
.Average();
return Math.Round(signalaverage, 2);
}
}
SignalStrength is an Int
Check if the collection is null or empty:
public double? GetSignalAverage
{
get
{
if(Gsmdata == null || GsmData.Count() == 0)
return null;
var signalaverage = Gsmdata.Select(x => x.SignalStrength)
.Average();
return Math.Round(signalaverage, 2);
}
}
You'll have to declare SignalStrength as int? to allow null values. (If you don't, int will be its default value, which is 0.) But even then, Average() will simply ignore those values, if there is any non-null element (see the documentation).
If they all are null, Average will return null, which you can catch and return, like this:
double? signalaverage = Gsmdata.Select(x => x.SignalStrength)
.Average();
if(signalaverage == null)
{
return signalaverage; // which is essentially 'return null;'
}
else
{
return Math.Round(signalaverage, 2);
}

Cannot apply indexing with [] to an expression of type? [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 got this error
Error 3 Cannot apply indexing with [] to an expression of type
my problem here
if (client["banhours"] == 0)
{
client["banhours"] = -1;
client["banreason"] = "Infinite time.";
client["banstamp"] = DateTime.Now.AddYears(100);
}
if (Account.State == Database.AccountTable.AccountState.Banned)
{
if (client["banhours"] != -1)
{
DateTime banStamp = client["banstamp"];
if (DateTime.Now > banStamp.AddDays(((int)client["banhours"]) / 24).AddHours(((int)client["banhours"]) % 24))
Account.State = Database.AccountTable.AccountState.Player;
}
}
client is >>>
Client.GameClient client;
Have you tried client.banhours or client.banreason?
If Client.GameClient is a class and those are properties or fields, they must not be accessed like an array or dictionary.

Categories

Resources