Does source code make sense?

Posted on 2006-06-23
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 (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 500 total points
Comment Utility
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

Comment Utility
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

Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

9 Experts available now in Live!

Get 1:1 Help Now