tostring

WPF IsEditable=true ComboBox filled with objects displays the ToString() as the selected item

The Wpf combo box allows editing, and this is fine if all your combo box items are strings, or have a ToString() method defined on them. When you select an item, it is displayed as Text, it does not use a DataTemplate, it just calls ToString() on the item that is selected. I get a list of items in my combo drop down that are formatted...

C#: Overriding ToString() method for custom exceptions

I have a custom exception class which contains some additional fields. I want these to be written out in the ToString() method, but if I implement my own ToString(), I loose some other useful stuff (like writing the exception type name, the inner exception data and the stack trace). What is the best way/pattern to implement your own To...

__toString problems

Hi there programmers! I'm building a little MVC system (learning) and I have some problems with showing variables in my view files. This is from my View class: private $vars = array(); public function __set($key, $value) { $this->vars[$key] = $value; } public function __get($key) { return $this->...

toString() unavailable error in Eclipse debugger

While stepping through a method using the Eclipse debugger, I started seeing "toString() unavailable - no suspended threads" for all the variables I wanted to inspect. Why did I get that error, and what should I do next to narrow down the problem in my code? edit My code does create a new process, but the variables I wanted to examine ...

Why do equal strings sometimes behave differently? (VB.NET COM interface)

Example: Dim Sh32 As Object = CreateObject("Shell.Application") Dim path As String = "C:\temp\catalog.zip" Dim sf As Object = Sh32.NameSpace(path) -> does not work, sf = Nothing Dim Sh32 As Object = CreateObject("Shell.Application") Dim path As String = "C:\temp\catalog.zip" Dim sf As Object = Sh32.NameSpace(path.ToString) -> works...

How to create a decent toString() method in scala using reflection?

To make debug-time introspection into classes easy, I'd like to make a generic toString method in the base class for the objects in question. As it's not performance critical code, I'd like to use Reflection to print out field name/value pairs ("x=1, y=2" etc). Is there an easy way to do this? I tried several potential solutions, and ...

Why does enum.ToString() give a different result than what's shown in the debugger tooltip?

Test program (.NET 2.0): [Flags] enum MyEnum { Member1 = 1, Member2 = 2, } class Program { // Inspecting r shows "Member1 | Member2" MyEnum r = MyEnum.Member1 | MyEnum.Member2; // s = "Member1, Member2" string s = r.ToString(); } I would have expected .ToString() to return a string with the members separated b...

Where toString fails to work?

function dTree() { return { init : function(data) { this.data = data; }, node : function(i){ return '' + i; } } }; dTree.prototype.toString = function() { var str = ''; for(var i = 0; i < this.data.length; i++) { str += this.node(this.data[i]); }; ...

PHP extension: convert an object to string with __toString()

Writing a PHP extension in C, I want to convert a userland object (IS_OBJECT) to a string through __toString() if it has one, and fail otherwise. What should I use? I don't need another zval on output, just a char *. zval *zo; switch (Z_TYPE_P(zo)) { case IS_STRING: ... Z_STRVAL_P(zo) ... break; case IS_OBJECT: ... ???(zo)...

Is it better use getter methods or to access private fields directly when overriding toString?

I've seen both approaches used but have never heard that one way is preferred over the other for any particular reason. public String toString() { return this.field1 + " " + this.field2; } versus public String toString() { return getField1() + " " + getField2(); } I used String concatenation in my example to keep the code brief...

How can I format a value as a percentage without the percent sign?

float f = 0.479f; Console.WriteLine(f.ToString("p1")); The output: 47.9 % What should I pass to ToString() in order to remove the percentage sign for output like this: 47.9 EDIT. I should have mentioned that I am passing the mask to a 3rd party component that does it's thing with it. I can't perform any acrobatics with the num...

Difference between .ToString and "as string" in C#

What is the difference between using the two following statements? It appears to me that the first "as string" is a type cast, while the second ToString is an actual call to a method that converts the input to a string? Just looking for some insight if any. Page.Theme = Session["SessionTheme"] as string; Page.Theme = Session["SessionThe...

Why Is ToString() Rounding My Double Value?

How do I prevent my double value from being rounded when converting to a string? I have tried both Convert.ToString and ToString() with the same result. For example my double may look something like 77.987654321, and the two strings conversions convert to to 77.98765. I need to keep the precision of the value as is. ...

C# IFormatable ToString("0.0000")

I have class and I want to reproduce the functionality associated with ToString("0.0000") as well as some other numerical formatting stuff. How can this be done? ...

Change "ToString" for a sealed class

I have a class I am working with: public sealed class WorkItemType It's ToString is weak (Just shows Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemType). Is there anyway to override this to show the name of the WorkItemType? Normally I would just aggregate the value in a new class, but I am using this for bindings in WPF ...

How does Linq-to-Xml convert objects to strings?

Linq-to-Xml contains lots of methods that allow you to add arbitrary objects to an xml tree. These objects are converted to strings by some means, but I can't seem to find the specification of how this occurs. The conversion I'm referring to is mentioned (but not specified) in MSDN. I happen to need this for javascript interop, but th...

C#: FullName of generic type without assembly info?

I have a database table where I store the height, width, state, et cetera, of windows. As identifier for the windows I use the full type name of form. It works well, but I discovered that some forms that are generic gets names which are incredibly long. The reason is that the generic type is listed with full assembly information. Is ther...

How can i add a toString() function for every class file, automated in Eclipse?

Hi, I'm doing a AS3 project in Eclipse and trace alot of values. I though it would be nice to have a toString() function in every class, at the bottom of each class as the last function, but i dont want to do this by hand for 500+ files. Is there a quick and good way of doing this automated? How would you go about this? Thanks in adv...

Override ToString() in asp.net c#, basics. Doesn'twork.

Hi I have the Default aspx. I want to test overriding default methods like to ToString(). Whenever I use ToString(), I thought with following code it has to add "my text";? why not? public partial class test : System.Web.UI.Page { public override string ToString() { return base.ToString() + "my text"; } prot...

Why does .NET decimal.ToString(string) round away from zero, apparently inconsistent with the language spec?

I see that, in C#, rounding a decimal, by default, uses MidpointRounding.ToEven. This is expected, and is what the C# spec dictates. However, given the following: A decimal dVal A format string sFmt that, when passed in to dVal.ToString(sFmt), will result in a string containing a rounded version of dVal ...it is apparent that decim...