Automatic code generation based on a class

Before anyone rants about it - yes I am aware of the ambiguous title - i was having a hard time phrasing my question into 1 line.

Say I have a very simple class like so:
class ANewClass
{
   public int i;
   public string str1;
   public double d;
   private int pvt_i;
}

is there a tool or way to automatically generate code based on this class like so:

ANewClass o = new ANewClass(); // user types this

// this bit gets automatically generated
o.i = 0;
o.str1 = "";
o.d = 0;

The reason I would like to know something like this is because we have some classes that literally have hundreds of really simple public members like the example and I would like to generate a assigned default value (non-null) for them. I understand that this could be done within the original class itself, but I would like to control the (default) assignment.
CodeWranglerAsked:
Who is Participating?
 
Marco GasiConnect With a Mentor FreelancerCommented:
Lol. I agree with AndyAinscow. I know you have not had what you were looking for, but this is not for our deficience but smply because it doesn't exist.
If you ask 'How can I send an email with php?', several answers could be valid and if someone said ?It isn't possible' that would be simply false. But if you ask 'How can I make a cofee with prepared statements?', than the only right answer would be 'You can't!'.

Both me and AndyAinscow have sugegsted possible solutions: looking for a better IDE or modify the way you write your code. You can value more the one or the latter, but they are both valid answer saying, explicitly or implicitly, that what you want is impossible.

Good luck with your project
Marco
0
 
Marco GasiFreelancerCommented:
AFAIK, more than an external tool it should be an IDE set or plugin. I never used C#, but for Php I use Netbeans IDE and I know it has a feature which allows you to exactly what you want. In Options panel it has an Editor tab and within this a Code Template panel where you can write a keyword and then type all code which has to be written automatically. So, for instance, I write a whole html form printed out in php (echo "<form name=....") typing only 'formp'.
That is not 'class based', but it's evident you can create a code template for each class and make what you want.

Unfortunately, Netbeans doesn't give support for C#, so you ca sure use it to write code but you'll don't get all those facilities one look for in an IDE. Anyway, you could look carefully in your IDE option panel: maybe you can find something similar...
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
None that I know of.

However:
When you write the class why not set the default values there

class ANewClass
{
   public int i = 42;
   public string str1 = "foo";
   public double d = 3.142;
   private int pvt_i = 99;
}
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
AndyAinscowFreelance programmer / ConsultantCommented:
Or if you mean different defaults then use a constructor

class ANewClass
{
public ANewClass(int ii, string s, double dd, int ii2)
{
i = ii; str1 = s; d = d; pvt_i = ii2;
}
   public int i;
   public string str1;
   public double d;
   private int pvt_i;
}

and
ANewClass o = new AnewClass(42, "hello", 4.2, 12);
ANewClass o2 = new AnewClass(597, "foo", 0.0, 9);
0
 
CodeWranglerAuthor Commented:
I've requested that this question be deleted for the following reason:

Didn't answer my question - i suspect there is no solution for what im looking for.
0
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
No is a valid answer to a question.
0
 
CodeWranglerAuthor Commented:
Agreed and understood! :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.