Accesing Constructor C#

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 371 Views - Last Post: 03 June 2019 - 11:26 AM Rate Topic: -----

#1 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Accesing Constructor C#

Posted 03 June 2019 - 09:51 AM

Hi guys, so far it has been a great day, sunny and funny. However, my programming skills became better today, I have solved a lot of my programming issues, however, I have one issue left, to accessing constructor in underclasses. Lemme give you an example bellow:

public class Author : Bok
{
    public string Roman;
    public Author(string Serie, string Titel, int Mark) :
        base(Serie, Titel, Mark)

    { Roman = "Roman"; }
}



When I go bellow in the main function, I have done this and it works perfectly fine:
                List<Bok> parts = new List<Bok>();
                Author Authors = new Author("Type in Author: ","Author is: ", 150);
                Titel Titeln = new Titel("type in Title","Title is: ", 150);
                Console.WriteLine(Author.Serie );
                Console.WriteLine(Titeln.Serie);
                // Add parts to the list.
                parts.Add(new Bok() { Serie = Console.ReadLine(), Titel = Console.ReadLine(), Märke = 1234});



If I choose to add category "Roman" as in the constructor, How can I add this one in the same "file" or "map" as the registered/added books?

Thanks

This post has been edited by Realme: 03 June 2019 - 10:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Accesing Constructor C#

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15170
  • View blog
  • Posts: 60,720
  • Joined: 12-June 08

Re: Accesing Constructor C#

Posted 03 June 2019 - 09:55 AM

Quote

If I choose to add category "Roman" as in the constructor, How can I add this one in the same "file" or "map" as the registered/added books?

What? This doesn't make a whole mess of sense. Which file? Map what? How is that happening now?
Was This Post Helpful? 0
  • +
  • -

#3 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Accesing Constructor C#

Posted 03 June 2019 - 10:01 AM

No no no, I mead adding the constructor "ROMAN" to the List :)
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15170
  • View blog
  • Posts: 60,720
  • Joined: 12-June 08

Re: Accesing Constructor C#

Posted 03 June 2019 - 10:04 AM

That does not jive at all.

You have a string variable called 'roman'.
A constructor is a specific function, for a class, that is called when the class is instantiated into an object. A string is not a function or a specialized function called a constructor.

Also, please do not edit your post after folk start replying or a significant amount of time has passed. It shifts how replies apply and continuity.
Was This Post Helpful? 0
  • +
  • -

#5 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Accesing Constructor C#

Posted 03 June 2019 - 10:06 AM

Yes you are right, How do I implement the string variable called "roman" in to the list?
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15170
  • View blog
  • Posts: 60,720
  • Joined: 12-June 08

Re: Accesing Constructor C#

Posted 03 June 2019 - 10:08 AM

What list? List of parameters in a constructor? Some other collection not known? Another class that shouldn't be there?
Was This Post Helpful? 0
  • +
  • -

#7 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Accesing Constructor C#

Posted 03 June 2019 - 10:09 AM

This list

List<Bok> parts = new List<Bok>();

Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15170
  • View blog
  • Posts: 60,720
  • Joined: 12-June 08

Re: Accesing Constructor C#

Posted 03 June 2019 - 10:12 AM

You have a string variable inside a class. So that string variable would not be part of that list. If you had an instance of the class 'author', which is inherited type of 'bok', and that has the variable in it.. then yeah.
Was This Post Helpful? 1
  • +
  • -

#9 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15170
  • View blog
  • Posts: 60,720
  • Joined: 12-June 08

Re: Accesing Constructor C#

Posted 03 June 2019 - 10:25 AM

Reminder from your previous thread- you classes are still not right.

It makes no sense to have a class 'Author' inherit from class 'Bok'.

As it was explained, inheritance, is usually defined as a "IS-A Relationship". That is to say when a class inherits from another the class doing the inheriting "IS A" type of class from the inheriter. Example if you have a class called 'fruit' then you made a class called 'apple' that inherited from 'fruit' you would say 'apple is a type of fruit'.

Variables and what not inside a class are "has-a" relationship. The variables that go about to compose a class.

In your classes it makes ZERO SENSE to say "author is-a type of book".

From what I can tell your whole misadventure down this homework's rabbit hole is made infinitely harder because of your incorrect initial choices in classes.


https://www.w3resour...elationship.php
Was This Post Helpful? 1
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6988
  • View blog
  • Posts: 23,760
  • Joined: 05-May 12

Re: Accesing Constructor C#

Posted 03 June 2019 - 10:32 AM

Even Mary Poppins will tell you that "The Cover is Not the Book". :)/>


Or in this case of this thread, the "The Author is Not the Book"... unless your are George R. R. Martin -- in which case the books are just about his own aggrandizement.
Was This Post Helpful? 0
  • +
  • -

