?
Solved

Automatic code generation based on a class

Posted on 2014-01-22
7
Medium Priority
?
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 31

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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

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 300 total points
ID: 39854522
No is a valid answer to a question.
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 1200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses

801 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