-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm_delegate.cs
More file actions
119 lines (95 loc) · 2.66 KB
/
vm_delegate.cs
File metadata and controls
119 lines (95 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Collections.Generic;
namespace Demo
{
public class Program
{
public static void Main(string[] args)
{
View v = new View();
v.Add("5");
v.Remove("5");
}
}
public delegate void Callback(int index);
// ######################
public class ViewNode{
private string str;
public ViewNode(string inStr){
str = inStr;
}
public string GetValue(){
return str;
}
}
// ######################
public class ModelNode{
private int num;
private int ind;
private Callback callback;
public ModelNode(int inNum, Callback f, int index){
num = inNum;
callback = new Callback(f);
ind = index;
}
public void Destroy(){
callback(ind);
}
public int GetValue(){
return num;
}
}
// ######################
public class View{
private List<ViewNode> list;
private Model model;
public View(){
list = new List<ViewNode>();
model = new Model();
}
public void Add(string s){
int n;
bool l = Int32.TryParse(s, out n);
if(!l) return;
ViewNode vn = new ViewNode(s);
list.Add(vn);
model.Add(n, Destroy, list.Count-1);
}
public void Remove(string s){
int n;
bool l = Int32.TryParse(s,out n);
if(!l) return;
model.Remove(n);
}
public void Destroy(int n){
Console.WriteLine("destroy: {0}", list[n].GetValue());
list.RemoveAt(n);
}
}
// ######################
public class Model{
private List<ModelNode> list;
public Model(){
list = new List<ModelNode>();
}
public void Add(int n, Callback c, int index){
ModelNode mn = new ModelNode(n, c, index);
list.Add(mn);
}
public void Remove(int n){
int i = GetIndex(n);
if(i != -1){
list[i].Destroy();
list.RemoveAt(i);
}
}
private int GetIndex(int n){
for(int i = 0; i<list.Count; i++){
if(list[i].GetValue() == n){
return i;
}
}
return -1;
}
}
}