|
Hi all
In my C# class I have my simple field declared as
String MyField{
get{
return MyString;
}
}
If I use for it the delegate declared as
delegate String StringDelegate();
List< StringDelegate > a = new List< StringDelegate >(100);
List.Add( MyField );
It doesn'! work. It tells me that it cannot convert
annot convert from 'string' to 'StringDelegate'
How I can refer to my field function using delegate?
Kind Regards
ManuStone
|
|
|
|
|
Before I get to your real question, we need to make sure we are on the same page. In your code example above, MyField is a property, not a field. Below is an example of a field and a property:
private String myField = String.Empty;
public String MyProperty {
get {
return this.myField;
}
}
Now, for your question. There is no direct way to create a delegate to a property's get or set accessors. It is possible, as shown here[^], but it's somewhat of a hack. I call it a hack, because you need to pass the name of the property to get the delegate. So if you change the name of the property, then your delegate will be broken.
You may be able to use anonymous methods[^] (see Figure 10) to accomplish what you want.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
You are right! Sorry for having being not very detailed (always too hurry while work!
Many Thanks for your time and for the articles you suggested.
Manu
|
|
|
|
|
As stated in the C# Programming Guide at MSDN delegates can only be used with named or anonymous methods.
AFAIK there is no way to define a delegate for a property ("field function").
If you change your Property to something like:
String GetMyField()
{
return MyString;
}
it should work.
Cheers
Markus
|
|
|
|
|
Many Thanks. It is exactly what I did for the time being
and it is ok!(it works)
Thanks for your time!
Manu
|
|
|
|
|
Hi All,
I was just looking through MSDN and got a shock in that there is no such thing as a temporary file .. (only a memorystream)
I need someting like the C++ tmpfile() ... I need to save some to a temp file as I have a third-party plugin that can only read an XML file if I specify a filename, therefore I need a temp file on the disk ...
Is there still a way to do this ?
Thanks
kort
|
|
|
|
|
|
I find that the following code can be helpful. This class creates a temporary file that you can use, which is removed once you dispose the instantiating class.
; public class TempFile : IDisposable
{
private string _fileName;
private bool disposed = false;
public TempFile()
{
_fileName = Path.GetTempFileName();
}
public string FileName
{
get { return _fileName; }
}
#region IDisposable Members
public void Dispose()
{
Dispose(true);
}
protected void Dispose(bool dispose)
{
if (dispose && !disposed)
{
if (File.Exists(_fileName))
{
try
{
File.Delete(_fileName);
}
catch (IOException ex)
{
Trace.WriteLine(string.Format("Unable to remove temporary file because of {0}",
ex.Message));
}
}
disposed = true;
GC.SuppressFinalize(this);
}
}
#endregion
} using (TempFile tempFile = new TempFile())
{
filename = tempFile.FileName;
using (FileStream fs = new FileStream(tempFile.FileName, FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine("Write to temp.");
}
fs.Close();
}
Console.WriteLine("The size of {0} is {1} bytes", tempFile.FileName,
new FileInfo(tempFile.FileName).Length);
}
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
To add to the 2 other answers, in .NET 2.0, there are new constructors added to System.IO.FileStream . 2 of them take a FileOption argument (can be bitwised). One of the members of this enumeration is DeleteOnClose . I leave as an excercise for you to figure out what this does
-----
You seem eager to impose your preference of preventing others from imposing their preferences on others. -- Red Stateler, Master of Circular Reasoning and other fallacies
If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
God is the only being who, to rule, does not need to exist. -- Charles Baudelaire
|
|
|
|
|
Hi All,
Can any one help me how to implement paging with the datalist,
Backend database MySQL 5.0
Thanks for all.
chandu
chandu
chandu
|
|
|
|
|
|
I tryed impleminting but it was not much of use.
chandu
|
|
|
|
|
Sounds likely that the problem was a human interface error.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Christian Graus wrote: a human interface error
PEBKAC?
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hi,
Its working fine. You must try for that.
Thanks,
Sun Rays
|
|
|
|
|
how i can make form at run time like design time .
i mean can select Object and show properties as Textbox , Labal.
123
|
|
|
|
|
you can't, not easily. You could use reflection, I suppose, and build a UI that lets you click on a control and show/edit it's properties. But, it's a fair amount of work.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
|
thanks a lot , it's very good
123
|
|
|
|
|
You are welcome
|
|
|
|
|
Is it possible to dynamically change the value of a CSS field using C#? For example, use C# code to change the value of #subnav1{display:none;} to #subnav1{display:block;}on page load.
(I'm trying to adapt some Cold Fusion code into a ASP.NET master page and it uses CSS for drop-down menus).
|
|
|
|
|
This isn't C# specific, you can overload CSS in your html/asp page quite easily, just print
<style type="text/css">
#subnav1{display:block;}
</style>
in the <head> of your html output (after you include the regular stylesheet)
|
|
|
|
|
|
There is a great article in the web that covers this topic - http://odetocode.com/Articles/460.aspx . It`s very short and simple, yet contains a lot of usefull information. However, the code provided with that article seams to have a bug.
The problem is in the class BugStateChangedEventArgs, which is marked with [Serializable] attribute. Everything does compile, but throws exception at runtime: "Class BugStateChangedEventArgs is not marked as Serializable".
The source code is available for you here: http://home.icslab.agh.edu.pl/~martinez/WorkflowLibrary3.rar
Do you have any idea what may be wrong? Thank you very much in advance!
|
|
|
|
|
The BugStateChangedEventArgs class is marked as serializable, but it has an field of type Bug. The Bug class is NOT marked as serialiable. Either the field need to be marked with the NonSerializedAttribute, or the Bug class needs to support serialization. The latter is probably your best bet.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|