Is there a ToString() generator available in Visual Studio 2010?
Asked Answered
C

11

40

Is there any way to generate a ToString() using Visual Studio 2010?

I really don't want to do this by hand!

[EDIT]

I'm looking for a simple string representation of my model. In previous IDEs * ToString generation has been enabled in the UI using simple templates and field selection.

Currently default implementations of Equals and Hashcode are offered in a similar pattern. I'd hoped there was something similar for ToString.

It seems not by default - thanks for the responses!

(* this is my first .net project)

Currier answered 8/2, 2011 at 10:53 Comment(9)
On what basis it should generate the string representation ?Deoxyribose
There is in ReSharper, I havent user VS without it so I really dont know if there is one;)Ibo
Do you mean an override? If so then simply type override, space and select ToString from the list - it will generate the method body for you.Acetanilide
@Deoxyribose I'd hoped there was something similar to Equals generation - where the fields are made available for selection.Currier
@lainie - can you edit the question and explain what you mean? What is there for Equals?Juliajulian
You probably need a snippet as the answer below suggestsDeoxyribose
@lainie, I'm with you on this one. We are both looking for some stupid-simple ToString generator like Eclipse: wiki.eclipse.org/ToString()_generation. I don't think too many .NETers are familiar with this kind of feature.Ironstone
It is a pity that VS2010 is not so smart as IntelliJ IDEA even of the first versions. Maybe, it is because that overriding ToString() is not so popular in .NET as in Java.Utoaztecan
For the year 2020. And VS 2019/2017. (for people who stumble onto this old question). marketplace.visualstudio.com/… DavideLettieri.AutoToStringTinctorial
E
4

You can create your own custom snippet for every boilerplate code and access it from IntelliSence
Here is a good tutorial http://msdn.microsoft.com/en-us/library/ms165392.aspx

Have a good look at how to create snippets with replacements. You can create quite generic structures.

Endgame answered 8/2, 2011 at 11:1 Comment(1)
I have no idea why this solution was accepted. It provides, at best, tangentially related information. Snippets do not have the power to do what the OP requests.Piraeus
S
26

Resharper supports this by generating "formatting members"

https://www.jetbrains.com/resharper/webhelp/Code_Generation__Formatting_Members.html

Resharper -> Edit -> Generate Code -> Formatting Members

or

alt + insert -> Formatting Members

I confirmed this is available in Resharper 8.

Stirrup answered 23/7, 2013 at 15:27 Comment(1)
Just when you think you found something ReSharper couldn't help out with...Astroid
A
10

With Reflection you can actually code a ToString() method:

public override String ToString()
{
    Type objType = this.GetType();
    PropertyInfo[] propertyInfoList = objType.GetProperties();
    StringBuilder result = new StringBuilder();
    foreach (PropertyInfo propertyInfo in propertyInfoList)
         result.AppendFormat("{0}={1} ", propertyInfo.Name, propertyInfo.GetValue(this));

     return result.ToString();
}
Alsoran answered 15/4, 2016 at 13:44 Comment(1)
Uggg Reflection and application performance. ToString methods should execute as fast as possible.Subset
H
6

You can use the StatePrinter project

class AClassWithToString
{
  string B = "hello";
  int[] C = {5,4,3,2,1};

  // Nice stuff ahead!
  static readonly StatePrinter printer = new StatePrinter();
  public override string ToString()
  {
    return printer.PrintObject(this);
  }
}
Heddle answered 6/4, 2014 at 13:54 Comment(1)
I think this is a good suggestion and gave it an upvote. However, beware anyone who is trying to do this from VB rather than C#: https://mcmap.net/q/408623/-using-stateprinter-from-vb-rather-than-c-to-implement-tostring/165164Battalion
E
4

You can create your own custom snippet for every boilerplate code and access it from IntelliSence
Here is a good tutorial http://msdn.microsoft.com/en-us/library/ms165392.aspx

Have a good look at how to create snippets with replacements. You can create quite generic structures.

Endgame answered 8/2, 2011 at 11:1 Comment(1)
I have no idea why this solution was accepted. It provides, at best, tangentially related information. Snippets do not have the power to do what the OP requests.Piraeus
U
4

Major pain that VS 2010 doesn't even have an autogenerate ToString method, syntax is close enough to Java where I used Ecilpse to Generate the ToString and then pasted it into VS...

Unavailing answered 14/4, 2011 at 16:48 Comment(0)
O
4

It doesn't exist on VS out of the box, but it does on the ReSharper plugin, if you don't want to implement it yourself. The plugin is commercial, but I personally think it is worth the money.

With ReSharper, it would be alt+ins -> overriding members -> tostring while class name is on the cursor.

Opposable answered 9/3, 2012 at 9:47 Comment(1)
alt+ins -> formatting members also generates a ToString in R# 7.1. ReSharper is an indispensable tool.Pollie
U
3

Maybe you should take a look at AutoCode 4.0. It is a Visual Studio extension which will brings some snippets with it.

For example you could somewhere within your class simply write tostr and press Ctrl+Enter and it will automatically generate the ToString() method that concatenates all public properties of the class.

Ulphiah answered 9/3, 2012 at 9:53 Comment(1)
But wish there was one for VS 2013 :-(Culm
J
2

If you need better representation of your object while debugging you can use the DebuggerDisplayAttribute:

[DebuggerDisplay("Count = {count}")]
class MyHashtable
{
    public int count = 4;
}

This can be quicker than overriding ToString, but it still doesn't let you choose fields, you have to type them.

Juliajulian answered 8/2, 2011 at 11:7 Comment(0)
P
0

If you don't write your own ToString method, Object provides one for you (although not very useful, since it only return the namespace and name of the objects type).

Otherwise, you have to create it yourself, since the IDE cannot possibly know what you want to output as an object's ToString method.

Propolis answered 8/2, 2011 at 10:57 Comment(2)
Eclipse and NetBeans offer a dump of your local variables, which is what I think she is asking for. I came to this thread in search of the same answer.Ironstone
And IntelliJ. Visual Shitio as usual - 10 years behind.Hortenciahortensa
E
-3

Since the logic of the overridden ToString() method would depend on your own business needs, the only thing I could imagine is an add-in which creates the empty ToString() override for you calling base.ToString() inside, if you then do not customize its content it does not make any sense to have it like that.

Visual Studio already helps you a lot, at least in C#, if you start typing public overrides.

Erastianism answered 8/2, 2011 at 10:56 Comment(2)
you don't even have to write public. If you start with override and select the ToString method from the dropdown, public will be added automatically.Homegrown
"only thing I could imagine"... how about the local variables printed out like Eclipse does? I don't see how that would be beyond imagination.Opposable
P
-3

ToString() is a method sitting on object so you do not need to add that to all your classes, only if you need to override and change behaviour.

Polypus answered 8/2, 2011 at 10:57 Comment(1)
It seems like you missed the point. Lainie is asking if VS will auto-generate the method like Eclipse and NetBeans. You can't always rely on having a debugger present, and being able to dump the state of the object to a log is essential.Ironstone

© 2022 - 2024 — McMap. All rights reserved.