tostring

Java: How to get the unique ID of an object which overrides hashCode()?

Hello! When a class in Java doesn't override hashCode(), printing an instance of this class gives a nice unique number. The Javadoc of Object says about hashCode(): As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. But when the class overrides hash...

Oddity with ToString() custom number formatting

I am trying to convert numbers in the form 123456 to 123,456 which I find easy enough by using the .NET ToString() method with a custom format of N0. However in one case of data I am getting strange formats whilst using this method. In this case I am generating a DataTable dynamically for my input table to test the code. DataTable dt ...

PHP: Implicit conversion to string instead of getting "Object ID #.."

I've moved to a new webhost were we have php 5.1 instead of 5.2 that I've been using until now. I still haven't figured out if it's a php version or configuration issue. Right now most (or all) of the classes that have __toString functions convert to "Object ID #" (like in php4) but before they all returned the correct values. How can ...

Sorting mixed numbers and strings

I have a list of strings that can contain a letter or a string representation of an int (max 2 digits). They need to be sorted either alphabetically or (when it is actually an int) on the numerical value it represents. Example: IList<string> input = new List<string>() {"a", 1.ToString(), 2.ToString(), "b", 10.ToString()}; input.Or...

Converting GUID to String

why doesn't this work? MsgBox("F6D8C47B-46E6-4E93-A393-00085ACA2242").ToString.Replace("-", "") ...

When is it safe to use .toString()?

Doesn't value have to return toString() to be able to call value.toString()? When do you know you can call value.toString()? <script> var newList = function(val, lst) { return { value: val, tail: lst, toString: function() { var result = this.value.toString(); if (this.tail != null) result += "; " ...

C#: Avoiding Bugs caused by not Overriding ToString

I find the following bug occurring far too often in my code and wondered if anyone knows some good strategies to avoid it. Imagine a class like this: public class Quote { public decimal InterestRate { get; set; } } At some point I create a string that utilises the interest rate, like this: public string PrintQuote(Quote quote) { ...

Does DateTime.ToString("s") return always same format?

According to MSDN on DateTime.ToString ToString("s") should always return string in the format of the sortable XML Schema style formatting, e.g.: 2008-10-01T17:04:32.0000000 In Reflector I came to this pattern inside DateTimeFormatInfo. public string SortableDateTimePattern { get { return "yyyy'-'MM'-'dd'T'HH'...

Problem with converting int to string in Linq to entities

var items = from c in contacts select new ListItem { Value = c.ContactId, //Cannot implicitly convert type 'int' (ContactId) to 'string' (Value). Text = c.Name }; var items = from c in contacts select new ListItem { Value = c.Conta...

What is the Objective-C equivalent for "toString()", for use with NSLog?

Is there a method that I can override in my custom classes so that when NSLog(@"%@", myObject) is called, it will print the fields (or whatever I deem important) of my object? I guess I'm looking for the Objective-C equivalent of Java's toString(). ...

Convert double to string C++?

I want to combine a string and a double and g++ is throwing this error: main.cpp: In function ‘int main()’: main.cpp:40: error: invalid operands of types ‘const char [2]’ and ‘double’ to binary ‘operator+’ Here is the line of code which it is throwing the error on: storedCorrect[count] = "("+c1+","+c2+")"; storedCorrect[] is a stri...

Stacktrace to string in Java

Easiest way to convert the result of Throwable.getStackTrace() to a string that depicts the stacktrace? ...

AspectJ vs. toString()

public pointcut myToString() : within(mypackage.*) String around(): myToString(){ System.out.println("myToString"); return proceed(); } It works only if I override toString in class Im trying to weave, is there any way to make it work on all toString methods? ...

Do any Java libraries use annotations for code generation?

Is anyone aware of a library that uses the the techniques (annotations and classworking) described in this article for automatically generating the standard Object methods toString(), equals() and hashcode() for standard java classes? ...

How to convert double to string without the power to 10 representation (E-05)

How to convert double to string without the power to 10 representation (E-05) double value = 0.000099999999833333343; string text = value.ToString(); Console.WriteLine(text); // 9,99999998333333E-05 I'd like the string text to be 0.000099999999833333343 (or nearly that, I'm not doing rocket science:) I've tried the following variant...

Is there a way to make derived classes override ToString()?

(I'm working in .NET 4.0 beta, C#.) I have an interface, and all classes derived from this interface should implement custom ToString() logic. Is that enforceable? If so, how? ...

Overriding ToString() of List<MyClass>

Hello. I have a class MyClass, and I would like to override the method ToString() of instances of List: class MyClass { public string Property1 { get; set; } public int Property2 { get; set; } /* ... */ public override string ToString() { return Property1.ToString() + "-" + Property2.ToString(); } } I w...

C#: How should ToString() be implemented?

The problems are: GUI libraries like to use ToString as a default representation for classes. There it needs to be localized. ToString is used for logging. There it should provide programming related information, is not translated and includes internal states like surrogate keys and enum values. ToString is used by many string operatio...

IListSource and ToString() with DataSource

First I have a Listbox and set the DataSource to a MyObjectCollection MyObjectCollection implements the Interface IListSource wich contains MyObject's MyObject has the method public override string ToString() { return "test"; } The Listbox now displays "test" for each element in the MyObjectCollection. But if I apply the IListS...

Encoding issues with python's etree.tostring

I'm using python 2.6.2's xml.etree.cElementTree to create an xml document: import xml.etree.cElementTree as etree elem = etree.Element('tag') elem.text = (u"Würth Elektronik Midcom").encode('utf-8') xml = etree.tostring(elem,encoding='UTF-8') At the end of the day, xml looks like: <?xml version='1.0' encoding='UTF-8'?> <tag>W&#195;&#...