C
C#14mo ago
OkOk

❔ Visual Studio - How to write out all properties when instansiating a class?

I would like to write out all properties automatically when creating an object Seems like something VS would have a solution for? (Could not find my answer on stackoverflow/chatgpt)
17 Replies
SG97
SG9714mo ago
write out to where?
OkOk
OkOkOP14mo ago
Just in the IDE writing something like MyClass obj = new MyClass would generate this for very big classes
MyClass obj = new MyClass
{
string name = "a
int age = 1
MyClass obj = new MyClass
{
string name = "a
int age = 1
SG97
SG9714mo ago
a few options, writing to log each individual property / reflection / json serialization though what for
OkOk
OkOkOP14mo ago
I just mean to faster write code, when I want to code the creation of an object
SG97
SG9714mo ago
I don't get what you mean why do you need to log all the properties
OkOk
OkOkOP14mo ago
I don't I want to quickly write a new object and initialize all of it's properties
SG97
SG9714mo ago
OH
OkOk
OkOkOP14mo ago
Inside Visual Studio, some kind of trick Heh 🙂
SG97
SG9714mo ago
there's not really an automatic way for VS to do this for you do you want them to have default values?
OkOk
OkOkOP14mo ago
Yes I do I tried googling but that went to old VS-extensions non-maintained
SG97
SG9714mo ago
public string Name {get;set} = "Default name"; so you introduce default values for the properties
OkOk
OkOkOP14mo ago
Yes that will solve some cases, thank you @Snow 🙂
Angius
Angius14mo ago
It's possible that making the properties required will let you do var thing = new Thing {} and use a quick fix to initialize all required properties
RodF
RodF14mo ago
You could overwrite the ToString() method. But that could be tedious, if you have a lot of properties.
OkOk
OkOkOP14mo ago
Good Idea but I could not make it work in VS 2022
Luxx
Luxx14mo ago
Could you not program a macro command to use reflection and do this?
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server