Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

(very) newbie question - Declaring two classes with the same name

Status
Not open for further replies.

sunil128

Programmer
Mar 20, 2008
45
GB
Hi all this is my first post in this forum, im very new to C#, although I have done a little C++ in the past im still trying to find my feet. So I am just going through a bunch of online tutorials to try & get my head round it. One of these is Microsofts.

In the section Classes\Instance constructors I found the following code:
(The actual page is -
*****************************************************
using System;
class Point
{
public double x, y;
public Point() {
this.x = 0;
this.y = 0;
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public static double Distance(Point a, Point b) {
double xdiff = a.x – b.x;
double ydiff = a.y – b.y;
return Math.Sqrt(xdiff * xdiff + ydiff * ydiff);
}
public override string ToString() {
return string.Format("({0}, {1})", x, y);
}
}
class Test
{
static void Main() {
Point a = new Point();
Point b = new Point(3, 4);
double d = Point.Distance(a, b);
Console.WriteLine("Distance from {0} to {1} is {2}", a, b, d);
}
}
************************************************************

I have a very simple question, how is it you can declare two classes of the same name as in this example? Im refering to the two public Point classes, one has no parameters and the other has two parameters. I have also seen this elsewhere and i am very confused!
Thanks

p.s. when i create a thread what are the tags to highlight code?
 
You are not declaring 2 Point classes but 2 Point Constructors. They are overloading the constructor so that if they decide not to pass any values in when creating a new point object, it is given a default value, and if they pass in 2 doubles when creating a new point object, x and y will be set to those values
 
The tags to format code blocks are

[ignore]
Code:
Your code
[/ignore]

as in
[ignore]
Code:
[/ignore][code]
using System;
class Point
{
   public double x, y;
   public Point() {
      this.x = 0;
      this.y = 0;
   }
   public Point(double x, double y) {
      this.x = x;
      this.y = y;
   }
   public static double Distance(Point a, Point b) {
      double xdiff = a.x – b.x;
      double ydiff = a.y – b.y;
      return Math.Sqrt(xdiff * xdiff + ydiff * ydiff);
   }
   public override string ToString() {
      return string.Format("({0}, {1})", x, y);
   }
}
class Test
{
   static void Main() {
      Point a = new Point();
      Point b = new Point(3, 4);
      double d = Point.Distance(a, b);
      Console.WriteLine("Distance from {0} to {1} is {2}", a, b, d);
   }
}
[ignore][/code][/ignore]

By the way, there is a special tag to enable you to display tags, this is: [ignore][/ignore]

Just underneath the posting window is a link Process TGML this details the various available tags.



Hope this helps.

[vampire][bat]
 
Like the others have said -- you've got an overloaded constructor there, not two classes.

But even so, it's possible to create two classes of the same name... as long as they're in different namespaces.
Code:
namespace TekTips.Forum
{
  public class User
  {
     :
  }
}

namespace TekTips.Blogs
{
  public class User
  {
     :
  }
}
So the full name of them would then differ, and the compiler can tell them apart.

If you ever needed to use both of them in the same section of code, the compiler will tell you to use the fully qualified name to disambiguate them.

But otherwise, you can use the normal "using" statement with one or the other as a shortcut.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top