Problem with C# conditional [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Hi I have the following code:
medianListTester.cfyPE = 0;
if (medianListTester.cfyPE != 0 || testStock.getCEPS() != 0)
{
medianCYPE_price = medianListTester.cfyPE * testStock.getCEPS();
counter++;
}
else
//do something else
but it doesn't seem to "do something else" and still fires the code inside the conditional. Am I doing something wrong here? Why won't it run the code in the if statement?

If testStock.getCEPS() is not 0 you will not hit the else block. Depending on what you need and what you are expecting you might need to use && instead of || ?

medianListTester.cfyPE is set to 0, so the first clause in your if statement evaluates to false. What does TestStock.getCEPS()? If that is non-zero, then that will evaluate to true, and the entire if statement is then true.

I can't really tell what you're asking, but you're calling getCEPS() twice, and perhaps that is returning two different values, and perhaps that is causing your trouble.
Try this instead:
medianListTester.cfyPE = 0;
var ceps = testStock.getCEPS();
if (medianListTester.cfyPE != 0 || ceps != 0)
{
medianCYPE_price = medianListTester.cfyPE * ceps;
counter++;
}
else
//do something else

It sounds like testStock.getCEPS() is returning something other than 0. That would explain why it never hits do something else. Have you checked what this is returning?

What's the value of the call to testStock.getCEPS()? If that's not 0 it will still enter that code block. Also, I don't know the details of the object referenced by medianListTester, but it is possible that there is a getter/setter that isn't working right.
Your best bet for solving this is to print out or somehow look at the values of both medianListTester.cfyPE and testStock.getCEPS() right after the assignment but just before the if statement.

Related

Substring compiles but code doesn't work [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
When I use the code below, it compiles but the rest of the code doesn't seem to work. When I take out the Substring part of it, it does.
-Steps
String theDate, theWeekDay;
if (ToTime(Time[0]) == ToTime(0, 0, 0))
{
theDate=ToDay(Time[0]).ToString().Substring(0,3);
theWeekDay=Time[0].DayOfWeek.ToString().Substring(4,8);
DrawTextFixed("day",theWeekDay, TextPosition.BottomRight);
DrawText("day"+Convert.ToString(ToDay(Time[0])),
theWeekDay+" "+theDate,0, Low[0]-TickSize*20, Color.Blue);
}
You haven't given enough information to solve your problem, but if you're just trying to get the day of the week name in the abbreviated format, use this instead:
theWeekDay = Time[0].ToString("ddd");
You're going to have to provide more than just this snippet of code. What is the Time object you're accessing via an indexer? Have you debugged this to see if Time[0] actually has a value? My guess here would be that Time[0] doesn't return a value that DayOfWeek can work with hence Substring(0,3) is being running against either an empty string or a null value
Unless you have omitted part of the code, your assignment does not take place within a class definition or a method.

