質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

解決済

3回答

878閲覧

if文がなぜかコンパイルエラーが起きるC#

mercurian-teto

総合スコア75

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2017/10/15 19:21

編集2017/10/16 03:59

このサイトにあるプログラムなんですが、(参照先のプログラムを参考にしてください)
フォームアプリとして起動して、リストボックスにある項目を選択すると
イベントハンドラーが発動するようになっていると思います。
イベントハンドラーの内容としていろんな状態を表すもの?となっています。
たとえば、bootmodeなら通常起動かセーフモードかを判別して値を返すようになっています。
それで質問なんですが

listboxのbootmodeを選択して、Normalの値が帰った時、何かしら実行するようにしたいのです。それで、

C#

1 prop = pi[i]; 2 break; 3 } 4 object propval = prop.GetValue(null, null); 5 textBox1.Text += "\r\nThe value of the "+propname+" property is: "+propval.ToString(); 6

この部分のコードの下に

C#

1if(propval.Tostring() == "Normal"){ 2Console.WriteLine("hello!") 3} 4コード

以上のようなコードを加えると
stringを暗黙でbool形に変換できませんとエラーが出てきます。

listboxのbootmodeを選択して、Normalの値が帰った時、何かしら実行するプログラムを作りたいので、だれか適切な助言をください。
※補足
以下のコードは実際打ったコードの全体です。

C#

