Login Login
MORE

Difference between revisions of "CSharp:Attributi e Properties di oggetti"

From Aino Wiki

Jump to: navigation, search
(Descrizione)
(Descrizione)
 
Line 34: Line 34:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
Questa differenza è IMPRESCINDIBILE se la classe è sottoposta ad analisi\automatismi che ne controlla le risorse è per l'analisi di Attributi piuttosto che Properties si useranno istruzioni diverse.<br />
 
Questa differenza è IMPRESCINDIBILE se la classe è sottoposta ad analisi\automatismi che ne controlla le risorse è per l'analisi di Attributi piuttosto che Properties si useranno istruzioni diverse.<br />
Altro esempio fondamentale:
+
Altro esempio fondamentale di "accesso" alle proprietà attraverso delle variabili private:
  
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
Line 46: Line 46:
 
     set { _name = value; }
 
     set { _name = value; }
 
   }
 
   }
 +
}
 +
</syntaxhighlight>
 +
Altro esempio (da Microsoft) simile ma con altra notazione:
 +
 +
<syntaxhighlight lang="csharp">
 +
public class Date
 +
{
 +
    private int _month = 7;  // Backing store
 +
 +
    public int Month
 +
    {
 +
        get => _month;
 +
        set
 +
        {
 +
            if ((value > 0) && (value < 13))
 +
            {
 +
                _month = value;
 +
            }
 +
        }
 +
    }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 19:48, 23 March 2025

Un duplicato con info che si aggiungono a quanto segue è: Attributi delle proprietà e metodi
Teroria:

Descrizione

Semplicemente sono caratteristiche di oggetti, gli Attributes (Attributi) sono variabili pubbliche alla classe/oggetto come
    public class UserLogin
    {
        public string UserName;
        public string Password;
        public string Domain;
    }

Mentre le Properties (proprietà) sono anche pubbliche ma son definite con i costruttori get e set:

    public class UserLogin
    {
        public string UserName
        {
            get;
            set;
        }
        public string Password
        {
            get;
            set;
        }
        public string Domain
        {
            get;
            set;
        }
    }

Questa differenza è IMPRESCINDIBILE se la classe è sottoposta ad analisi\automatismi che ne controlla le risorse è per l'analisi di Attributi piuttosto che Properties si useranno istruzioni diverse.
Altro esempio fondamentale di "accesso" alle proprietà attraverso delle variabili private:

public class Autore
{
  private string _nome; // Campo "nascosto" usato per tramite
 
  public string Nome   // properità
  {
    get { return _name; }
    set { _name = value; }
  }
}

Altro esempio (da Microsoft) simile ma con altra notazione:

public class Date
{
    private int _month = 7;  // Backing store
 
    public int Month
    {
        get => _month;
        set
        {
            if ((value > 0) && (value < 13))
            {
                _month = value;
            }
        }
    }
}

Attributi di proprità, DataAnnotations

Si chiamano tecnicamente DataAnnotations, sono delle descrizioni associate a ciascuna Properties utile per associare un determinato comportamento in fase di esecuzione. Notare i seguenti [Required]:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;// necessario per le dataannotations
//...
    public class UserLogin
    {
        [Required (ErrorMessage = "Campo Cognome obbligatorio")]
        [Display(Name = "Nome utente")]
        public string UserName
        {
            get;
            set;
        }
        [Required]
        public string Password
        {
            get;
            set;
        }
        public string Domain
        {
            get;
            set;
        }
    }

Altro esempio di Attributi di Properties:

    public class UserModels
    {
        [Required (ErrorMessage = "Campo Cognome obbligatorio")]
        [DisplayName ("Cognome")]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }
        public string Address { get; set; }
        [Required]
        [StringLength (50)]
        public string Email { get; set; }
        [DataType (DataType.Date)]
        public DateTime DOB { get; set; }
        [Range (10,100000)]
        public decimal Salary { get; set; }     
    }

C# | Teoria


CSharp:Automatismo caricamento Entità | XML Serializzazione


Visual Studio