Jump to content

Properties

- - - - -

  • Please log in to reply
6 replies to this topic

#1
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
this is my code:


using System;

using System.Collections;



class Boje1

{

    

    private string boja;


    public void Boja(string boja)

    {

        this.boja = boja;

    }


    public string Opis()

    {

        return "Ovaj auto ima žutu, crvenu i crnu boju";

    }

    

    public string Boja1

    {

        get { return boja; }

        set { boja = value; }

    }

}


class Boje2

{


    private string boja2;


    public void Boja2(string boja)

    {

        this.boja2 = boja;

    }


    public string Opis2()

    {

        return "Ovaj auto ima bijelu, zelenu i crnu boju";

    }


    public string Boja1

    {

        get { return boja2; }

        set { boja2 = value; }

    }


}

static class BMW

{

    public static void Serija1()

    {

        Boje1 ispis1 = new Boje1();


        ArrayList lista1 = new ArrayList();

        lista1.Add("125i Cabrio, Cijena: 37.000 eura");

        lista1.Add("116i, Cijena: 15.000 eura");

        lista1.Add("118d Cabrio, Cijena: 22.500 eura");

        lista1.Add("116d, Cijena: 19.200 eura");

        lista1.Add("120d, Cijena: 7.400 eura");

        lista1.Add("118i, Cijena: 15.500 eura");


        foreach (string prva in lista1)

        {

            Console.WriteLine(prva);

            Console.WriteLine(ispis1.Opis());

        }

    }

    public static void Serija3()

    {


        Boje1 ispis2 = new Boje1();

        Boje2 ispis3 = new Boje2();


        ArrayList lista2 = new ArrayList();

        lista2.Add("330d Automatik, Cijena: 10.300 eura");

        lista2.Add("320d Automatik, Cijena: 17.000 eura");

        lista2.Add("318d, Cijena: 18.550 eura");

        lista2.Add("320i Automatik, Cijena: 4.600 eura");

        lista2.Add("318i, Cijena: 8000 eura");


        foreach (string druga in lista2)

        {         

            Console.WriteLine(druga);

            Console.WriteLine(ispis2.Opis());

            Console.WriteLine(ispis3.Opis2());

        }

    }

    public static void Serija5()

    {


        Boje2 ispis4 = new Boje2();

        

        ArrayList lista3 = new ArrayList();

        lista3.Add("535i, Cijena: 7.900 eura");

        lista3.Add("530d, Cijena: 18.550 eura");

        lista3.Add("M5, Cijena: 49.900 eura");

        lista3.Add("525d, Cijena: 39.487 eura");

        lista3.Add("530d Automatik, Cijena: 16.200 eura");

        lista3.Add("530 ix Automatik, Cijena: 25.900 eura");


        foreach (string treca in lista3)

        {

            Console.WriteLine(treca);

            Console.WriteLine(ispis4.Opis2());

        }

    }

}


static class Maserati

{

    public static void MaseratiCijene()

    {


        Boje1 ispis5 = new Boje1();


        ArrayList listaM1 = new ArrayList();


        listaM1.Add("Maserati Biturbo RACING, Cijena: 9.000 eura");

        listaM1.Add("Maserati 222 Biturbo, Cijena: 7.000 eura");

        listaM1.Add("Maserati 420 Limuzina, Cijena: 1.000 eura");

        listaM1.Add("Maserati 430 2.8 V6 Biturbo, Cijena: 8.000 eura");

        listaM1.Add("Maserati Ghibli, Cijena: 18.000 eura");

        listaM1.Add("Maserati Granturismo, Cijena: 93.000 eura");

        listaM1.Add("Maserati Quattroporte SportGT, Cijena: 75.000 eura");


        foreach (string Mas in listaM1)

        {

            Console.WriteLine(Mas);

            Console.WriteLine(ispis5.Opis());

        }

    }

}


static class Fiat

{

    public static void Punto()

    {


        Boje2 ispis6 = new Boje2();


        ArrayList Pt = new ArrayList();


        Pt.Add("Fiat Punto 1.2, Cijena: 4.590 eura");

        Pt.Add("Fiat Punto 55s, Cijena: 1.100 eura");

        Pt.Add("Fiat Punto 1.4, Cijena: 800 eura");


        foreach (string Pun in Pt)

        {

            Console.WriteLine(Pun);

            Console.WriteLine(ispis6.Opis2());

        }

    }

    public static void Panda()

    {


        Boje1 ispis7 = new Boje1();

        

        ArrayList Pa = new ArrayList();


        Pa.Add("Fiat Panda 1.2, Cijena: 6.120 eura");

        Pa.Add("Fiat Panda 1.3 JTD 4x4, Cijena: 6.999 eura");

        Pa.Add("Fiat Panda 1.3 Multijet 16V, Cijena: 5.350 eura");


        foreach (string Pan in Pa)

        {

            Console.WriteLine(Pan);

            Console.WriteLine(ispis7.Opis());

        }

    }

}

