9 Replies - 1332 Views - Last Post: 27 August 2013 - 09:11 AM Rate Topic: -----

#1 Pogaca   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 120
  • Joined: 24-August 13

[C#] - Inheritance

Posted 26 August 2013 - 09:42 AM

I tried to follow tutorial from one web page and it turned out even I do what they did it doesn't work for me. I made one class called Books, and then I tried to add another class inside it called Fantasy. Then I tried to create object called Book with Fantasy class. But it doesn't work. Here is my Program.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace vezbanje_C_sharp
{
    class Program
    {
        static void Main(string[] args)
        {
           Fantasy  Book = new Fantasy();
            Book.Title("Lord of the rings");
            Book.ID("0x3434");
            Book.Show();
            Console.ReadKey();
        }
    }
}

And here is my class Book.cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace vezbanje_C_sharp
{
    class Books
    {
        public string Title;
        public string ID;

        //Default Constructor
        public Books()
        {
            Title = "";
            ID = "";
        }
        //Overloaded constructor
        public Books(string title, string id)
        {
            Title = title;
            ID = id;
        }
        class Fantasy : Books
        {
            public void Show()
            {
                Console.WriteLine(Title);
                Console.WriteLine(ID);
            }
        }
    }
}



What's the problem, where I make mistake?

Is This A Good Question/Topic? 0
  • +

Replies To: [C#] - Inheritance

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14174
  • View blog
  • Posts: 56,774
  • Joined: 12-June 08

Re: [C#] - Inheritance

Posted 26 August 2013 - 09:46 AM

Classes do not go inside other classes. They can stay in the same namespace, but not inside one another. Pop it outside the class.
Was This Post Helpful? 1
  • +
  • -

#3 Pogaca   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 120
  • Joined: 24-August 13

Re: [C#] - Inheritance

Posted 26 August 2013 - 09:52 AM

Ohhhhhhh, I see it now. Also I made mistake with adding values to book title and ID. I used it like it's function xD . Thanks!!
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6563
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Re: [C#] - Inheritance

Posted 26 August 2013 - 11:54 AM

View Postmodi123_1, on 26 August 2013 - 04:46 PM, said:

Classes do not go inside other classes. They can stay in the same namespace, but not inside one another. Pop it outside the class.

Nested classes can be created in C# but the OPs class Fantasy was not available publicly (in Main).
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6563
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Re: [C#] - Inheritance

Posted 26 August 2013 - 12:09 PM

Just if anyone is interested in this subject, nested, or inner, classes were introduced with Java 1.1 and are supported by a number of languages.

wiki

  • Inner classes became a feature of the Java programming language starting with version 1.1.
  • Nested classes are also a feature of the D programming language, Visual Basic .NET, Ruby, C++ and C#.
  • In Python, it is possible to nest a class within another class, method or function.
  • C++ has nested classes that are like Java's static member classes, except that they are not declared with "static".


So what are inner classes good for anyway?
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: [C#] - Inheritance

Posted 26 August 2013 - 06:35 PM

There is also a tutorial on classes linked in my signature block.
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: [C#] - Inheritance

Posted 27 August 2013 - 08:23 AM

andrewsw is correct, inner classes are allowed and even useful from time to time. But they're scoped to the class, so the OP could have referenced his class as Books.Fantasy.

Personally, I love using them when I'm writing code with several string constants.

public static class Constants{
  public static class Configuration{
    public const string URL = "http://www.example.com";
  }
  public static class Tables{
    public const string DATABASE_NAME = "example\\example";
  }
}
//...
var url = Constants.Configuration.URL;

Was This Post Helpful? 1
  • +
  • -

#8 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6563
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Re: [C#] - Inheritance

Posted 27 August 2013 - 08:42 AM

View PostCurtis Rutland, on 27 August 2013 - 03:23 PM, said:

andrewsw is correct, inner classes are allowed and even useful from time to time. But they're scoped to the class, so the OP could have referenced his class as Books.Fantasy.

He would also have to use the class modifier public:

public class Fantasy

although creating public inner-classes is not a common practice (perhaps not recommended).

(The class Fantasy could be public static but then the class Books would also need to be declared in this way.)
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: [C#] - Inheritance

Posted 27 August 2013 - 08:52 AM

I'm not sure if we're on the same page or not. This is definitely possible. You're right about the visibility modifier on the inner class.

Spoiler


We're both definitely on the same page that this isn't typical. There's nothing particularly wrong with it. But it's not particularly right either.
Was This Post Helpful? 1
  • +
  • -

#10 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6563
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Re: [C#] - Inheritance

Posted 27 August 2013 - 09:11 AM

I'm sure we are both (more or less) on the same page. I just wanted to emphasize that using Books.Fantasy alone would not be enough for the original code to compile.

This post has been edited by andrewsw: 27 August 2013 - 09:14 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1