質問するログイン新規登録

回答編集履歴

3

ごじていせい

2018/06/26 02:17

投稿

退会済みユーザー
answer CHANGED
@@ -130,7 +130,7 @@
130
130
 
131
131
  **【追伸2】**
132
132
 
133
- ststic class でのコンストラクタの定義の記事です。記事の中に MSDN へのリンクも張ってありますので見てください。
133
+ static class でのコンストラクタの定義の記事です。記事の中に MSDN へのリンクも張ってありますので見てください。
134
134
 
135
135
  C# static class constructor
136
136
  [https://stackoverflow.com/questions/6721832/c-sharp-static-class-constructor](https://stackoverflow.com/questions/6721832/c-sharp-static-class-constructor)

2

ついしん2

2018/06/26 02:17

投稿

退会済みユーザー
answer CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  **【追伸】**
4
4
 
5
+ **訂正**:YAmaGNZ さんのご指摘の通り、以下の例はインスタンスクラスで質問者さんの言われる「Staticなクラス内」ではないです。すみません。【追伸2】に「Staticなクラス内」の例を書いておきます。
6
+
5
7
  言葉だけではピンとこないと思いますので、Windows Forms アプリで使われているコード例をアップしておきます。(質問者さんのケースではこのようにできるかどうかは分かりませんが)
6
8
 
7
9
  Program.cs の Main メソッドがエントリーポイントで、そこで Form4() というコンストラクタを使って Form を初期化すると、Form4.cs に定義されている Form4() コンストラクタで InitializeComponent() メソッドが起動され、Form 上に配置されたコントロール類が初期化されます。
@@ -123,4 +125,43 @@
123
125
  private System.Windows.Forms.PictureBox pictureBox1;
124
126
  }
125
127
  }
128
+ ```
129
+
130
+
131
+ **【追伸2】**
132
+
133
+ ststic class でのコンストラクタの定義の記事です。記事の中に MSDN へのリンクも張ってありますので見てください。
134
+
135
+ C# static class constructor
136
+ [https://stackoverflow.com/questions/6721832/c-sharp-static-class-constructor](https://stackoverflow.com/questions/6721832/c-sharp-static-class-constructor)
137
+
138
+ 具体例も書いておきます。
139
+
140
+ ```
141
+ namespace ConsoleApplication4
142
+ {
143
+ static class YourClass
144
+ {
145
+ static string s = null;
146
+
147
+ static YourClass()
148
+ {
149
+ // perform initialization here
150
+ s = "test string";
151
+ }
152
+
153
+ public static string Method1()
154
+ {
155
+ return s;
156
+ }
157
+ }
158
+
159
+ class Program
160
+ {
161
+ static void Main(string[] args)
162
+ {
163
+ var x = YourClass.Method1();
164
+ }
165
+ }
166
+ }
126
167
  ```

1

追伸

2018/06/26 02:15

投稿

退会済みユーザー
answer CHANGED
@@ -1,1 +1,126 @@
1
- 全体的なやりたいことが不明なので外れかもしれませんが、コンストラクターでなんとかできないですか?
1
+ 全体的なやりたいことが不明なので外れかもしれませんが、コンストラクターでなんとかできないですか?
2
+
3
+ **【追伸】**
4
+
5
+ 言葉だけではピンとこないと思いますので、Windows Forms アプリで使われているコード例をアップしておきます。(質問者さんのケースではこのようにできるかどうかは分かりませんが)
6
+
7
+ Program.cs の Main メソッドがエントリーポイントで、そこで Form4() というコンストラクタを使って Form を初期化すると、Form4.cs に定義されている Form4() コンストラクタで InitializeComponent() メソッドが起動され、Form 上に配置されたコントロール類が初期化されます。
8
+
9
+ この InitializeComponent() メソッドが質問者さんの言う「必ず呼ばれる関数」に該当します。
10
+
11
+ **Program.cs**
12
+
13
+ ```
14
+ using System;
15
+ using System.Collections.Generic;
16
+ using System.Linq;
17
+ using System.Threading.Tasks;
18
+ using System.Windows.Forms;
19
+
20
+ namespace WindowsFormsApplication1
21
+ {
22
+ static class Program
23
+ {
24
+ /// <summary>
25
+ /// アプリケーションのメイン エントリ ポイントです。
26
+ /// </summary>
27
+ [STAThread]
28
+ static void Main()
29
+ {
30
+ Application.EnableVisualStyles();
31
+ Application.SetCompatibleTextRenderingDefault(false);
32
+ Application.Run(new Form4());
33
+ }
34
+ }
35
+ }
36
+ ```
37
+
38
+ **Form4.cs**
39
+
40
+ ```
41
+ using System;
42
+ using System.Collections.Generic;
43
+ using System.ComponentModel;
44
+ using System.Data;
45
+ using System.Drawing;
46
+ using System.Linq;
47
+ using System.Text;
48
+ using System.Threading.Tasks;
49
+ using System.Windows.Forms;
50
+
51
+ namespace WindowsFormsApplication1
52
+ {
53
+ public partial class Form4 : Form
54
+ {
55
+ public Form4()
56
+ {
57
+ InitializeComponent();
58
+ }
59
+ }
60
+ }
61
+ ```
62
+
63
+ **Form4.Designer.cs**
64
+
65
+ ```
66
+ namespace WindowsFormsApplication1
67
+ {
68
+ partial class Form4
69
+ {
70
+ /// <summary>
71
+ /// Required designer variable.
72
+ /// </summary>
73
+ private System.ComponentModel.IContainer components = null;
74
+
75
+ /// <summary>
76
+ /// Clean up any resources being used.
77
+ /// </summary>
78
+ /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
79
+ protected override void Dispose(bool disposing)
80
+ {
81
+ if (disposing && (components != null))
82
+ {
83
+ components.Dispose();
84
+ }
85
+ base.Dispose(disposing);
86
+ }
87
+
88
+ #region Windows Form Designer generated code
89
+
90
+ /// <summary>
91
+ /// Required method for Designer support - do not modify
92
+ /// the contents of this method with the code editor.
93
+ /// </summary>
94
+ private void InitializeComponent()
95
+ {
96
+ this.pictureBox1 = new System.Windows.Forms.PictureBox();
97
+ ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
98
+ this.SuspendLayout();
99
+ //
100
+ // pictureBox1
101
+ //
102
+ this.pictureBox1.Location = new System.Drawing.Point(13, 13);
103
+ this.pictureBox1.Name = "pictureBox1";
104
+ this.pictureBox1.Size = new System.Drawing.Size(100, 50);
105
+ this.pictureBox1.TabIndex = 0;
106
+ this.pictureBox1.TabStop = false;
107
+ //
108
+ // Form4
109
+ //
110
+ this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
111
+ this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
112
+ this.ClientSize = new System.Drawing.Size(262, 120);
113
+ this.Controls.Add(this.pictureBox1);
114
+ this.Name = "Form4";
115
+ this.Text = "Form4";
116
+ ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
117
+ this.ResumeLayout(false);
118
+
119
+ }
120
+
121
+ #endregion
122
+
123
+ private System.Windows.Forms.PictureBox pictureBox1;
124
+ }
125
+ }
126
+ ```