class Ispis

{

    static void Main()

    {

        int broj;


        Console.WriteLine("Sve cijene su sa stranice www.njuskalo.hr");

        Console.WriteLine("Datum pregleda: 19.05.2011.");




        Console.WriteLine("Odaberite:");

        Console.WriteLine("1. Cijenik BMW serije 1");

        Console.WriteLine("2. Cijenik BMW serije 2");

        Console.WriteLine("3. Cijenik BMW serije 5");

        Console.WriteLine("4. Cijenik Maserati-ja");

        Console.WriteLine("5. Cijenik Fiat Punto");

        Console.WriteLine("6. Cijenik Fiat Panda");


        bool provjera;


        provjera = true;


        while (provjera == true)

        {

            if (Int32.TryParse(Console.ReadLine(), out broj))

            {

                if (broj == 1)

                {

                    BMW.Serija1();

                }

                else if (broj == 2)

                {

                    BMW.Serija3();

                }

                else if (broj == 3)

                {

                    BMW.Serija5();

                }

                else if (broj == 4)

                {

                    Maserati.MaseratiCijene();

                }

                else if (broj == 5)

                {

                    Fiat.Punto();

                }

                else if (broj == 6)

                {

                    Fiat.Panda();

                }

                else

                {

                    Console.WriteLine("Odaberite drugi broj!");

                }

                

            }

            else

            {

                Console.WriteLine("Niste unijeli broj");

            }


            

        }


    }

}


can someone explain me meaning if this keyword and Boja and Boja1 method

#2
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
Hey Tonchi,
I'm not sure if I fully understand what the question is, or if you're asking the question right. =)
But, a property is something of a developer aid, that I've only seen in .NET and VB. It works like a method, but it's accessed like a variable.

something like this in C#:


private string m_val;


public string Value {

   get {

      return m_val;

   } set {

      m_val = value;

   }

}


would simply mean that you can read from the property Value, and you can write to the property Value.
A read from the property would read from the private variable m_val. Similarly a write to it, would write to that variable.

Why? Well a couple of reasons.

You can control access to that variable. For example, you can make it write only, or read only, simply by omitting either the Get or Set Accessor.

Secondly, you can build additional logic into the property, like say, for example, if you wanted any assignment to a variable logged for auditing purposes, or you needed an even fired whenever that variable is read.


private string m_val;


public string Value {

   get {

      VariableRead("m_val"); //assuming VariableRead was an event to raise

      return m_val;

   } set {

      m_val = value;

      log("m_val set to " + value); //assuming log was a method that would log that

   }

}


We see these patterns throughout many languages. For example, I believe that would roughly translate to this in java.



   private String m_val;


   public  String getValue() {

      return m_val;

   } 

   public void setValue(String value) {

      m_val = value;

   }

}



hope that helps

#3
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
Oh, and there are some nice shorthands to this as well since .NET 3 or 3.5.

Automatic Properties are one of my favorite newer features. You can shorthand basic properties like this.



//Standard read/write

public string Value { get; set; }



//Public Read, Private Wriate

public string value { get; private set; }



well you get the idea. When writing it this way, you don't need to frame out the private variables etc. And you can 'upgrade' these to standard properties at any time when the need suits, without having to refactor any other code.

Anyways, I'm done talking about properties... What's next? :cool:

#4
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
hehehe but there is still one more question for you...
you said that since .NET 3 and 3.5 I can managed automatic properties like this

//Standard read/write

public string Value { get; set; }



//Public Read, Private Wriate

public string value { get; private set; }

why get and set in that example doesn't have any values???

#5
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
it will work.
See it's called automatic properties, because it will automatically infer what it is you mean.
for example,

public string Value { get; set; }


would mean this

private string _Value;

public string Value { get { return _Value; } set { _Value = value; } }


its a short hand that in some cases can save a lot of typing, and ultimately remove redundancy, and make your code more readable.

example:

class Person {

public string FirstName { get; set; }

public string LastName { get; set; }

public string AddressStreet1{ get; set; }

public string AddressStreet2{ get; set; }

public string AddressCity{ get; set; }

public string AddressProvince{ get; set; }

public string AddressCountry{ get; set; }

public string AddressPostalCode{ get; set; }

public string ContactPhone1{ get; set; }

public string ContactPhone2{ get; set; }

public string ContactEmail{ get; set; }

public string Notes{ get; set; }

}


in this example, you just saved yourself from mapping out 12 variables to properties, and with no performance hit at all. The code is cleaner, more readable.

#6
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
tny for explaining :D

#7
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
No Problem!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users