views:

444

answers:

9
+1  Q: 

C# Inheritance

I had a coworker see this the other day and I'm not quite sure why this happened. Can anyone explain it?

We have class A:

using System;
using System.Data;

public class A
{
  protected DataTable _table;

  public A()
  {

  }
}

We have class B that inherits from class A (assume there in the same namespace):

using System;

public class B : A
{
  public B()
  {

  }
}

In the constructor of class B, if I try this._table, it doesn't compile? Why does it not?

But to get it to work, I have to add using System.Data; and everything works fine.

Why does .NET not do this for me? So that when I try to access the protected member, it knows that its a System.Data.DataTable?

forgive me if the classes are 100% correct...

+2  A: 

Other than the semi-colon missing after the declaration of _table, it's fine. On the other hand, I personally avoid having fields with an access modifier of anything other than private.

Jon Skeet
"anything other than public" or "anything other than private" ?
James Curran
Oops, thanks James. Fixed :)
Jon Skeet
just fixed that dang semi-colon... no dang intelli-sense here to fix it for me.. hehe
Miles
I see no problem with using internal fields. YAGNI.
Jay Bazuzi
Jay: I think we'll have to agree to differ. I prefer not to let even other classes within the same assembly violate encapsulation. I regard fields as a matter of implementation, not API.
Jon Skeet
I've come around on the fields vs. properties debate. With C# 3's syntax, properties are pretty much free, and the ability to easily make them publicly read-only seals the deal.
Wedge
+3  A: 

This is not a language issue - if anything, you could call it an IDE issue, for not adding the using statement when creating the new class. Even then, the IDE does not know you will be accessing this member, and I prefer to keep my "usings" as minimal as possible. In any case, the fact that B extends A does not tell the compiler anything about which namespaces it must search for the types referenced in B.

Also, I would strongly suggest you change your access of the _table member to use a getter property.

Chris Marasti-Georg
You don't need the using directive anyway, necessarily. For instance, you could write:Console.WriteLine(_table);with no issues.
Jon Skeet
(Oh, and the linker doesn't care about namespaces. It cares about assemblies.)
Jon Skeet
Console.WriteLine expects an Object, so it does not need the _table's Type.
harley.333
Thanks for the clarification Jon
Chris Marasti-Georg
@harley: Exactly. There's nothing in the OP's question suggesting that he needed using System.Data.
Jon Skeet
A: 

Hmm

Is this exact code? I don't see a semi-colon after your declaration of _table...

no, this isn't the exact code, thats what i mentioned up in the original message
Miles
A: 

Are the classes in the same assembly? I tried your sample with the classes in the same assembly and it compiled fine.

Jared
A: 

It should work. I just tried it and it does work.

Check if both classes are in the same namespace. If they are not, don't forget to include the namespace of class A with the using keyword.

Check if both classes are in the same assembly. If they are not, don't forget to include a reference to the assembly of class A in the assembly of class B.

DrJokepu
A: 

I agree with ya'll that they should be using "getters/setters" and just set the _table to private.

I was just curious to why the .Net language wouldn't even let it compile....

Miles
Given that it compiles when we try the code you've given, it's hard to say. What was the error message?
Jon Skeet
A: 

Actually I don't think the compiler even looks at _table, as soon as it sees DataTable and sees that it's an undefined type it will skip forward to the next line that looks syntatically correct. I think even if you had some junk variable after DataTable in class B you would still get the same error.

Sijin
A: 

Shouldn't we be fully qualifing our types in a class anyway? It was the mantra driven into my head while learning c++ anwyay.

Nope, not unless you like reading really wide lines of code. And after you've seen your 1000th System.Windows.Form.* namespace, it begins to drag a little...
Mark Ingram
Thanks... While coding I use namespaces but for release builds I fully qulaify everything. The idea as I was taught was to remove any possible ambiguity...
A: 

I'm agreeing with ya'll now.

I thought I'd post the question because I believed my coworker knew that he was talking about. I tried the following and had now problems:

using System.Data;

namespace WindowsApplication1
{
    public class classA
    {
        protected DataTable _table;

        public classA()
        {

        }
    }
}

next file:

namespace WindowsApplication1
{
    public class classB : classA
    {
        public classB()
        {
            this._table = new System.Data.DataTable();
        }
    }
}

and that compiled just fine. I thought there was going to be a problem with a linkage between all the .Net namespaces but there's not.

Thanks for all the comments/answers.

Miles