Go Premium for a chance to win a PS4. Enter to Win


Does source code make sense?

Posted on 2006-06-23
Medium Priority
Last Modified: 2010-04-16
I was given the task of "fixing" a C# Windows application.  The application runs in our shop, but the source code is kept at our headquarters, in a land far, far away.  In a lapse of propriety, I'll say that their source control is atrocious.  Most of the time, they can't find source code, much less know if it's current.  

For background info, they used an earlier version of C#.  The Framework is 1.xxx (can't remember exactly which).  We are using VS2005, Framework is 2.

When I opened the solution, it asked me if I wanted to upgrade.  I said yes.  So far, all appeared normal.  When I tried to build the resulting solution, I had 32 errors.  I resolved all but one of them in short order, by adding using statements and relatively easy things like that.

The last error, and the one that I saved for Monday, is detailed below.  I'm afraid my C# skills are not yet good enough to know if this is allowed.

The last error says that (we'll call it) ReportBuilder is declared twice in the same namespace.  There is a public class named ReportBuilder and a public method in that class named ReportBuilder.  Is that legal?  What's the answer to that?

But the more important question is, do you think this source code is what we are actually using in our compiled app?  Would the conversion from an older C# version to the latest version cause so many namespace errors?  I mean, these were .NET Framework namespaces, like System.Data, routine stuff like that.  Would the conversion process cause 31 of these things to be lost?

I know you can only speculate without seeing the code, but it sure makes me uneasy to have code that doesn't compile, and most of it for fairly trivial reasons.

Please give me your opinions.
Question by:tguillor
  • 2

Accepted Solution

mistaeverly earned 2000 total points
ID: 16973929
First off, it is valid (and in fact necessary) to have an identically-named function within a class.  This is the "constructor" and will get executed when the class is instantiated.  A simple example is:

public class ReportBuilder
  int id;
  public ReportBuilder()

In this case, when a new instance of the class is created (with code similar to "ReportBuilder myReportBuilder = new ReportBuilder()") the instantiated class will be inialized to have the id=-1.

As for ReportBuilder being declare in two places, my guess would be one of the three following possibilities:
1) Pre-Conversion, ReportBuilder existed within a namespace.  The "namespace" tags surrounding the class definition were removed and the name of the class matches a .NET pre-existing class, resulting in the overlap.
2) Post-Conversion, your page includes a "using" statement that didn't exist pre-conversion and this referenced namespace contains a class definition with the same name as your created class (what we're calling "ReportBuilder")
3) In 2.0. there is such a thing as a "partial" class.  When the "partial" keyword is used, it's a way of telling the compiler to merge the multiple class definitions into one class definition like follows:

public partial class Class1
 int a;

public partial class Class1
 int b;

In this case, the compiler merges these to get the following definitions:
public class Class1
 int a;
 int b;

So, if the "partial" keyword were removed post-conversion (if the conversion process introduced the them), the compiler would see it as two different classes with conflicting names rather than two parts of the same class.

With the "using" statements being lost, it is possible that the conversion process just placed the default using statements in the class definitions rather than what the old code was.  Personally, I haven't had much luck converting 1.1 Apps to 2.0 and I usually end up doing it manually so I'm not entirely sure how it works.

I hope this is of some help.  Including some applicable code could certainly help to diagnose the error more specifically.
Let me know if I can be of any more help.


Author Comment

ID: 16976448
Excellent answer, and very good info, Chad!!!  I will dig around in the code Monday and try to see why the compiler is complaining about "ReportBuilder".

Pretty intricate stuff the guy wrote, Web Services, delegates, he was really using what he learned in school :-)  And not the first comment anywhere in his code.  Last time I inherited some of his code, once I understood the business reason, I replaced a huge bloated app with eight lines of VBScript that performed the same functionality.  Added bonus: it even worked :-)

Expert Comment

ID: 16981389
As an inheritor of countless Delphi scripts that still make very little sense to me, I can certainly understand where you're coming from.

Let me know if you still need any help after testing this on Monday.

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

876 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