tostring

Printing out a linked list using toString

岁酱吖の 提交于 2019-11-28 11:24:50
Ok guys, so I am trying to learn how to print out a linked list. I have all the methods that I would need to use for the list, but I can't figure out how to display the values of the nodes. Right now there is nothing in my main method because I kept getting errors trying to call non static methods in the main. I have a toString method that displays the contents of the list. How would I go about calling this toString to display the value of each node? Any advice will be greatly appreciated. Here is the node class: public class LinkedListNode { private int data; private LinkedListNode next;

Problem getting GUID string value in Linq-To-Entity query

社会主义新天地 提交于 2019-11-28 10:25:42
问题 I am trying to write a GUID value to a string in a linq select. The code can be seen below (where c.ID is GUID), but I get the following error: Unable to cast the type 'System.Guid' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types. var media = ( from media in Current.Context.MediaSet orderby media.CreatedDate select new Item { Link = "~/Media.aspx?id=" + media.ID, Text = "Media", Time = media.CreatedDate } ).ToList(); 回答1: One way would be to

How can I override the toString method of an ArrayList in Java?

允我心安 提交于 2019-11-28 10:05:57
I would like to have my own implementation of the toString() method for an ArrayList in Java. However, I can't get it working even though I added my toString() like this to the class that contains the ArrayList. @Override public String toString() { String result = "+"; for (int i = 0; i < list.size(); i++) { result += " " + list.get(i); } return result; } When I call my ArrayList like this list.toString() , I still get the default representation. Am I missing something? You should do something like public static String listToString(List<?> list) { String result = "+"; for (int i = 0; i < list

When stepping through my program the ToString() method is being called for no reason

Deadly 提交于 2019-11-28 09:47:41
问题 This is a bizarre problem I'm having even my senior programmer next to me is also confused. The issue in full is that somehow my ToString() method is being called and I don't know or understand how this is my code static void Main(string[] args) { Console.Out.WriteLine("Blank Constructor"); Form form = new Form(); <-- ToString() gets called on this line. form.ToString(); Console.Read(); } public Form() { FormName = ""; FormImageLocation = ""; FormDescription = ""; FormID = 0; CreatedDate =

When is it desired to not implement toString() in Java?

隐身守侯 提交于 2019-11-28 09:35:37
A lead developer on my project has taken to referring to the project's toString() implementations as "pure cruft" and is looking to remove them from the code base. I've said that doing so would mean that any clients wishing to display the objects would have to write their own code to convert the object to string, but that was answered with "yes they would". Now specifically, the objects in this system are graphic elements like rectangles, circles, etc and the current representation is to display x, y, scale, bounds, etc... So, where does the crowd lie? When should you and when shouldn't you

Automatic .ToString()?

妖精的绣舞 提交于 2019-11-28 09:25:51
问题 I have a method like this: void m1(string str) and have a class like this: public class MyClass { public bool b1 { set; get; } //and other properties } Now why following code does not cause compile error? IClass2 _class2 = new Class2(); MyClass c1 = new MyClass(); _class2.m1("abcdef" + c1); When I debug it, I realized that c1.ToString() has been passed to m1 . Why this automatic .ToString() has been occurred? The only thing I could say is that m1 has been defined in IClass2 interface and has

Map to String in Java

怎甘沉沦 提交于 2019-11-28 08:51:20
When I do System.out.println(map) in Java, I get a nice output in stdout. How can I obtain this same string representation of a Map in a variable without meddling with standard output? Something like String mapAsString = Collections.toString(map) ? BalusC Use Object#toString() . String string = map.toString(); That's after all also what System.out.println(object) does under the hoods. The format for maps is described in AbstractMap#toString() . You can also use google-collections (guava) Joiner class if you want to customize the print format 来源: https://stackoverflow.com/questions/2828252/map

Why does dynamic.ToString() return something between a string and not a string?

会有一股神秘感。 提交于 2019-11-28 08:10:38
问题 I use a type derived from a DynamicObject as a builder for some strings. At the end I call ToString to get the final result. At this point I thought it would give me a normal string but this string is somehow strange. It behaves like one when I use string functions on it but it behaves like I don't know actually what, something neither a string nor a dynamic. This is how I implemented ToString on my builder public class Example : DynamicObject { public override bool TryConvert(ConvertBinder

C# Enum.ToString() with complete name

馋奶兔 提交于 2019-11-28 07:34:09
问题 I am searching a solution to get the complete String of an enum. Example: Public Enum Color { Red = 1, Blue = 2 } Color color = Color.Red; // This will always get "Red" but I need "Color.Red" string colorString = color.ToString(); // I know that this is what I need: colorString = Color.Red.ToString(); So is there a solution? 回答1: public static class Extensions { public static string GetFullName(this Enum myEnum) { return string.Format("{0}.{1}", myEnum.GetType().Name, myEnum.ToString()); } }

How to display DateTime with an abbreviated Time Zone?

≯℡__Kan透↙ 提交于 2019-11-28 07:11:22
I am aware of the System.TimeZone class as well as the many uses of the DateTime.ToString() method. What I haven't been able to find is a way to convert a DateTime to a string that, in addition to the time and date info, contains the three-letter Time Zone abbreviation (in fact, much the same way StackOverflow's tooltips for relative time display works). To make an example easy for everyone to follow as well as consume, let's continue with the StackOverflow example. If you look at the tooltip that displays on relative times, it displays with the full date, the time including seconds in twelve