The type or namespace name 'Date' could not be found

  • Thread starter Thread starter Victor
  • Start date Start date
V

Victor

When building a C# project in VS (compacted code included here), I get the
following error message.
What does it mean, and what should I do to overcome the problem?

Error Messge:

C:\USR\Dev\C#\DateInheritance.cs(15): The type or namespace name 'Date'
could not be found (are you missing a using directive or an assembly
reference?)

Thanks,
Victor

=================================

using System ;

class DateInheritance
{
public static void Main()
{ ...}
}
class DatePlus : Date <---------- Problematic line
{ public DatePlus() {}
public DatePlus(int year, int month, int day): base(year, month, day) {}
public int DaysSince1600
{ ... } // properties
public override string ToString()
{ ... }
public static int operator -(DatePlus date1, DatePlus date2)
{ ... }
}
 
Make sure that the correct assembly is referenced and that you import the
correct namespace.
 
Victor said:
When building a C# project in VS (compacted code included here), I get the
following error message.
What does it mean, and what should I do to overcome the problem?

Well, what class are you trying to derive from? There is no Date class
as far as I know.

If you meant DateTime, should should realise that that's a struct, so
you can't derive from it.
 
Victor said:
Thank you, Peter.
How do I do that?

Assuming you're using VS.NET, you add a reference to an assembly by
right-clicking on the "References" item in the project.

You tell the compiler you wish to use a specific namespace by putting

using TheNamespaceIWant;

at the top of your source code.
 
Thank you, Jon.

I am following Charles Petzold's book "Programming Microsoft Windows with
C#", page 41.

Also, I have successfully used Date as type for variables definitions.
What I cannot understand is why is it that I cannot inherit from it in this
specific code sample.

I guess if he wrote code using the Date class, he knows what's he talking
about.
In addition to that, when I try DateTime I get the following error:

C:\USR\Dev\C#\DateInheritance.cs(15): 'DatePlus' : cannot inherit from
sealed class 'System.DateTime'

Victor
 
Victor said:
Thank you, Jon.

I am following Charles Petzold's book "Programming Microsoft Windows with
C#", page 41.

Unfortunately, my copy of that book is at work.
Also, I have successfully used Date as type for variables definitions.

I seriously doubt that, unless you've defined Date in the same project
- which Petzold may well have done.

Could you post a short but complete program which demonstrates using
the Date class, and which compiles without you also including the Date
class itself?
What I cannot understand is why is it that I cannot inherit from it in this
specific code sample.

I guess if he wrote code using the Date class, he knows what's he talking
about.

Unfortunately, in my experience code in books often doesn't compile.
Alternatively, as suggested above, he's included his own Date class.
Date is most definitely *not* a type in the main framework though.
In addition to that, when I try DateTime I get the following error:
C:\USR\Dev\C#\DateInheritance.cs(15): 'DatePlus' : cannot inherit from
sealed class 'System.DateTime'

Yes, because as I said, DateTime is a struct, so you can't derive from
it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top