1using System; 2using System.Collections; 3using System.ComponentModel; 4using System.Drawing; 5using System.Reflection; 6using System.Windows.Forms; 7 8namespace jikannwoenntyousuru 9{ 10 public class SystemInfoBrowserForm : System.Windows.Forms.Form 11 { 12 private System.Windows.Forms.ListBox listBox1; 13 private System.Windows.Forms.TextBox textBox1; 14 15 public SystemInfoBrowserForm() 16 { 17 this.SuspendLayout(); 18 InitForm(); 19 20 // Add each property of the SystemInformation class to the list box. 21 Type t = typeof(System.Windows.Forms.SystemInformation); 22 PropertyInfo[] pi = t.GetProperties(); 23 for (int i = 0; i < pi.Length; i++) 24 listBox1.Items.Add(pi[i].Name); 25 textBox1.Text = "The SystemInformation class has " + pi.Length.ToString() + " properties.\r\n"; 26 27 // Configure the list item selected handler for the list box to invoke a 28 // method that displays the value of each property. 29 listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged); 30 this.ResumeLayout(false); 31 } 32 33 private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 34 { 35 // Return if no list item is selected. 36 if (listBox1.SelectedIndex == -1) return; 37 // Get the property name from the list item. 38 string propname = listBox1.Text; 39 40 if (propname == "PowerStatus") 41 { 42 // Cycle and display the values of each property of the PowerStatus property. 43 textBox1.Text += "\r\nThe value of the PowerStatus property is:"; 44 Type t = typeof(System.Windows.Forms.PowerStatus); 45 PropertyInfo[] pi = t.GetProperties(); 46 for (int i = 0; i < pi.Length; i++) 47 { 48 object propval = pi[i].GetValue(SystemInformation.PowerStatus, null); 49 textBox1.Text += "\r\n PowerStatus." + pi[i].Name + " is: " + propval.ToString(); 50 } 51 } 52 else 53 { 54 // Display the value of the selected property of the SystemInformation type. 55 Type t = typeof(System.Windows.Forms.SystemInformation); 56 PropertyInfo[] pi = t.GetProperties(); 57 PropertyInfo prop = null; 58 for (int i = 0; i < pi.Length; i++) 59 if (pi[i].Name == propname) 60 { 61 prop = pi[i]; 62 break; 63 } 64 object propval = prop.GetValue(null, null); 65 textBox1.Text += "\r\nThe value of the " + propname + " property is: " + propval.ToString(); 66 if (propval.Tostring() == "Normal") 67 { 68 Console.WriteLine("hello!"); 69 } 70 71 72 } 73 } 74 75 private void InitForm() 76 { 77 // Initialize the form settings 78 this.listBox1 = new System.Windows.Forms.ListBox(); 79 this.textBox1 = new System.Windows.Forms.TextBox(); 80 this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 81 | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); 82 this.listBox1.Location = new System.Drawing.Point(8, 16); 83 this.listBox1.Size = new System.Drawing.Size(172, 496); 84 this.listBox1.TabIndex = 0; 85 this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 86 | System.Windows.Forms.AnchorStyles.Right))); 87 this.textBox1.Location = new System.Drawing.Point(188, 16); 88 this.textBox1.Multiline = true; 89 this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; 90 this.textBox1.Size = new System.Drawing.Size(420, 496); 91 this.textBox1.TabIndex = 1; 92 this.ClientSize = new System.Drawing.Size(616, 525); 93 this.Controls.Add(this.textBox1); 94 this.Controls.Add(this.listBox1); 95 this.Text = "Select a SystemInformation property to get the value of"; 96 } 97 98 [STAThread] 99 static void Main() 100 { 101 Application.Run(new SystemInfoBrowserForm()); 102 } 103 } 104} 105

コピーしてみて試してみてください。
ただし、これを実行したらなぜかエラーが

CS1061 'object' に 'Tostring' の定義が含まれておらず、型 'object' の最初の引数を受け付ける拡張メソッド 'Tostring' が見つかりませんでした。using ディレクティブまたはアセンブリ参照が不足していないことを確認してください。 66 アクティブ
と変更されていました。

ソリューションエクスプローラーの画面です。
イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

KSwordOfHaste

2017/10/16 23:37

motuoさんにBA(ベストアンサー)をつけて質問をきちんと閉じましょう。 マナーですので。
mercurian-teto

2017/10/17 08:23

すいません、お手数をおかけしました。
guest

回答3

0

ベストアンサー

if(propval.Tostring() == "Normal"){では、Tostringとsが小文字になっています。

if (propval.ToString() == "Normal")とsを大文字にするだけでエラーは無くなるかと思いますが…

投稿2017/10/16 04:07

motuo

総合スコア3027

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mercurian-teto

2017/10/16 05:32

エラーは無事解除されました。
mercurian-teto

2017/10/16 05:33

回答ありがとうございました。
guest

0

Tostring はtypoに見えるのでまずはToStringに訂正したら解消されないでしょうか?

普通は未定義エラーになると思います。objectにbool型の拡張メソッドTostringが定義してあればそういう感じのエラーになる気もしますが、そんなことになっているとは思えないし・・・

なぜそういうエラーになったのか不思議です。

投稿2017/10/16 00:09

KSwordOfHaste

総合スコア18394

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mercurian-teto

2017/10/16 03:57

回答ありがとうございます。 実際に書いたコードを※補足で書いてあります。 また補足に書いてあるように新しいエラー画面が出るようになりました。 visual studio 2017で作成しているのですが、補足に添付した通りにファイルを作成しております。
KSwordOfHaste

2017/10/16 04:17

motuoさんがコメントされてますが、typoというのは「綴りミス」のことです。自分は'S'が小文字になっている点を指摘したつもりだったのですが、わかりにくかったかな・・・
mercurian-teto

2017/10/16 05:31

S'が小文字になっている点に気づきませんでした。すいませんでした。
mercurian-teto

2017/10/16 05:32

エラーは無事解除されました。
mercurian-teto

2017/10/16 05:33

回答ありがとうございました。
guest

0

質問に記載頂いたコードでは同じようなエラーにならないのではないかと思います。
推測になりますが、実際の記述が以下のようなコードになっている可能性があります。

C#

1if (propval.Tostring() = "Normal") { 2 Console.WriteLine("hello!"); 3}

=が1つだと代入となり、if文の判定条件として上記だと文字列"Normal"を使用する形になります。
if( propname == "PowerStatus" )のように、比較には==を用います。

投稿2017/10/15 21:13

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mercurian-teto

2017/10/16 03:57

回答ありがとうございます。 実際に書いたコードを※補足で書いてあります。 また補足に書いてあるように新しいエラー画面が出るようになりました。 visual studio 2017で作成しているのですが、補足に添付した通りにファイルを作成しております。
退会済みユーザー

退会済みユーザー

2017/10/16 04:09

エラーの内容はKSwordOfHasteさんやmotuoさんのご回答の通りかと思います。
mercurian-teto

2017/10/16 05:32

エラーは無事解除されました。回答ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問