tostring

C# increment ToString

邮差的信 提交于 2019-12-05 10:44:08
I add an unexpected behaviour from C#/WPF private void ButtonUp_Click(object sender, RoutedEventArgs e) { int quant; if( int.TryParse(Qnt.Text, out quant)) { string s = ((quant++).ToString()); Qnt.Text = s; } } So, if I get quant as 1, quant will be incremented to 2. But the s string will be 1. Is this a question of precedence? EDIT: I re-wrote this as: quant++; Qnt.Text = quant.ToString(); and now this works as I expected. You are using the post -increment operator. This evalutates to the original value, and then increments. To do what you want in a one-liner you can use the pre -increment

ToString on null string

孤街醉人 提交于 2019-12-05 08:55:45
问题 Why does the second one of these produce an exception while the first one doesn't? string s = null; MessageBox.Show(s); MessageBox.Show(s.ToString()); Updated - the exception I can understand, the puzzling bit (to me) is why the first part doesn't show an exception. This isn't anything to do with the Messagebox, as illustrated below. Eg : string s = null, msg; msg = "Message is " + s; //no error msg = "Message is " + s.ToString(); //error The first part appears to be implicitly converting a

Why is System.out.println(super) not permitted?

旧巷老猫 提交于 2019-12-05 07:46:10
Why is System.out.println(super) not permitted? System.out.println(this); This is OK and this.toString() is called and printed automatically. Of course, instance variable is OK instead of this . However, this and super can be used in same way as I know. System.out.println(super); So why does this fail? I think it's supposed to call super.toString() implicitly. I have read Java specification document, but I haven't found the reason. Implementing a standalone variant of super that breaks virtual method dispatch would be an extremely bad idea. Let's think about it for a while. abstract class Base

Format decimal as currency based on currency code

落爺英雄遲暮 提交于 2019-12-05 04:55:26
I have a table of charges with the amount, and the currency code (USD, JPY, CAD, EUR etc.), and am looking for the easiest way to properly format the currency. Using my local culture code (USA) and taking my decimal.ToString("c") gets me $0.00 output, but I'd like the correct currency sign and number of decimals based on the code. Do any libraries exist for this? I can of course write up a switch statement and custom rules for each one, but thought this must have been done before. Update: I've modified Jon B's sample code as follows static IDictionary<string, string> GetCurrencyFormatStrings()

Add an empty string vs toString - why is it bad?

荒凉一梦 提交于 2019-12-05 04:45:18
According to the tool PMD, the following is a bad practice: String s = "" + 123; // bad String t = Integer.toString(456); // ok This is an inefficient way to convert any type to a `String`. Why is it a bad thing to do? String s = "" + 123; // bad String t = Integer.toString(456); Will be compiled to: String s = "123"; String t = Integer.toString(456); so: "" +123 is obvious slightly better! Checked with JAD public static void main(String args[]) { // 0 0:ldc1 #16 <String "123"> // 1 2:astore_1 // 2 3:sipush 456 // 3 6:invokestatic #18 <Method String Integer.toString(int)> // 4 9:astore_2 // 5

Print arrays in Java

↘锁芯ラ 提交于 2019-12-05 04:19:31
I'm writing a method that prints every Object it get passed. This works fine by calling the Object.toString() method for the object but doesn't works for arrays. I can find out whether it is an Array with the Object.getClass().isArray() method, but I don't know how to cast it. int[] a; Integer[] b; Object aObject = a; Object bObject = b; // this wouldn't work System.out.println(Arrays.toString(aObject)); System.out.println(Arrays.toString(bObject)); If you don't know the type you can cast the object to Object[] and print it like this (after making sure it is indeed an array and can be cast to

Overriding ToString() and adding to ListBox C#

末鹿安然 提交于 2019-12-05 03:39:22
Can anyone explain this: public class Test : List<int> { public override string ToString() { return "My ToString"; } } If I instantiate this and add it to a ListBox control on a Windows Form , it displays "Collection" rather than "My ToString". Test test = new Test(); listBox1.Items.Add(test); I thought the add to Items would just call my class's ToString() . The following works as expected of course MessageBox.Show(test.ToString()); manji For that to work you have to disable formatting: listBox1.FormattingEnabled = false; It looks like if formatting is enabled, its doing some magic tricks and

TypeScript override ToString() [closed]

孤人 提交于 2019-12-04 22:11:52
Closed. This question is off-topic . It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 months ago . Let's say I have a class Person which looks like this: class Person { constructor( public firstName: string, public lastName: string, public age: number ) {} } Is it possible to override the toString() method in this class, so I could do something like the following? function alertMessage(message: string) { alert(message); } alertMessage(new Person('John', 'Smith', 20)); This override could look something like

object to string in Python

烈酒焚心 提交于 2019-12-04 18:01:45
问题 I have some data objects on which I want to implement a to string and equals functions that go in depth. I implemented str and eq and although equality works fine I cannot make str behave in the same way: class Bean(object): def __init__(self, attr1, attr2): self.attr1 = attr1 self.attr2 = attr2 def __str__(self): return str(self.__dict__) def __eq__(self, other): return self.__dict__ == other.__dict__ When I run: t1 = Bean("bean 1", [Bean("bean 1.1", "same"), Bean("bean 1.2", 42)]) t2 = Bean

How do I format a number with commas?

房东的猫 提交于 2019-12-04 16:28:07
问题 int a = 10000000; a.ToString(); How do I make the output? 10,000,000 回答1: Try N0 for no decimal part: string formatted = a.ToString("N0"); // 10,000,000 回答2: You can also do String.Format: int x = 100000; string y = string.Empty; y = string.Format("{0:#,##0.##}", x); //Will output: 100,000 If you have decimal, the same code will output 2 decimal places: double x = 100000.2333; string y = string.Empty; y = string.Format("{0:#,##0.##}", x); //Will output: 100,000.23 To make comma instead of