Eval() javascript code from within C# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to eval() and run this javascript code from my C# program, but it won't even debug.
How can I do this?
string jsFunc = "eval(function(p,a,c,k,e,d){while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+c+'\\b','g'),k[c])}}return p}('8 4=\'6/13!)!6/12))6/19))))2!,!18*!16!15*!,!:14*-!17:9*!,!26***<\';8 5=\"\";20(3=0;3<4.24;3++){10(4.7(3)==25){5+=\'\\&\'}11 10(4.7(3)==23){5+=\'\\!\'}11{5+=21.22(4.7(3)-1)}};5;',10,27,'|||i|s|m|Nbui|charCodeAt|var||if|else|bct|spvoe|521|8477|_|73|2689|njo|for|String|fromCharCode||l{�ength|28|4451'.split('|')))";
JSEval.JSEval eval = new JSEval.JSEval();
string expression, result;
Console.Write("Выражение: ");
expression = jsFunc;
try
{
result = eval.Eval(expression).ToString();
}
catch
{
result = "!!!";
}
One potential problem, if I am permitted to hazard a guess based on the slim details available, is the odd character sequence found in the string:
...||l{�ength|28|4451'.split('|')))";
Perhaps you should remove the {� and re-run the code.
To elaborate on other meanings of the phrase "code don't debug":
Ensure the project is configured to build in Debug mode.
If your expectation is that you can step through the JavaScript, this will not be possible. You should instead debug the JavaScript using something like Firebug.
If you cannot mentally debug the JavaScript, because it has been minified, you should look at a tool to unpack the JavaScript into something more human readable.

Creating a do statement that has the properties of a while statement [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Has been solved. Was trying to create a do statement that would not do anything until the while was checked. Was solved by simply putting an if statement before the do statement.
Using a do statement and anything else you might need, write the equivalent of:
while (b) {
s;
}
I think you might be looking for
if(b)
{
do
{
s;
}
while(b);
}
Is a standard part of the language:
do
{
s;
} while (b);
If I understood your question correctly you have a code that uses do..while loop and you want to replace it with a "regular" while loop.
That is not so simple as you say in your example. The difference is that do..while will always run at least once, and check the condition after it's completed. While loop may not run at all.

How to use ?: operator [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How would I convert the following code to use the ?: operator.. Is it Possible?
tbtotalamount.Text = string.Format("{0:n2}", dtl.Compute("sum(NetPay)", ""));
if (tbtotalamount.Text.Length == 0)
{
tbtotalamount.Text = "0";
}
The quoted code wouldn't benefit from using the ? : operator, which is called the conditional operator (sometimes called "the ternary operator" although technically, it's only a ternary operator — e.g., an operator that has three operands).
Typically the conditional operator is handy for when you have a variable and want to assign one of two values to it on the basis of a condition. So code in this form:
if (someCondition) {
a = "one value";
}
else {
a = "a different value";
}
can be rewritten
a = someCondition ? "one value" : "a different value";
In your case, though, you don't know that tbtotalamount.Text is blank until after you've done the string.Format, so you're better off leaving it with the if.
Yes. Here's how:
string test = string.Format("{0:n2}", dtl.Compute("sum(NetPay)", ""));
tbttotalamount.Text = test.length == 0 ? "0" : test;
Sorry to see so many downvotes, I'm not familiar with the ? (ternary) operator for a very long time either. I think it is very handy.
To the left of it is your test expression, it should be a boolean after evaluation. To the right is what the operator returns: if true, it will return the value to the left of the :. If false, the value to the right. Note that the whole expression returns something, and the compiler needs you to do something with it. You can't use the ternary operation to replace if-else statements that call functions whose return type is void.
What I mean to say is that a lot of people who've never used it before (like me) seem to think this is a pure if-else replacement, which it is not.

Error on maximum number of Files [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
is there any way to throw new exception on maxmium number of files in a directory ?
No, there is none:
you can catch it with a more generic
System.IO.IOException
And read the (underlying) message you could throw a self written Exception from that point.
UPDATE
I just learned . that you use the
bool isfull = info.GetFiles().LongLength == 4.294.967.295
property. Unfortunately it will eat all your memory.
Therefore using
DirectoryInfo.EnumerateFiles().Count()
perhaps in a chunked manner could be a better approach
fyi:
65,534 for FAT32
4,294,967,295 FOR NTFS
(source)
I don't know specifically about C#, but I know Java doesn't have a specific exception or error to throw for a full drive. I suggest simply making your own exception class and using that. I suggest calling it a FullDirectoryError or DirectoryOverflowError, assuming it's serious enough to be called an error.
Something like this?
public static void checkFileNumber(string directoryToCheck, int maxNumber )
{
if ( Directory.Exists( directoryToCheck ) )
{
if ( Directory.GetFiles( directoryToCheck ).Length > maxNumber )
{
throw new Exception("Too many files in " + directoryToCheck);
}
}
}
Be sure to use System.IO; :-)
Throwing exceptions is an expensive action; when you can avoid it, please do so. Try to program defensively if it is possible. But when throwing one, you have to supply a matching catch on a specific exception.
Like Casper Kleijne said, you could catch on IOException but why in the first place would you want to catch anyway ? What is the compensating action in the catch ?
Please supply some more info what you want to achieve with it.

Categories

Resources