|
When your loop is in its last iteration, count == bits.Length - 1 , so guess what count + 1 is equal to?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
No its not. Look at the output. I ran the entire alphabet with no problem, but soon as I try to run just one or any special ascii characters like µ it gives an error.
|
|
|
|
|
OK, I might be wrong. I didn't trace the code, I only gave a quick look. My apologies.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
You're incrementing the count variable within the loop, and then trying to access the item at the new incremented index without checking whether it's in range. Once the count is out of range, the indexer will throw an IndexOutOfRangeException .
Add a test to the start of each if block to ensure that the index is still in range:
if (count < bits.Length - 1 && bits[count] == false && bits[count + 1] == true)
count++;
if (count < bits.Length - 1 && bits[count] == false && bits[count + 1] == false)
count++;
if (count < bits.Length - 1 && bits[count] == true && bits[count + 1] == true)
count++;
if (count < bits.Length - 1 && bits[count] == true && bits[count + 1] == false)
count++;
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Your solution did not produce an error, but it I still cannot understand why it allowed to process the ENTIRE alphabet in one shot. It would not even allow me to process one(1) special character with producing an error. Can you please explain why this? I simply cannot wrap my thoughts around it.
|
|
|
|
|
For ASCII characters, the byte values will be 127 or less. These bytes do not have the most significant bit set. For byte values greater than 127, the most significant bit is set.
Due to the way you've constructed your loop, if the most-significant bit is set, you will increment count out of range within the loop, and without the range checks in place, you'll get an exception.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I looked at my program for two (2) days straight. I looked at it night and day and could not make heads or tails of the problem. At one point I thought I was going to lose my mind. I am not sure I that I understand your explanation completely, but thanks. I really needed some sort of explanation so I put my mind at rest. Thanks.
|
|
|
|
|
The best part about this is that if you make it work, i.e. don't increment count before checking values again, the entire content of the if statement simplifies to
count++;
Console.WriteLine("count = {0}", count);
... because you are testing all combinations.
I'm not sure what you're trying to do but I'm sure the code is not doing it.
|
|
|
|
|
I was also reading online and saw that many people had this same problem. One of the experts said it was a dot.net error and that people should contact Microsoft tech support for a fix.
|
|
|
|
|
This is indeed a very strange problem to experience. I ran the entire English alphabet all at once and did not get an error. I ran only one special character and got errors every time.
|
|
|
|
|
That's what I thought at first, but it's not quite that simple. If condition 1 is met, you move to the next index, and test condition 2 against the new index. Therefore, it's possible to increment the indexer at least three times within the same iteration of the loop.
For example:
0 1 1 0
^
(condition 1 is met)
0 1 1 0
^
(condition 2 is not met)
(condition 3 is met)
0 1 1 0
^
(condition 4 is met)
0 1 1 0
^
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
That's true as it stands but I'm pretty sure that's not what's intended, because incrementing count multiple times can easily result in an out of range exception.
|
|
|
|
|
Hello all,
I am using sqladapter and update command in it. I am checking how update command does the functioning. For reading the command i am using getupdatecommand.commandtext and the command which comes to me is
UPDATE [empdata] SET [name] = @p1, [id] = @p2, [salary] = @p3 WHERE (([name] = @p4) AND ([id] = @p5) AND ([salary] = @p6))
where name,id and salary are the tables in db. I am not able to understand what these @p are and how is all this working. I will be very thankful for its explanation.
modified 6-Mar-14 15:02pm.
|
|
|
|
|
The @name stuff is a parameter, and it allows you to transfer information from your C# code to SQL without converting it to string, or concatenating strings. This is important, because not using parameters can be very dangerous.
Suppose you have a string you want to update against an integer id:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand com = new SqlCommand("UPDATE myTable SET myColumn1='" + myNewString + "' WHERE Id=" + myId.ToString(), con))
{
com.ExecuteNonQuery();
}
}
Will work, but it leaves you wide open to something called SQL Injection Attacks, where I can use the value in the string to add commands to the UPDATE: if the myNewString variable contains
Hello';DROP TABLE myTable;--
Then what gets passed to SQl is a comannd string:
UPDATE myTable SET myColumn='Hello';DROP TABLE myTable; Which as far as SQL is concerned is two valid SQL commands and a comment.
It updates every row in your table to "Hello", and then deletes the table.
Parametrized queries are a way to avoid that.
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand com = new SqlCommand("UPDATE myTable SET myColumn1=@MYSTRING WHERE Id=@ID", con))
{
com.Parameters.AddWithValue("@ID", myId);
com.Parameters.AddWithValue("@MYSTRING", myNewString);
com.ExecuteNonQuery();
}
}
Passes the same info to SQL, but the string value is now passed as a parameter and not interpreted as an SQL command at all.
It is a very, very good idea to use parameters at all times - it prevents other problems as well!
[edit]Typos - loads of typos... [/edit]
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Have you just given your email address to a Danish member[^] who joined 11 years ago and has never participated since?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Sh*t d*mn b*gg*r f*ck.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Very very thankful for your so clear explanation Also please explain what here AND operations are doing, i will be very thankful to you
|
|
|
|
|
AND is what is called a boolean operator, along with OR and it is used to combine two "tests" to make a bigger, more powerful test.
AND is true is both the test on the left and the text on the right are both true: so
Object is round AND object is blue
is true only for blue balls - it is false for red balls, blue cubes, green pyramids, ...
OR if true if one or the other of the left and right tests are true, and false only if they are both false:
Object is round OR object is blue
is true for blue balls, red balls, and blue cubes, but it is false for green pyramids.
In the case of SQL, you could use to to limit the number of rows SELECTED, or updated:
SELECT * FROM MyTable WHERE ID = 666 AND dateEntered > '2014-01-01' Would only return rows from me this year.
Make sense?
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
I completely agree with what you are saying but here i mean to ask that
UPDATE [empdata] SET [name] = @p1, [id] = @p2, [salary] = @p3 WHERE (([name] = @p4) AND ([id] = @p5) AND ([salary] = @p6))
here what @p4, @p5 , @p6 would constitute?
what if i changed all the three values i.e. name, id and salary, then how would it know which row am i asking to change?
|
|
|
|
|
OK...combine the two comments I've given you and you have the answer.
Let's go back a stage or so, and look at a simple WHERE clause.
SELECT * FROM MyTable WHERE MyColumn=Value
The WHERE clause is "MyCoumn=Value" and it evaluates to a boolean: true or false
If it is true then the row is SELECTed (in this case, it could be UPDATEd in a different statement, such as yours) If it is false then it isn't.
The simple boolean test here is in three parts: "X = Y" where
X is saying which column of the row is to be compared
= is saying the the left and right sides must be the same
Y is the value to compare against the column
Working this back to your UPDATE, you have a three part WHERE clause, each part being of this form, and converted to a more complex test by AND to ensure that all three conditions must be true in order to update the row.
Each part is of the same form as my example above: "X = Y"
[name] = @P4
[id] = @P5
[salary] = @P6
If you change the names, it will check against different columns.
The part on the right is just the identifier of the parameter - it matches the name you use in your C# code to supply the actual value - look back at the first comment I made and it should be clear.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
I am creating a wrapper for a ThirdParty component. That component offers an event in case of an error:
public delegate bool ErrorOccurred(string id, string message); Of course, I'd prefer a custom exception, and wrote a class for that.
But there is a catch: I must return true to ThirdParty to indicate that we took care of the error, otherwise ThirdParty is likely to crash...
I subscribed to that event, and in the handler I try to do:
protected bool Session_ErrorOccurred(string id, string message)
{
throw new SASException(id, message);
return true;
} ... but that doesn't work, of course.
I thought of throwing in a different thread (ThirdParty is multi-threaded anyway), but with
protected bool Session_ErrorOccurred(string id, string message)
{
Thread t = new Thread(() => ThrowSASException(id, message));
t.Start();
return true;
}
private void ThrowSASException(string id, string message)
{
throw new SASException(id, message);
} the exception gets swallowed somewhere in nirwana.
What do you suggest?
|
|
|
|
|
Perhaps your wrapper code could raise another event indicating an error occured and return true ? That event's handler can choose to do whatever it wants (e.g. throw an exception). Not sure if this is a solution, but it may be worth trying.
/ravi
|
|
|
|
|
That's nasty!
Damn good idea, but nasty all the same...don't think there is a tidy solution for this, so it gets my vote.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
OriginalGriff wrote: That's nasty! Agreed.
OK then, I suggest not throwing an exception and instead simply having the consumer of the wrapper subscribe to the wrapper's OnError() handler.
/ravi
|
|
|
|
|
Trouble is...I'm not sure that even with an event the return will be executed at all times...if the exception isn't caught by anything, I think the app will crash. And I'm pretty sure that the exception will be immediately executed on the same thread and will pop the stack all the way back to the exception handler without ever hitting the instruction after the one raising the event...
This is nastier than I thought...I'm not sure there is any way to do this, short of a timer which is truly horrible!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|