Solved

Automatic code generation based on a class

Posted on 2014-01-22
7
313 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 

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 31

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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 perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

705 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