|
try like this ,
var styles=items.Select(item=>GetMyStyle(item.Style)).ToList();
sorry...answers are already there
modified on Wednesday, May 25, 2011 9:29 AM
|
|
|
|
|
Hi all,
I need some advice here, I hope somebody can help me.
I have the following class structure (simplified):
public class Bar: IDisposable {...}
public abstract class FooBase: IDisposable
{
Bar bar;
bool disposed;
internal FooBase(Bar bar)
{
this.bar=bar;
}
public void Dispose()
{
if (!this.disposed)
{
this.bar.Dispose(true);
GC.SupressFinalize(this);
this.disposed = true;
}
}
protected void Dipose(bool disposing)
{
if (disposing)
{
this.bar.Dispose();
}
}
}
public FooA: Foo {...}
public FooB: Foo {...}
public static class FooProvider
{
public static FooA GetFooA()
{
Bar bar = new Bar();
...
return new FooA(bar);
}
public static FooB GetFooB()
{
Bar bar = new Bar();
...
return new FooB(bar);
}
...
}
When I run Code Analysis on this, I get Warnings CA2000 on all 'CreateFooX()' methods of the FooProvider class. This warning gives the following message: "Microsoft. Reliability: In method 'FooProvider.GetFooX()', call System.IDisposable.Dispose on object 'bar' before all references to it are out of scope."
Microsoft recommends to never supress this warning but I'm not really sure its warning about a real problem in the code. True that 'bar' is not disposed before going out of scope in whatever 'CreateFooX()' method we consider but a reference to it lives on in the 'FooX' object which eventually will get disposed and will in turn take care of disposing 'bar'.
Am I understanding something wrong about how the Dispose pattern should work and I have some fundamental flaw in my code or should I just supress this warning?
Thanks for any advice.
|
|
|
|
|
you can fix this warning by applying 'using'.Do like this.
public static FooA GetFooA()
{
using(var bar = new Bar())
{
...
return new FooA(bar);
}
}
public static FooB GetFooB()
{
using(var bar = new Bar())
{
...
return new FooB(bar);
}
}
|
|
|
|
|
OK. Now have a think about what effect calling Dispose on an item that you've just allocated via a constructor will be. Explain to the OP what happens when you attempt to access bar in FooB or FooA.
|
|
|
|
|
Thanks for the reply but that is not a valid solution.
If I do that, GetFooX() will be returning an invalid FooX object(disposed bar).
|
|
|
|
|
No , Just check the scope of "using". It will track the instance 'bar'
|
|
|
|
|
This is why I said you should explain the effect of the using statement further, I knew it would cause you confusion.
|
|
|
|
|
I'm sorry but that is wrong.
A use statement is just syntactic sugar for a try finally block. What you are proposing translates to:
Bar bar = null;
try
{
bar = new Bar();
...
return new FooX(bar);
}
finally
{
if (bar != null)
{
bar.Dispose();
}
}
When the method gets to the return statement, it will execute the finally clause before exiting the method's scope returning a FooX object with a disposed bar.
If you are not convinced execute the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Tests
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Creating FooA 'fooA' from GetFooAWithUsingStatement.");
FooA fooA=FooProvider.GetFooAWithUsingStatement();
Console.WriteLine("'fooA' internal bar is disposed: {0}", fooA.InternalBarIsDisposed);
Console.WriteLine("Disposing 'fooA'");
fooA.Dispose();
Console.WriteLine("'fooA' internal bar is disposed: {0}", fooA.InternalBarIsDisposed);
Console.WriteLine();
Console.WriteLine("Creating FooA 'fooA' from GetFooAWithoutUsingStatement.");
fooA = FooProvider.GetFooWithoutUsingStatement();
Console.WriteLine("'fooA' internal bar is disposed: {0}", fooA.InternalBarIsDisposed);
Console.WriteLine("Disposing 'fooA'");
fooA.Dispose();
Console.WriteLine("'fooA' internal bar is disposed: {0}", fooA.InternalBarIsDisposed);
Console.Write("Press a key to exit.");
Console.ReadKey();
}
}
class Bar : IDisposable
{
bool disposed;
public void Dispose()
{
this.disposed = true;
}
public bool IsDisposed { get { return this.disposed; } }
}
static class FooProvider
{
public static FooA GetFooAWithUsingStatement()
{
using (Bar bar = new Bar())
{
return new FooA(bar);
}
}
public static FooA GetFooWithoutUsingStatement()
{
Bar bar = new Bar();
return new FooA(bar);
}
}
class FooBase : IDisposable
{
bool disposed;
Bar bar;
internal FooBase(Bar bar)
{
this.bar = bar;
}
public void Dispose()
{
Dispose(true);
this.disposed = true;
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
this.bar.Dispose();
}
}
public bool IsDisposed { get { return this.disposed; } }
public bool InternalBarIsDisposed { get { return this.bar.IsDisposed; } }
}
class FooA : FooBase
{
public FooA(Bar bar)
: base(bar)
{
}
}
}
To make things clear I should clarify that 'FooX' needs 'bar' to not be disposed during its lifetime. I thought I had made myself clear indirectly by needing to dispose 'bar' in 'FooBase.Dispose()' method.
My question remains the same: Is the Warning I'm getting valid or is the Analyzer not understanding the way the code really works and I can supress it even if MS guidelines says you should not do so?
Edit: typos (not all by any means)
modified on Tuesday, May 24, 2011 9:37 AM
|
|
|
|
|
Then it is better to suppress the warning.
|
|
|
|
|
IMO, the warning is invalid in this situation. If the method was simply returning Bar then you would not get the warning, so it's not understanding that returned Foo requires Bar so should not be disposed.
|
|
|
|
|
Hi to all I am making bar chart in crystal report13.I want to show comparison between two years data like room occupancy of Jan-2010 and room occupancy of Jan-211 are place near to each other and when next month start that is place at some distance.
Its can be possible?
Thanks & Regards,
Ayushi Rastogi
|
|
|
|
|
Hi,
I have a following file names in my database.
TestDocument _ 1 _ 0
TestDocument _ Evidence
Test45
123_publish
when i am entering the Test_ in the DocumentName textbox it is forst converted to uppercase and send to the query and query has following expression which returns all record where it should not have to return any record.
please tell me the changes in the following expression.
UPPER(DOC.ORIGINAL_FILE_NAME) like '%TEST_%'
Thanks,
Umesh Tayade .
modified on Tuesday, May 24, 2011 6:32 AM
|
|
|
|
|
This looks like a database question, not a C# issue.
And wildcard operations aren't the same for all databases, so you'll have to specify what it is you are using.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Hi,
I am using Oracle10g database.
Thanks,
Umesh Tayade
|
|
|
|
|
|
Windcards? Where did the OP mention chronic flatulence?
|
|
|
|
|
Sorry. I meant windcharts of course. Fixed it. Thanks.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Seriously, you can't see the issue. Take a look at your file names and point out the name that starts with 'TEST_' . I see TestDocument and Test45 no Test_
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi,
I know that there is no document with the name TEST in my database but what i am telling is that when i search for TEST_ it should not have to return any records. but it is returning the records.
Thanks,
Umesh Tayade
Never Underestimate the power of Human Stupidity
|
|
|
|
|
It is returning records because you have two sets of wildcards in the query. In other words, you have the % and the _ characters. Effectively, your query is %TEST%.
|
|
|
|
|
If you want to use the _ as a valid identifier in your query rather than having it act as a wildcard, you should precede it with a \ , so your query would become %TEST\_% .
|
|
|
|
|
Are you aware that, in Sql Server at least, the underscore character (_) is the wildcard for a single character? Very nasty in my opinion. You may wish to specify a different character in your query.
|
|
|
|
|
Hi
I have 50 splitcontainers.. i want to read all the splitcontainers & its panels programmatically...Is it possible...?
Because the below code only reads Splitcontainer4 & Panel1, But Iam looking to read all the splitcontainers and its panels programmatically...
foreach (Control Ctrl in this.splitContainer4.Panel1.Controls)
{
if (Ctrl is RichTextBox)
{
((RichTextBox)(Ctrl)).KeyPress += new KeyPressEventHandler(this.Common_KeyPress);
}
}
Thanks for ideas...
Regards
PARAMU
|
|
|
|
|
Just expand your code to add a foreach statement to check each SplitContainer in this , and each Panel in the SplitContainer s.
The best things in life are not things.
|
|
|
|
|
Can you show some samples..I tried...I didn't get...?
foreach (Control AllCtrl in this.Controls)
{
if (AllCtrl is SplitContainer)
{
foreach (Panel MyPanes in AllCtrl.Controls)
{
MessageBox.Show(MyPanes.Name, "some text", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
}
Thanks
|
|
|
|