Solved

Does source code make sense?

Posted on 2006-06-23
3
230 Views
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.
0
Comment
Question by:tguillor
[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
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
mistaeverly earned 500 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()
  {
     this.id=-1;
  }
}

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.

Chad
0
 

Author Comment

by:tguillor
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 :-)
0
 
LVL 3

Expert Comment

by:mistaeverly
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.
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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

761 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