|
how to implement escape sequence like ( ) / \
these in coding
Regex.Matches(str1, " [\,/]");
this shows escape sequence error
the quieter u become more u hear
|
|
|
|
|
Hi,
you only have to escape the \ by using \\. So your regex will look like this:
<br />
Regex.Matches(str1, "[\\,/]");<br />
Regards
Sebastian
|
|
|
|
|
s = Regex.Replace(s," ( ", " ");
s = Regex.Replace(s," ) ", ",");
how about here... same problem
thanks
the quieter u become more u hear
|
|
|
|
|
Hmm, that should be no problem. Pasted it in my code and the IDE doesn't show an error. Do you get a runtime error or a compiler error? Could you please provide the whole error text?
Thanks.
|
|
|
|
|
parsing " ( " - Not enough )'s.
this is the error msg shown.... runtime error
compling suceeded.....
the quieter u become more u hear
|
|
|
|
|
That is because round paranthesis are a special character within regex. You have to escape them also like this:
<br />
s = Regex.Replace(s," \( ", " ");<br />
|
|
|
|
|
thanks a lot......
but single slash \ shown error again......
used \\ and executed sucessfully
thank uuuuu
the quieter u become more u hear
|
|
|
|
|
Actually you have to escape the parenthesis in the regular expression, not the string. Put a backslash in the pattern:
s = Regex.Replace(s," \\( ", " ");
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Or you could use the verbatim operator
s = Regex.Replace(s, @"\(", " ");
Saves one escaping.
|
|
|
|
|
Hi,
I'm making a thread that generates random numbers into an array. Then I make 2 threads from that one I made, but I get the same random numbers in each thread. I've tried to use lock or Monitor, what am I doing wrong?
the C# girl
|
|
|
|
|
Hi,
could you please provide the code you are using to create the random numbers? Maybe you are using the same seed for the creation of the numbers, but that's something that can be seen in the code.
Regards
Sebastian
|
|
|
|
|
Hi,
I've just got!!!
I put static in front of the "Random random = new Random();" not private/public and know it works. Thanks.
|
|
|
|
|
Well, you're welcome...
|
|
|
|
|
Do you know why it didn't work first?
The Random class uses the current time as the "seed" for generating random numbers. Because you created two instances, one for each thread, and the two threads started running in parallel right away, both of them got the same seed and therefore generated the same sequence of random numbers.
BTW, the MSDN doc says that instance methods on the Random class are not thread safe - you might have to look out for that.
|
|
|
|
|
Hi all,
If I have a SqlCommand of which the query includes a join, let's say something as follows:
SELECT * FROM Stock INNER JOIN Transactions ON Stock.StockID = Transactions.StockID
how can I use SqlDataReader[string name] to obtain values from the result coming from a specified field in a specified table? In other words, if both the tables Stock and Transactions include a field with the name Price , the result in SqlDataReader will include two columns, both named Price . If I want to compare the two for instance, how can I force SqlDataReader[string name] to return Stock.Price or Transactions.Price ?
I was hoping for something like:
using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
{
while (sqlDataReader.Read())
{
if (sqlDataReader["Stock.Price"] != sqlDataReader["Transactions.Price"])
ShowError("Item was sold at incorrect price");
}
}
This is a simple example but I hope you get the point. I'm hoping that it isn't necessary to change my SQL query so as to give explicit names to the different columns.
|
|
|
|
|
Hi Dewald,
i'm surprised that the query can be executed with two tables containing a column with the same name. AFAIK you have to rewrite your query to give the two columns explicit names. If I understood the concept of datareaders right, the reader doesn't know that the query contains two tables. For him it is only one table (more like a view).
If there is another solution than rewriting the query, i would be very interested to hear about.
Regards
Sebastian
|
|
|
|
|
Dewald wrote: other words, if both the tables Stock and Transactions include a field with the name Price, the result in SqlDataReader will include two columns, both named Price
AFAIK, that wouldn't be possible. Since the Item is an indexer to the column list, there cannot be multiple instances with exactly the same name (which one would the indexer return or how would you refer to the other one, price or price ).
I think the reader works as other classes in the SqlClient so it will add an ordinal number to the end of the column if several columns exists with the same name. So I guess your other column is Price1. The downside is that you cannot be sure which one it is.
Dewald wrote: sqlDataReader["Stock.Price"]
Single Item is not structural so the dot notation in this case wouldn't have any meaning if structure is considered (although the name of a column can contain a dot).
So after all, the most reliable way is to rewrite the sql. One idea could be that you include your logic (Stock.Price != Transactions.Price) to the sql statement itself using CASE and perhaps event use the condition in where logic (depending what you want). This way your program would have more prepared results and it would be easier to investigate them and perhaps the amount of rows returned would be smaller. But all of this depends on your requirements and logic.
|
|
|
|
|
Mika Wendelius wrote: AFAIK, that wouldn't be possible. Since the Item is an indexer to the column list, there cannot be multiple instances with exactly the same name (which one would the indexer return or how would you refer to the other one, price or price ).
OK thanks, at least I can stop trying to make it work now. Guess I'll just have to rewrite the SQL to give the columns explicit names.
However...
Mika Wendelius wrote: One idea could be that you include your logic (Stock.Price != Transactions.Price) to the sql statement itself using CASE and perhaps event use the condition in where logic (depending what you want).
Of course! Excellent suggestion. In this instance it is actually exactly what I need and should have done from the start. For future problems where this wouldn't suffice I will just rewrite the SQL though.
Thanks for the help.
|
|
|
|
|
You're welcome, glad if it helped
|
|
|
|
|
Hi all
I have created an application windows using c#
In that i want to save the image in Image folder which i created in my application....
now when i save its going to default location
ie \bin\Debug/ImageName...
how can i save the image in my Image folder inside the application ?
can anybody help me?...
|
|
|
|
|
do you have your image as an Image class?
if so just use Image.Save(Application.StartupPath + "\\Images\\Image1.png");
or whatever file path you want to use
|
|
|
|
|
If i give this it is going to same debug\bin\Images\myimage
|
|
|
|
|
what do you mean 'image forlder you created in your application'?
did you create a folder using the application?
if so then you can change the file path in the code i gave to what ever file path you want, drop the application.startuppath if you need to.
or do you mean you have a folder in your 'project' that you want to save the images to.
the application once compiled does not contain folders.
and i very 'almost' 100% sure you can modify your application while its running.
what exactly do you want to do?
|
|
|
|
|
ya u r correct...
I have an imagefolder which i created directly....not through code...
is it possible to save my image to tht folder?...or any other option to create a folder through code and save it in that...
am new to c#
thanks in advance
|
|
|
|
|
In my opinion your best option would be to create the folder through code.
lets say you want a folder on the C Drive for example you would code something like this:
System.IO.Directory.CreateDirectory("C:\\MyNewFolder");
then you can access that with the same code as previously given.
There are also ways to access the current path of system folder such as 'Program Files' and 'Desktop' but i cant remember that of the top of my head, you will have to look at that if you need something like that.
And remember not all computer will be setup the same. Some may not have a C Drive for instance
P.S. you can access any folder that you know the full path of
|
|
|
|