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

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

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

Microsoft Visual Basic .NETのことで、Microsoft Visual Basic(VB6)の後継。 .NET環境向けのプログラムを開発することができます。 現在のVB.NETでは、.NET Frameworkを利用して開発を行うことが可能です。

Q&A

解決済

3回答

1551閲覧

使用していない変数の宣言の効果

tokuo0

総合スコア12

VB.NET

Microsoft Visual Basic .NETのことで、Microsoft Visual Basic(VB6)の後継。 .NET環境向けのプログラムを開発することができます。 現在のVB.NETでは、.NET Frameworkを利用して開発を行うことが可能です。

0グッド

0クリップ

投稿2021/04/13 23:03

編集2021/04/13 23:26

使用していない変数の宣言を省略すると
実行時にエラーが発生します。
エラー発生の理由を教えて下さい。
よろしくお願いします。

Option Explicit Class bind Function f(a,b) f=b.f(a) End Function End Class Class pureget Private v Function f(x) v = x f = Me End Function End Class Class cw Private v Function f(x) Console.WriteLine(x.v) f = me End Function End Class Class pureput Function f(x) f = new cw f.v = x End Function End Class Class exec Function f(x) f =x.f(x.v) End Function End Class Module Program Sub Main() dim getput, bind, a, b, e bind = new bind a = new pureget b = new pureput a.f("Hello") getput = bind.f(a,b) e = new exec e.f(getput) End Sub End Module

“Class cw”の”Private v”を省略して実行すると以下のエラーが発生します。

Microsoft (R) Visual Basic Compiler version 3.6.0-4.20224.5 (ec77c100) Copyright (C) Microsoft Corporation. All rights reserved. Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.MissingMethodException: Method 'cw.v' not found. at System.RuntimeType.InvokeMember (System.String name, System.Reflection.BindingFlags bindingFlags, System.Reflection.Binder binder, System.Object target, System.Object[] providedArgs, System.Reflection.ParameterModifier[] modifiers, System.Globalization.CultureInfo culture, System.String[] namedParams) [0x00710] in <9f0df102fe6e4cfea29d2e46f585d8a5>:0 at Microsoft.VisualBasic.CompilerServices.LateBinding.LateSet (System.Object o, System.Type objType, System.String name, System.Object[] args, System.String[] paramnames) [0x00082] in <78b8fb9203f94730bb724970afd421d2>:0 at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSet (System.Object Instance, System.Type Type, System.String MemberName, System.Object[] Arguments, System.String[] ArgumentNames, System.Type[] TypeArguments) [0x00001] in <78b8fb9203f94730bb724970afd421d2>:0 at pureput.f (System.Object x) [0x00007] in :0 at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&) at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <9f0df102fe6e4cfea29d2e46f585d8a5>:0 --- End of inner exception stack trace --- at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00083] in <9f0df102fe6e4cfea29d2e46f585d8a5>:0 at System.RuntimeType.InvokeMember (System.String name, System.Reflection.BindingFlags bindingFlags, System.Reflection.Binder binder, System.Object target, System.Object[] providedArgs, System.Reflection.ParameterModifier[] modifiers, System.Globalization.CultureInfo culture, System.String[] namedParams) [0x006e7] in <9f0df102fe6e4cfea29d2e46f585d8a5>:0 at Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet (System.Object o, System.Type objType, System.String name, System.Object[] args, System.String[] paramnames, System.Boolean[] CopyBack) [0x00052] in <78b8fb9203f94730bb724970afd421d2>:0 at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet (System.Object Instance, System.Type Type, System.String MemberName, System.Object[] Arguments, System.String[] ArgumentNames, System.Type[] TypeArguments, System.Boolean[] CopyBack) [0x00001] in <78b8fb9203f94730bb724970afd421d2>:0 at bind.f (System.Object a, System.Object b) [0x00001] in :0 at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&) at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <9f0df102fe6e4cfea29d2e46f585d8a5>:0 --- End of inner exception stack trace --- 以下省略

以上ですが、よろしくお願いします。

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

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

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

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

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

ozwk

2021/04/13 23:21 編集

f = new cw f.v = x で使ってません?
退会済みユーザー

退会済みユーザー

2021/04/13 23:31 編集

option strict on にしたらどうなりますか?
tokuo0

2021/04/13 23:33

v と f.vは異なる変数なのでは?
退会済みユーザー

退会済みユーザー

2021/04/14 00:46

option strict on にしたらどうなりますか? ← 聞いてる? デフォルトの option strict off では議論するレベルにないと言っても過言ではないと個人的には思ってます。
退会済みユーザー

退会済みユーザー

2021/04/14 09:16

> v と f.vは異なる変数なのでは? 「f.v = x」はどこへ代入することを意図して書いたのでしょうか。
guest

回答3

0

きちんとエラーメッセージを読んでください。

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.MissingMethodException: Method 'cw.v' not found.

cwのvというメソッドが見つからないと言ってますよね。この時点で使用しているのは明白です。
あと、Debugビルドで、デバッグの開始で実行しているならどの行でエラーになっているかも判る筈です。

投稿2021/04/14 01:33

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

0

ベストアンサー

VB

1f = new cw 2f.v = x

とありますので、fcwクラスのインスタンスで、f.vcwクラスのvメンバ変数です。

なお、option strict offだと、型を明示しなければprivateだろうがアクセス可能です。

VB

1option strict off 2class Hoge 3 Private v As integer 4 Sub setv(x) 5 v = x 6 end sub 7 8 Sub disp 9 Console.WriteLine(v) 10 end sub 11end class 12 13 14public class compiler 15 shared function Main as integer 16 17 dim hoge = new Hoge 18 hoge.setv(1) 19 Console.WriteLine(hoge.v) ' => 1 20 hoge.v = 2 21 hoge.disp ' => 2 22 23 return 0 24 End function 25end class 26

投稿2021/04/14 00:33

ozwk

総合スコア13521

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

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

0

このソースは本当にコンパイルが通りますか?

クラスのPrivate と Public の使い方が解っているようには見えません。

ちなみに、cw 無いで宣言されている v に関しては、class pureput で、使っていますので、使っていない変数ではありません(private ではなく、public ならという条件は付きます)。

あと、このソースは、わざと読みにくく作っているのですか?

投稿2021/04/13 23:32

nfox

総合スコア229

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問