Solved

Automatic code generation based on a class

Posted on 2014-01-22
7
309 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 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS subreport with parameter 3 29
updating order of the rows, N+1 query issue, C#, asp.net, MVC, SQL Server 3 25
start a process from a service 3 21
C# Gridview 1 27
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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 tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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…

816 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

8 Experts available now in Live!

Get 1:1 Help Now