Solved

Automatic code generation based on a class

Posted on 2014-01-22
7
307 Views
Last Modified: 2014-02-19
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.
0
Comment
Question by:CodeWrangler
  • 3
  • 2
  • 2
7 Comments
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39800395
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39800461
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39800477
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:CodeWrangler
ID: 39854521
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
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 100 total points
ID: 39854522
No is a valid answer to a question.
0
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 400 total points
ID: 39854584
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
 

Author Comment

by:CodeWrangler
ID: 39870769
Agreed and understood! :)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now