はじめに
今回は、 以下は、 このクラスは、 インターフェイスとは、 なお、 以下は、 このメソッドでは、 protectedアクセス修飾子は、 virtualキーワードは、 例えば、 これによってOverrideSampleオブジェクトでOnPropertyChangeメソッドを実行すると上記の処理が実行されます。 次に、 すでに、 このクラスの持つインターフェイスは、 つまりはCodeにセットされた値に対して、 Readによって得られた値はRowプロパティによってRowオブジェクトとして提供されます。
また、 前回移植したC#のコードでは、 このクラスでは、 Form1クラスは、 今までに作成したクラスを使って、 上記のコードを眺めることで、 前回移植したコードでは、 しかし、 つまり、 コンストラクタにある以下のコードが、 このコードを見て、 詳細についてcodeTextBoxを例にとって順にご説明します。 codeTextBoxにオブジェクトをバインディングしています。
従って、 しかし、 バインディングしたオブジェクトは、 このイベントによってcodeTextBoxは、 つまり、 以下は、 これによって、 発生する例外エラーで渡されるオブジェクトはException型だけではありませんが、 今回作成したクラスダイアグラムは次のようになっています。 前回から2回にわたって、 次回はIDEの機能や既存のクラスを利用した省力化の方法をご紹介します。イベントを発生させるクラスを作る
using System.ComponentModel;
namespace WindowsFormsApplication1
{
//*************************************************************
/// <summary>値が変わったことを通知するクラスです</summary>
//*************************************************************
class NotifyPropertyChanged : INotifyPropertyChanged
{
//*************************************************************
/// <summary>PropertyChanged イベント</summary>
//*************************************************************
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName) {
if (this.PropertyChanged == null) return;
object sender = this;
PropertyChangedEventArgs e
= new PropertyChangedEventArgs(propertyName);
this.PropertyChanged(sender, e);
}
}
}
インターフェイスの継承
public event PropertyChangedEventHandler PropertyChanged;
イベント発生メソッド
protected virtual void OnPropertyChanged(string propertyName) {
if (this.PropertyChanged == null) return;
object sender = this;
PropertyChangedEventArgs e
= new PropertyChangedEventArgs(propertyName);
this.PropertyChanged(sender, e);
}
class OverrideSample : NotifyPropertyChanged {
protected override void OnPropertyChanged(string propertyName) {
if (propertyName == "") return;
base.OnPropertyChanged(propertyName);
}
}
ロジックのクラスを作る
using System;
namespace WindowsFormsApplication1
{
//*************************************************************
/// <summary>電話帳サンプルコンポーネント</summary>
//*************************************************************
class Logic : NotifyPropertyChanged
{
private Adapter adapter;
//*************************************************************
/// <summary>コンストラクタ</summary>
//*************************************************************
public Logic() {
this.adapter = new Adapter();
}
//*************************************************************
/// <summary>Code プロパティ</summary>
//*************************************************************
private int _Code = 0;
public int Code {
get { return this._Code; }
set {
if (this._Code == value) return;
this._Code = value;
base.OnPropertyChanged("Code");
}
}
//*************************************************************
///<summary>Row プロパティ</summary>
//*************************************************************
private Row _Row = new Row();
public Row Row {
get { return this._Row; }
private set { this._Row = value; }
}
//*************************************************************
/// <summary>追加処理を実行します</summary>
//*************************************************************
public void Add() {
this.InvalidCode();
if (this.adapter.Read(this.Code, this.Row)) {
throw new Exception("既に登録されている番号です");
}
this.adapter.Add(this.Code);
this.Read();
}
//*************************************************************
/// <summary>更新処理を実行します</summary>
//*************************************************************
public void Update() {
this.InvalidCode();
this.adapter.Update(this.Row);
}
//*************************************************************
/// <summary>削除処理を実行します</summary>
//*************************************************************
public void Delete() {
this.InvalidCode();
this.adapter.Delete(this.Code);
this.Code = 0;
this.Row.Clear();
}
//*************************************************************
/// <summary>読込み処理を実行します</summary>
//*************************************************************
public void Read() {
if (!this.adapter.Read(this.Code, this.Row)) {
this.Row.Clear();
throw new Exception("番号が見つかりません");
}
}
//*************************************************************
/// <summary>番号値が有効かどうかを検証した結果を取得します
/// </summary>
//*************************************************************
private void InvalidCode() {
if (this.Code > 0 || this.Code < 100000000) return;
throw new Exception("番号が有効な値ではありません");
}
}
}
クラスの概要
例外エラー
ユーザーインターフェイスのクラスを作る
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
//*************************************************************
/// <summary>電話帳サンプルソフトフォーム</summary>
//*************************************************************
public partial class Form1 : Form
{
private Logic logic;
//*************************************************************
///<summary>コンストラクタ</summary>
//*************************************************************
public Form1() {
this.InitializeComponent();
this.logic = new Logic();
this.codeTextBox.DataBindings.Add(
"Text", this.logic, "Code");
this.nameTextBox.DataBindings.Add(
"Text", this.logic.Row, "Name");
this.telephoneNumberTextBox.DataBindings.Add(
"Text", this.logic.Row, "TelephoneNumber");
}
//*************************************************************
// イベント処理用メソッド
//*************************************************************
private void addButton_Click(object sender, EventArgs e) {
try {
this.logic.Add();
}
catch(Exception ex) {
MessageBox.Show(ex.Message);
}
}
private void closeButton_Click(object sender, EventArgs e) {
this.Close();
}
private void readButton_Click(object sender, EventArgs e) {
try {
this.logic.Read();
}
catch(Exception ex) {
MessageBox.Show(ex.Message);
}
}
private void updateButton_Click(object sender, EventArgs e) {
try {
this.logic.Update();
}
catch(Exception ex) {
MessageBox.Show(ex.Message);
}
}
private void deleteButton_Click(object sender, EventArgs e) {
try {
this.logic.Delete();
}
catch(Exception ex) {
MessageBox.Show(ex.Message);
}
}
}
}
データバインディング
this.codeTextBox.DataBindings.Add(
"Text", this.logic, "Code");
this.nameTextBox.DataBindings.Add(
"Text", this.logic.Row, "Name");
this.telephoneNumberTextBox.DataBindings.Add(
"Text", this.logic.Row, "TelephoneNumber");
コマンドの実行
try {
this.logic.Add();
}
catch(Exception ex) {
MessageBox.Show(ex.Message);
}
今回作成したクラスダイアグラム
次回の予定