Okay I have declared a Key CellX to boolean value True. In a sense as long as this value remains true my data will keep inserting in and If I turn it false it will stop at what ever count it was on. Now I have to Stop at when 400 orders have been inserted in data base and no more... can I actually apply a condition
like
Key CellX "True"
if CellX ==400 then "False"
else
"True"
can we accomplish this... I am doing in C#.
I C most of you have given good logic.
little more CellX is declared in web.xml along with other keys such as CellA, B, C and A, B, are receiving data in DB so to stop it from reaching 400 I can manually change the CellX Boolean Value or How can I implement a condition which will make my Boolean value to turn false when Cell A, B, C reach 400.
You can do something like
myBool = (CellX == 400);
However, in your post you refer to CellX as both a boolean and as something that can be compared to an integer value of 400. A given variable can be boolean or an integer.
If CellX is your boolean, instead do something like:
CellX = (myLoopCounter == 400);
The syntax
myBool = (myLoopCounter == 400);
evaluates like this:
Evaluate whether myLoopCounter is exactly 400 (true, or false)
Assign the result of the previous step to myBool
A variable can't be dynamically evaluated like that (at least not in the way I think you're looking for, things like Func notwithstanding). But you can create it as a read-only property on the class which would be evaluated each time (since a property is mainly a syntactically different form of a method call):
private bool LimitReached
{
get
{
return (CellX == 400);
}
}
So if you keep checking the value of LimitReached over and over, it should potentially change if the value of CellX is also changing.
Is this what you're trying to do?
int Counter = 1;
bool Continue = true;
while(Continue)
{
// Insert one row
if (Counter == 400)
Continue = false;
Counter++;
}
This can also be written:
for(int i = 0; i < 400; i++)
{
// insert one row
}
Related
Please forgive me the noob question, but C# is not my native language. In code I took over, I stumbled upon
var success = true;
success = Upload.Status == FileStatus.Ok;
In there, FileStatus is of type enum, somewhere custom defined. I guess, success is essentially non-nullable Boolean. I think that the 2nd line sets success to false if (and only if) Upload.Status == FileStatus.Ok, which would also induce that the latter two variables should be of same type.
Could you please let me know, whether my hypotheses are correct? Also: How is such a construct called? What is it short for?
success is going to be a bool.
It is initialized with true, but that should be unessesary.
var success = true;
And will then be set to the result of (Upload.Status == FileStatus.Ok) which is a bool.
success = Upload.Status == FileStatus.Ok;
Think of it as success = (Upload.Status == FileStatus.Ok); if that helps.
And yes, it took me a moment to parse it too. I have a profound dislike for people trying to save lines at the cost of readability. I would use this:
if(Upload.Status == FileStatus.Ok)
success = true;
else
success = false;
Maybe use the shorter if syntax.
Upload.Status == FileStatus.Ok evaluates to a bool, either true or false. success will be assigned the result of that evaluation.
It's no more mystical than var sum = 4 + 5; resulting in sum being assigned a value of 9.
It's assigning the result of an expression to a variable, same as var x = 1 + 2; just that in this case the expression is of type bool.
Needlessly verbose, this would be the same:
bool success;
if (Upload.Status == FileStatus.Ok)
{
success = true;
}
else
{
success = false;
}
protected IteratorAdvance<int> Advance;
public delegate void IteratorAdvance<T2>(out T2 itemIndex);
public IteratorBase()
{
Advance = delegate(out int i) { i++; }; // <--- here
}
Above is a snippet of a class that is a wrapper for a number of types of loop. In the constructor overload below, the instantiator can specify the method by which the condition of continuing a for loop is determined and also how the control value of that loop is modified on each iteration.
Obviously, the simplest and most likely use of this is to start at 0 and increment by 1 until the last index of the data source is reached. To that end I've provided default methods and an initial value that are effectively (and in this case a very convoluted version of)
for(var i = 0; i < list.Count; i++)
Here's the constructor:
public IteratorBase(IterationStyle style, Func<int, T1, bool> complete, IteratorAdvance<int> advance, bool onErrorAbort = true, int initialValue = 0)
{
Style = style;
InitialValue = initialValue;
Complete = complete;
Advance = advance;
AbortOnError = onErrorAbort;
}
The problem is where I'm incrementing the control value, the compiler wants a default value for a variable (i) that is declared in that scope and because it is an out parameter, cannot accept a default value.
Even if I move the body of the function to a separate method, the same problem exists. I cannot initialise the out parameter prior to the for loop itself initialising it.
case IterationStyle.For:
for (controlValue = InitialValue; !Complete(controlValue, item); Advance(out controlValue))
{
action.TargetItem = item = dataSource[controlValue];
if (!ActWrapper(action))
return false;
}
break;
The only solution I can think of is to intialise the out parameter only on the first call of the method, which will involve using some kind of boolean external to the method. I don't think I'm alone in preferring to avoid that sort of thing. And it turns out it that doesn't work either so I would have to make the control value global. Over my dead body.
Usually, in this sort of situation it means your approach is wrong. But I can't think of another way. I'm hoping you can. Or else tell me how I can either initialise an out parameter or give it a default value.
Just in case anyone else is having a bad brain day, here's the solution. Use ref not out.
Advance = delegate(ref int i) { i++; };
I am new to C# (and programming in general) so I am unsure how to ask this using proper terms (please bear with me):
What I am trying to do is, if a set of conditions are true, I want to create a new value that is true (so it equals 1). A new set of conditions later in the code will contain the new value as a variable (along with a number of other new conditions). Basically it will look like this:
// Condition set 1
if (Position.GetProfitLoss(Close[0], PerformanceUnit.Percent) < -0.015)
{
//this section will have a newly created value ('variable1')
//that if true will equal 1, and if false will equal 0
}
in the next condition set, the previously created variable will be a part of the decision:
// Condition set 2
if (RVI>50)
&& variable1=1
{
Buy100Shares
}
How do I define the first variable?
You define the first variable as a boolean. C# offers you the bool type:
bool meetsThreshold = (Position.GetProfitLoss(Close[0], PerformanceUnit.Percent) < -0.015);
After this line of code has executed, meetsThreshold will either be true or false.
You can then use this variable in later predicates:
if ((RVI>50) && meetsThreshold)
{
Buy100Shares
}
I have this code. What does it mean?
bool q = false;
if (i < 0) {
q = !q;
}
I assume !q means true?
UPDATE: The full code is below. When ! is used in the IF statement, is the variable in that situation always false?
bool q = false;
if (i < 0) {
q = !q;
}
if (!q) {
/// do stuff
}
All it means is it's "not q", so it's opposite of whatever q is.
In the case of a boolean like here, the variable q can be either true or false.
when you put a ! in front of something in most languages, it means "opposite"
As example,
1 != 2
means:
1 is opposite of equal to 2
.
This being for a condition, in your case, it would mean
assign the opposite of q to q.
Also, I believe most people on SO (Stack Overflow) will tell you this question does not belong here because you can find it easily on the internet, if you want, there are various books to learn programming. You can search "it ebooks" on the internet and you will probably find many for free .
As the other poster said, it switches the bool property from true to false. In your example, q starts as false. If i is less than 0 then q becomes true. Then comes the if statement, which says "if q is false, then execute the next code block". q only stays false if i is greater than or equal to 0. So no, the code inside your if block will not always execute. It depends on i.
I have written a method in c# to get a count of a table, and saves the count in settings properties.
public static bool CompareCount(int? currentCount)
{
Properties.Settings.Default.Count = (int)currentCount;
Properties.Settings.Default.Save();
if (currentCount < Properties.Settings.Default.Count)
{
return false;
}
else
{
return true;
}
}
At first time if the count returned is 20. I will save it in the settings and I shud not compare that wiht the previous count. ON second time I want to compare the current count wiht the previoulsy saved count in settings. The above method should assign the current count for the first time . but on second time it shud compare.
thanks in advance.
First of all, think about what would happen when you cast the int? that's coming in to int if the parameter is null. It doesn't make sense to use a nullable parameter if you don't do anything with it later on. You should either change the parameter type to int or you could do this:
Properties.Settings.Default.Count = currentCount ?? 0;
Then, the method will always return true, as the if condition is always false - remember you're setting Properties.Settings.Default.Count to currentCount just two lines above that? So how should it ever be larger than currentCount?
You need to define for yourself how to determine "the first time" and "the second time". What's the condition to find out whether the method is run for the first time? For the code below I'll assume that there's some default value for Properties.Settings.Default.Count that helps you determine whether the method runs for the first time.
Then, from what you say, your code should look like this:
public static bool CompareCount(int? currentCount)
{
int countValue = currentCount ?? 0;
if (Properties.Settings.Default.Count == <some default value>)
{
Properties.Settings.Default.Count = (int)currentCount;
Properties.Settings.Default.Save();
}
else
{
return currentCount >= Properties.Settings.Default.Count;
}
}
What is your problem in implementing it? You have all the blocks already here at hand. Just reorder them properly.
If the problem is that the "int Count" defined in settings is "0" by default, you can change it i.e. to default to -1 so it obviously will be NOT a Count written before. Or, you can change it to int? to have default null..
Ask the current found Count to check if it is equal to the default value (zero, or not found or set your own once not found let say -1), so once not found you do not compare otherwise compare the values.
For Example:
public static bool CompareCount(int? currentCount)
{
int foundCount = ReadFoundCountFromProperties;
if (foundCount != 0)
{
Properties.Settings.Default.Count = (int)currentCount;
Properties.Settings.Default.Save();
if (currentCount < foundCount)
return false;
return true;
}