#11 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Accesing Constructor C#

Posted 03 June 2019 - 10:33 AM

Look at this code and what I am trying to accomplish in my new codes and you will understand that it DOES make a sense

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Bok
{
    public string Typ;
    public string Titel;
    public string Författare;

    public Bok(string indataTyp, string indataTitel, string indataFörfattare)
    {
        
        Typ = indataTyp;
        Titel = indataTitel;
        Författare = indataFörfattare;
    }
}
    public class Titel : Bok
{
    public string Romanen;
    public Titel(string indataTyp, string indataTitel, string indataFörfattare) :
        base (indataTyp, indataTitel, indataFörfattare)
    {
        Romanen = "Boktypen är: Roman";
        
    }
}
public class Författare : Bok
{
    public string Novell;
    public Författare(string indataTyp, string indataTitel, string indataFörfattare) :
        base(indataTyp, indataTitel, indataFörfattare)
    {
        Novell = "Boktypen är: Novellsamling";

    }
}

public class Utgivningsår : Bok
{
    public string Tidskriften;
    public Utgivningsår(string indataTyp, string indataTitel, string indataFörfattare) :
        base(indataTyp, indataTitel, indataFörfattare)
    {
        Tidskriften = "Boktypen är: Tidskrift";

    }
}

class program
{
    static void Main(string[]args)
    {
        
            Console.WriteLine("Välkommen");
            Console.WriteLine("[1] För att registrera bok");
            Console.WriteLine("[2]");
            string read = Console.ReadLine();
            Int32.TryParse(read, out int P);
       
            switch (P)
            {
                case 1:
                Console.WriteLine("Registrering av ny bok: ");
                List<string> Listan = new List<string>();
                Console.WriteLine("Skriv in Titeln: ");
                Titel Titeln = new Titel("", "Titeln är: " + Console.ReadLine(), "Författaren är: ");
                Console.WriteLine("Skriv in Författaren: ");
                Författare Författaren = new Författare("Tidskriften är: " , "", "Författaren är: " + Console.ReadLine());
                Listan.Add(Titeln.Titel);
                Utgivningsår Utg = new Utgivningsår("", "Titeln är: " , "Författaren är: ");
                Listan.Add(Författaren.Författare);
                Console.WriteLine("Boktyp: ");
                Console.WriteLine("[1] Novellsamling");
                Console.WriteLine("[2] Roman");
                Console.WriteLine("[3] Tidskrift");
                string Reader = Console.ReadLine();
                Int32.TryParse(Reader, out int R);
                if (R == 1)
                {
                    Listan.Add(Författaren.Novell);
                }
                if (R ==2)
                {
                    Listan.Add(Titeln.Romanen);
                }
                if (R == 3)
                {
                    Listan.Add(Utg.Tidskriften);
                }
                
                foreach (string item in Listan)
                {
                    Console.WriteLine(item);
                }
                break;
                default:
                    Console.WriteLine("Skriv siffra");
                    break;
            }
        Console.ReadKey();
    }
}

Was This Post Helpful? 0
  • +
  • -

#12 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6988
  • View blog
  • Posts: 23,760
  • Joined: 05-May 12

Re: Accesing Constructor C#

Posted 03 June 2019 - 10:38 AM

OMG! My eyes! My eyes!

I thought that you were supposed to have a list of books. Now you simply have a list of strings.
068                List<string> Listan = new List<string>();


Was This Post Helpful? 0
  • +
  • -

#13 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Accesing Constructor C#

Posted 03 June 2019 - 10:41 AM

Yeah, that is different code dude. The string List one I am not working on. That is just to demonstrate why the string "roman" and different clases makes a sense
Was This Post Helpful? 0
  • +
  • -

#14 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15170
  • View blog
  • Posts: 60,720
  • Joined: 12-June 08

Re: Accesing Constructor C#

Posted 03 June 2019 - 10:53 AM

021    public class Titel : Bok

031    public class Författare : Bok

042    public class Utgivningsår : Bok


Title is not a type of book.
Author is not a type of book.
Year is not a type of book.

On the other hand a book can have an author, can have a title, and can have a year. I am not certain why you are fighting this reasoning, and have yet to read a comprehensible reason to back up your plan.

Now.. dictionary can be a type of book. Romance novel can be a type of book.. Fiction can be a type of a book. That is reasonable.
Was This Post Helpful? 0
  • +
  • -

#15 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Accesing Constructor C#

Posted 03 June 2019 - 10:59 AM

Yes that is true also that is my task but who cares, it is just different name but same function. Don't you think?

this makes me think of people that are too strict about fact that makes them forget the beauty in real life. For example if I talk to a girl who isn't very well at English I would get to like her more because that would make me curious about what she whants to say and makes my heart burst a little.

You see?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2