回答編集履歴

3

ごじていせい

2018/06/26 02:17

投稿

退会済みユーザー
test CHANGED
@@ -262,7 +262,7 @@
262
262
 
263
263
 
264
264
 
265
- ststic class でのコンストラクタの定義の記事です。記事の中に MSDN へのリンクも張ってありますので見てください。
265
+ static class でのコンストラクタの定義の記事です。記事の中に MSDN へのリンクも張ってありますので見てください。
266
266
 
267
267
 
268
268
 

2

ついしん2

2018/06/26 02:17

投稿

退会済みユーザー
test CHANGED
@@ -6,6 +6,10 @@
6
6
 
7
7
 
8
8
 
9
+ **訂正**:YAmaGNZ さんのご指摘の通り、以下の例はインスタンスクラスで質問者さんの言われる「Staticなクラス内」ではないです。すみません。【追伸2】に「Staticなクラス内」の例を書いておきます。
10
+
11
+
12
+
9
13
  言葉だけではピンとこないと思いますので、Windows Forms アプリで使われているコード例をアップしておきます。(質問者さんのケースではこのようにできるかどうかは分かりませんが)
10
14
 
11
15
 
@@ -249,3 +253,81 @@
249
253
  }
250
254
 
251
255
  ```
256
+
257
+
258
+
259
+
260
+
261
+ **【追伸2】**
262
+
263
+
264
+
265
+ ststic class でのコンストラクタの定義の記事です。記事の中に MSDN へのリンクも張ってありますので見てください。
266
+
267
+
268
+
269
+ C# static class constructor
270
+
271
+ [https://stackoverflow.com/questions/6721832/c-sharp-static-class-constructor](https://stackoverflow.com/questions/6721832/c-sharp-static-class-constructor)
272
+
273
+
274
+
275
+ 具体例も書いておきます。
276
+
277
+
278
+
279
+ ```
280
+
281
+ namespace ConsoleApplication4
282
+
283
+ {
284
+
285
+ static class YourClass
286
+
287
+ {
288
+
289
+ static string s = null;
290
+
291
+
292
+
293
+ static YourClass()
294
+
295
+ {
296
+
297
+ // perform initialization here
298
+
299
+ s = "test string";
300
+
301
+ }
302
+
303
+
304
+
305
+ public static string Method1()
306
+
307
+ {
308
+
309
+ return s;
310
+
311
+ }
312
+
313
+ }
314
+
315
+
316
+
317
+ class Program
318
+
319
+ {
320
+
321
+ static void Main(string[] args)
322
+
323
+ {
324
+
325
+ var x = YourClass.Method1();
326
+
327
+ }
328
+
329
+ }
330
+
331
+ }
332
+
333
+ ```

1

追伸

2018/06/26 02:15

投稿

退会済みユーザー
test CHANGED
@@ -1 +1,251 @@
1
1
  全体的なやりたいことが不明なので外れかもしれませんが、コンストラクターでなんとかできないですか?
2
+
3
+
4
+
5
+ **【追伸】**
6
+
7
+
8
+
9
+ 言葉だけではピンとこないと思いますので、Windows Forms アプリで使われているコード例をアップしておきます。(質問者さんのケースではこのようにできるかどうかは分かりませんが)
10
+
11
+
12
+
13
+ Program.cs の Main メソッドがエントリーポイントで、そこで Form4() というコンストラクタを使って Form を初期化すると、Form4.cs に定義されている Form4() コンストラクタで InitializeComponent() メソッドが起動され、Form 上に配置されたコントロール類が初期化されます。
14
+
15
+
16
+
17
+ この InitializeComponent() メソッドが質問者さんの言う「必ず呼ばれる関数」に該当します。
18
+
19
+
20
+
21
+ **Program.cs**
22
+
23
+
24
+
25
+ ```
26
+
27
+ using System;
28
+
29
+ using System.Collections.Generic;
30
+
31
+ using System.Linq;
32
+
33
+ using System.Threading.Tasks;
34
+
35
+ using System.Windows.Forms;
36
+
37
+
38
+
39
+ namespace WindowsFormsApplication1
40
+
41
+ {
42
+
43
+ static class Program
44
+
45
+ {
46
+
47
+ /// <summary>
48
+
49
+ /// アプリケーションのメイン エントリ ポイントです。
50
+
51
+ /// </summary>
52
+
53
+ [STAThread]
54
+
55
+ static void Main()
56
+
57
+ {
58
+
59
+ Application.EnableVisualStyles();
60
+
61
+ Application.SetCompatibleTextRenderingDefault(false);
62
+
63
+ Application.Run(new Form4());
64
+
65
+ }
66
+
67
+ }
68
+
69
+ }
70
+
71
+ ```
72
+
73
+
74
+
75
+ **Form4.cs**
76
+
77
+
78
+
79
+ ```
80
+
81
+ using System;
82
+
83
+ using System.Collections.Generic;
84
+
85
+ using System.ComponentModel;
86
+
87
+ using System.Data;
88
+
89
+ using System.Drawing;
90
+
91
+ using System.Linq;
92
+
93
+ using System.Text;
94
+
95
+ using System.Threading.Tasks;
96
+
97
+ using System.Windows.Forms;
98
+
99
+
100
+
101
+ namespace WindowsFormsApplication1
102
+
103
+ {
104
+
105
+ public partial class Form4 : Form
106
+
107
+ {
108
+
109
+ public Form4()
110
+
111
+ {
112
+
113
+ InitializeComponent();
114
+
115
+ }
116
+
117
+ }
118
+
119
+ }
120
+
121
+ ```
122
+
123
+
124
+
125
+ **Form4.Designer.cs**
126
+
127
+
128
+
129
+ ```
130
+
131
+ namespace WindowsFormsApplication1
132
+
133
+ {
134
+
135
+ partial class Form4
136
+
137
+ {
138
+
139
+ /// <summary>
140
+
141
+ /// Required designer variable.
142
+
143
+ /// </summary>
144
+
145
+ private System.ComponentModel.IContainer components = null;
146
+
147
+
148
+
149
+ /// <summary>
150
+
151
+ /// Clean up any resources being used.
152
+
153
+ /// </summary>
154
+
155
+ /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
156
+
157
+ protected override void Dispose(bool disposing)
158
+
159
+ {
160
+
161
+ if (disposing && (components != null))
162
+
163
+ {
164
+
165
+ components.Dispose();
166
+
167
+ }
168
+
169
+ base.Dispose(disposing);
170
+
171
+ }
172
+
173
+
174
+
175
+ #region Windows Form Designer generated code
176
+
177
+
178
+
179
+ /// <summary>
180
+
181
+ /// Required method for Designer support - do not modify
182
+
183
+ /// the contents of this method with the code editor.
184
+
185
+ /// </summary>
186
+
187
+ private void InitializeComponent()
188
+
189
+ {
190
+
191
+ this.pictureBox1 = new System.Windows.Forms.PictureBox();
192
+
193
+ ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
194
+
195
+ this.SuspendLayout();
196
+
197
+ //
198
+
199
+ // pictureBox1
200
+
201
+ //
202
+
203
+ this.pictureBox1.Location = new System.Drawing.Point(13, 13);
204
+
205
+ this.pictureBox1.Name = "pictureBox1";
206
+
207
+ this.pictureBox1.Size = new System.Drawing.Size(100, 50);
208
+
209
+ this.pictureBox1.TabIndex = 0;
210
+
211
+ this.pictureBox1.TabStop = false;
212
+
213
+ //
214
+
215
+ // Form4
216
+
217
+ //
218
+
219
+ this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
220
+
221
+ this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
222
+
223
+ this.ClientSize = new System.Drawing.Size(262, 120);
224
+
225
+ this.Controls.Add(this.pictureBox1);
226
+
227
+ this.Name = "Form4";
228
+
229
+ this.Text = "Form4";
230
+
231
+ ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
232
+
233
+ this.ResumeLayout(false);
234
+
235
+
236
+
237
+ }
238
+
239
+
240
+
241
+ #endregion
242
+
243
+
244
+
245
+ private System.Windows.Forms.PictureBox pictureBox1;
246
+
247
+ }
248
+
249
+ }
250
+
251
+ ```