|
You should read the documentation for the NumberFormatInfo class and the String.Format method. You still can use format specifiers, but they work a little differently. For a similar format for "%03d", you would use Console.WriteLine("{0:000}", val) . This more closely represents the MaskEdit control commonly used in VB (and various ports).
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Thanks! I was having a hard time trying to find the info in the documentation.
|
|
|
|
|
Don't worry. That little feature isn't actually documented, just like using a number after the "X" format code to manipulate hex values, like "X2" is used to format a byte so that you always get 2 characters per byte (necessary for hex values in many cases). I meant to mention that it isn't really documented in the .NET Framework SDK, but I only remember after I hit "Submit".
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Ahh... you always beat me to it!
|
|
|
|
|
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Ah, Justin, shouldn't you know by now that there's no beating Heath!
"if you vote me down, I shall become more powerful than you can possibly imagine" - Michael P. Butler.
Support Bone
|
|
|
|
|
|
int val = 1;
Console.WriteLine( "{0,0:000}", val );
But the angel said to them, '...I bring you good news of great joy... Today in the town of David a Savior has been born to you; he is Christ the Lord.'
FLUID UI Toolkit | FloodFill in C# & GDI+
|
|
|
|
|
Hi, I need to serialize following class 'F'..., but is not working (propably because there are circular references, but in MSDN is something like "we support it". ). Any solutions ? thanx.
<br />
[Serializable]<br />
public class G {<br />
public F f;<br />
public G() {<br />
}<br />
public G(F f) {<br />
this.f = f;<br />
}<br />
}<br />
[Serializable]<br />
pblic class F {<br />
public G g;<br />
public F() {<br />
this.g = new G(this);<br />
}<br />
}<br />
Wizard_01
|
|
|
|
|
First of all, using the SerializableAttribute will not make the object XML-serializable if you're talking about the System.Xml.Serialization namespace classes (mostly, the attributes therein).
Second, what is the specific error that you're getting?
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
It wrote : "There was an error generating the XML document. A circular reference was detected while serializing an object of type WindowsApplication1.F."
from MSDN :
The serialization architecture provided with the .NET Framework correctly handles object graphs and circular references automatically. The only requirement placed on object graphs is that all objects referenced by the object that is being serialized must also be marked as Serializable ...
Wiizi
|
|
|
|
|
For serialization (such as the SOAP and binary formatters), yes, circular references are supported; but, for XML serialization they are not. SOAP and binary formatting for serialization (using the System.Runtime.Serialization namespace elements) uses IDREFs. XML serialization (using the System.Xml.Serialization namespace elements) uses simple nesting, so circular references are not supported. There is a vast difference between these two serialization technologies.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Oppps. And is possible to simply extend XML serialization with circular references ?
Wiizi
|
|
|
|
|
Changing XML serialization is not feasibily possible, but it is technically possible. Frankly, though, you should look for ways to avoid circular references. This can be disasterous for the CLR when cleaning up or disposing objects.
To extend it a little, you can implement IXmlSerializable , but there is little documentation about this because you're not supposed to implement it. I used ildasm.exe to find out the details since they aren't well documented and did it with success, but I don't know if that'll solve your problem. The most you can do is nest your elements so that in your serializated XML document they don't appear circular - your serialization routines should be programmed to construct the objects again.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
I came up on a bunch of problems (but not circular references in particular) when implementing serialization in Fluid. I ended up building my own internal serialization system that can quickly and simply serialize any object that can be serialized by the .NET XmlSerializer, is marked as binary-serializable, can be converted to a string using the TypeConverter, or implements my own interface that's similar to IXmlSerializable. It automatically stores and retrieves the datatypes of the objects, and allows for short typename aliases. It doesn't handle circular references, though, and I currently haven't been able to get it to work with GAC'ed types (for info see this Fluid post[^]).
Currently it's sort of built into the Fluid architecture, but it wouldn't be hard at all to extract it. If any of you are interested, let me know. It isn't revolutionary, but it works.
|
|
|
|
|
I have to ask the question, "why?". Why not just use the BinaryFormatter and save yourself the trouble? I'm not doubting your skills as a developer (I've seen enough from you to know you're kung fu is good), but the serialization was developed by a team of Microsoft developers, tested internally and by the entire community that helps beta-test the framework versions, and is time-tested as well. There's something to be said for that. Besides, it already handles cirucular references (not that they happen much) and things you're might not. You can easily extend serialization by implementing ISerializable or using an ISerializationSurrogate to serialize types that aren't attributed with the SerializableAttribute , not to mention changing serialization between two types using a SerializationBinder .
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Heath Stewart wrote:
Why not just use the BinaryFormatter and save yourself the trouble?
Because we want to allow most of the types to be able to edited by hand in the XML file, but also allow embedded binary serialization of types that can't be serialized to XML directly.
Note that we are *not* re-implementing serialization from scratch - only unifying and extending what's already there. Our system basically orchestrates the identification of what serializer to use, and allows the serialization to be handled the same from the outside regardless of what means is used for serialization. So if it can be serialized through our interface, that is used, if it can be serialized using the XmlSerializer directly, the XmlSerializer is used, if it can be serialized using the TypeConverter, that is used, or if it is marked as serializable, binary serialization is used and it is encoded using Base64 encoding.
The first and foremost reason we re-implemented the IXmlSerializable interface ourselves is that it is unsupported and therefore may change without notice or backwards compatibility.
As I said, it's not anything really special, but it serves a need.
|
|
|
|
|
if (foo == null)
System.Data.Foo _Foo = new System.Data.Foo;
∙ ^ Error ^
if (foo == null) {
System.Data.Foo _Foo = new System.Data.Foo;
}
∙ ^ No Error ^
Becouse?
|
|
|
|
|
You can't define a variable in an inline if statement. The correct way would be:
System.Data.Foo _Foo=null;
if(foo==null)
_Foo = new System.Data.Foo;
System.Data.Foo _Foo=(foo==null ? new System.Data.Foo : null);
|
|
|
|
|
If you use:
if (foo == null)
{
System.Data.Foo _Foo = new System.Data.Foo;
}
Now _Foo is only available within the { and } .
Now, if you could use the short version:
if (foo == null)
System.Data.Foo _Foo = new System.Data.Foo;
...then where would _Foo be available? In the { and } , which you left out, or in the parent's context (e.g. between the methods { and } )?
Because of this confusion, it is not possible.
|
|
|
|
|
If you use:
if (foo == null)
{
System.Data.Foo _Foo = new System.Data.Foo;
}
Now _Foo is only available within the { and } .
Now, if you could use the short version:
if (foo == null)
System.Data.Foo _Foo = new System.Data.Foo;
...then where would _Foo be available? In the { and } , which you left out, or in the parent's context (e.g. between the methods { and } )?
Because of this confusion, it is not possible.
|
|
|
|
|
yes correct
but i see the c# schema ambiguous ;)
|
|
|
|
|
I cant seem to get my text to align in a list box, heres my code, i just cant remember what im doing wrong, im trying to left align the first string, then have both of the following strings in left aligned columns. But depending on the size of the first string, the second and third strings end up offset.
String.Format("{0, -20}", m_sTimerName) + String.Format("{0, 10}", m_iInitialTime.ToString()) + String.Format("{0, 10}", m_bRepeat.ToString());
Thanks,
Ryan
|
|
|
|
|
Ryan@SalamanderTechnologies wrote:
im trying to left align the first string, then have both of the following strings in left aligned columns. But depending on the size of the first string, the second and third strings end up offset.
2. Those last 2 should be -10.
1. if the timername is too long it will screw up.
leppie::AllocCPArticle("Zee blog"); Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.
|
|
|
|
|
Whoa! Talk about string concatenation inefficiencies! String concatenation is one slow piece of code. For each concat, the CLR needs to measure the length of each string, then create a string large enough to hold the total length, then copy the characters from each string. This is a 2*O(n+m) operation. Instead, just use String.Format one:
string.Format("{0,-20}{1,-10}{2,-10}",
m_sTimerName, miInitialTime, m_bRepeat); Notice also that you don't need to call ToString on non-string variables. String.Format will do this automatically, or it will use "G" for the format if the object implements IFormattable , or it will call Format if the object implements ICusotmFormatter (see the String.Format documentation for details). Leaving the formatting details to String.Format makes it easier to take into account globalization (i.e., using the regional settings when necessary/available).
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|