0

i'm trying to get this code to work.. i've been googling for over an hour already but i cant seem to get this code to work: (i'm totally new to C#)

i think it has something to do with the method print. i get the following error:

type 'object' does not contain a definition for 'Voornaam' and no extension method 'Voornaam' of type 'object' could be found

// naam: Dylan Westra
// klas: i1b
// datum 07-09-2013
using System;

namespace les2
{

    public class client
    {
        public string Voornaam;
        public string Achternaam;
        public string Adres;
        public int Salaris;

        public client (string voornaam, string achternaam, string adres, int salaris)
        {
            Voornaam = voornaam;
            Achternaam = achternaam;
            Adres = adres;
            Salaris = salaris;
        }
        public void print (object obje)
        {
            Console.WriteLine (obje.Voornaam + " " + obje.Achternaam + " " + obje.Adres + " " + obje.Salaris);
        }

    }

    class MainClass
    {
        public static void Main (string[] args)
        {
            client gegevens1 = new client("Dylan", "Westra", "Rozengracht 34", 1200);
            //client gegevens2 = new client("Rosa", "de Gruijter", "Rozengracht 34", 200);
            client.print(gegevens1);
        }
    }
}

EDIT: Is there anything like an image (such as a box model in html) but then for classes, objects and methods?

3 Answers 3

2
public void print (object obje){
   Console.WriteLine (obje.Voornaam + " " + obje.Achternaam + " " + obje.Adres + " " + obje.Salaris);
}

Either change the method to public void print (Client obje)

or cast it first:

public void print (object obje) {
   obje = (Client) obje;
   // OR
   obje = obje as Client;
   Console.WriteLine (obje.Voornaam + " " + obje.Achternaam + " " + obje.Adres + " " + obje.Salaris);
}

The reason behind this is that you define your method to take an Object (which is every class's base class) as parameter, but this also means that you will only be able to access the methods defined in the Object class. If you want to use your methods defined in Client, you have to either define your method as such (first approach) or explicitly tell the compiler that you're going to use that parameter as an object of type Client (second approach).

That being said: considering Print() is defined in the Client class, you can just omit the parameter and directly access the instance members.

Resulting in this:

public void Print() {
 Console.WriteLine(Voornaam + " " + Achternaam + " " + Adres + " " + Salaris);
}
Sign up to request clarification or add additional context in comments.

Comments

1

For OOP style, you should do something like this:

using System;

namespace les2
{

  public class client
  {
    public string Voornaam;
    public string Achternaam;
    public string Adres;
    public int Salaris;

    public client (string voornaam, string achternaam, string adres, int salaris)
    {
        Voornaam = voornaam;
        Achternaam = achternaam;
        Adres = adres;
        Salaris = salaris;
    }
    public void print()
    {
        Console.WriteLine (Voornaam + " " + Achternaam + " " + Adres + " " + Salaris);
    }
  }

  class MainClass
  {
    public static void Main (string[] args)
    {
        client gegevens1 = new client("Dylan", "Westra", "Rozengracht 34", 1200);
        gegevens1.print();
    }
  }
}

NOTE: Remember to name the classes, properties and public methods with first letter capitalized, Client not client, Print not print. You may want to find more about static member and instance member.

1 Comment

I find this the best answer. in my opinion this answer explains the most and gives me a better perspective of objects. i'll accept it soon as answer
1

If you need to pass the client object as a parameter, define the parameter type in the print method:

    public void print (client obje)
    {
        Console.WriteLine (obje.Voornaam + " " + obje.Achternaam + " " + obje.Adres + " " + obje.Salaris);
    }

This tells the compiler what fields/methods are available on the object.

However given that print is defined within client, there is no need to pass an object into the method. You could just access the client fields directly:

public void print ()
    {
        Console.WriteLine (Voornaam + " " + Achternaam + " " + Adres + " " + Salaris);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.