|
Thanks for your help, Leslie. Actually I think the question was simpler than that - Guffa's post below answers it.
Sincerely,
Alexander Wiseman
|
|
|
|
|
As strings are immutable, you can't change a string. What happens when you assign a new value to a string variable, is that the new value is a separate string object, and the reference to that object replaces the reference to the previous value.
As reading and writing references are atomic, there is no need to use a lock.
When you read the string value, you are copying the reference to the string. You can then use that reference to access the string that was the value at the moment you read the reference, without fear of anything happening to that string.
---
b { font-weight: normal; }
|
|
|
|
|
Thanks for your reply Guffa. That makes perfect sense! I think the thing that I was missing was that reading and writing references are atomic.
Sincerely,
Alexander Wiseman
|
|
|
|
|
Guffa wrote: As reading and writing references are atomic, there is no need to use a lock.
Oh, that's good to know! I had guessed that it wasn't but glad to learn that I was wrong.
|
|
|
|
|
The original post was correct strings are immutable and therefore threadsafe.
|
|
|
|
|
It's kind of misleading though. For instance:
if(someVolatileVariable != null)
{
someVolatileVariable.DoSomething();
}
That is buggy multithreaded code! The reason is that another thread could potentially set someVolatileVariable to null after the current thread checks whether it is not null. A correct version of that would be:
Foo someVar = someVolatileVariable;
if(someVar != null)
{
someVar.DoSomething();
}
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Goof around music jam with my brothers (with video)
The apostle Paul, modernly speaking: Epistles of Paul
Judah Himango
|
|
|
|
|
Good point. Events work the same way:
Bad:
if(WorkCompleted != null)
{
WorkCompleted(this, EventArgs.Empty);
}
Good:
EventHandler handler = WorkCompleted;
if(handler != null)
{
handler(this, EventArgs.Empty);
}
|
|
|
|
|
Reading and writing references may be atomic, but that doesn't solve much; a thread may need to read a variable, but may be seeing old/dirty data depending on the processor architecture. That's why we have methods like Thread.VolatileRead, Thread.VolatileWrite, and the volatile keyword.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Goof around music jam with my brothers (with video)
The apostle Paul, modernly speaking: Epistles of Paul
Judah Himango
|
|
|
|
|
I have an unusual situation where I have about 30 classes that have huge swaths of source code that is identical to each class.
Unfortunately about 30 % is unique to each class and I can't just take the similar bits and make a new class out of them because it's a big struct and method to fill that struct from a database (they are IList based class that feeds a read only datagrid and is used for reporting)
What I'm currently doing is copying and pasting but this is tedious and what I'd really like to do is define the chunks of code that are common and have the compiler insert them into the classes on compiling. AFAIK this isn't a feature in visual studio 2k5 or c#.
Does anyone have any ideas or suggestions?
|
|
|
|
|
Could you break out the parts that are repeated and put them into a base class that all of your other classes will derive from?
|
|
|
|
|
No unfortunately, the common code is inside a struct and the different part is inside the same struct.
|
|
|
|
|
Does it need to be a struct?
---
b { font-weight: normal; }
|
|
|
|
|
yes absolutely, it's business objects in released software production code, there is a huge system revolving around it for reporting, displying in grids and filtering and sorting, matching up sql column names and localized text keys to public properties in the struct, binding it to reports and grids etc etc and to top it all off I'm over 300 of them now so absolutely no possibility of doing it any differently at this point, on top of which it's the best way to do it.
|
|
|
|
|
I see. To me it sounds like it should have been a class from the beginning, but I guess you are stuck with it...
Could you make an interface that the structs would implement to give access to the data in the struct? Then you could put the common code in a class and use it from the struct. Something like:
interface IDataAccess {
int SomeData { get; set; }
...
}
public class DataHandler {
private IDataAccess dataAccess;
public DataHandler(IDataAccess dataAccess) { this.dataAccess = dataAccess; }
public void DoSomething() {
this.dataAccess.SomeData = 42;
}
}
public struct SomeStruct : IDataAccess {
public DataHandler Handler;
public SomeStruct() {
Handler = new DataHandler(this);
}
public int SomeData { get {...} set {...} }
...
}
---
b { font-weight: normal; }
|
|
|
|
|
John Cardinal wrote: Does anyone have any ideas or suggestions?
Use C++
[Modified] -- had to get that in quick before anyone beat me to it
Now I go back and read your post and I must not understand the problem, could you expand a bit?
John Cardinal wrote: I can't just take the similar bits and make a new class out of them because it's a big struct and method to fill that struct from a database
I don't understand that.
|
|
|
|
|
What about creating a simple custom tool.
This[^] article is very to the point, I'm guessing that all you'll have to do is read in the contents of the file, replace #include<filename> with the contents of filename and return the bytes.
The article explains more fully what you have to do. I also seem to remember that there was also an article about custom tools here.
Formula 1 - Short for "F1 Racing" - named after the standard "help" key in Windows, it's a sport where participants desperately search through software help files trying to find actual documentation. It's tedious and somewhat cruel, most matches ending in a draw as no participant is able to find anything helpful. - Shog9
|
|
|
|
|
Thanks, I'll have a look.
|
|
|
|
|
Thanks for answering my question from earlier in the day as well
I posted one this morning about writing a custom tool. It was excellent luck that you happened to post exactly what I needed.
|
|
|
|
|
gantww wrote: I posted one this morning about writing a custom tool
Which question what, did I respond to it or did you stumble across this answer?
Formula 1 - Short for "F1 Racing" - named after the standard "help" key in Windows, it's a sport where participants desperately search through software help files trying to find actual documentation. It's tedious and somewhat cruel, most matches ending in a draw as no participant is able to find anything helpful. - Shog9
|
|
|
|
|
John Cardinal wrote: What I'm currently doing is copying and pasting but this is tedious and what I'd really like to do is define the chunks of code that are common and have the compiler insert them into the classes on compiling. AFAIK this isn't a feature in visual studio 2k5 or c#.
Does anyone have any ideas or suggestions?
Hmm, I don't think this is what you're looking for, but code snippets might help automate the copying and pasting. It's not the same as a compiler inserting code into a class at compile time, which, to be honest, I'm not really sure I understand, but it would certain save on key strokes.
|
|
|
|
|
Yes, use abstraction and interfaces, you're working too hard.
|
|
|
|
|
Hello,
I want to force the user to close first the child form befor he/she can click on the mainform. just like those message box.
How do you do that?
Donkaiser
|
|
|
|
|
use ShowDialog() instead of Show()
|
|
|
|
|
My typing is slow
Sincerely,
Elina
Life is great!!!
Enjoy every moment of it!
|
|
|
|
|