いつもお世話になっております。
こちらでは過去の投稿でもお知恵をいただき、何とか初心者エンジニアとして頑張らせてもらっています。
今回は過去に作成しましたプログラムをWPFで同様の処理をしてみろということでの課題です。
多少、書き換えもありましたが問題なくコンパイルも通ったところで下記の例外ではじかれました。
「インデックスが範囲を超えています。負でない値で、コレクションのサイズよりも小さくなければなりません。パラメータ名 index」
うまくアイテムを入れきれてないのか・・・なのか。
下記にxaml.vbのソースを記述します。
Namespace View '■■■■■■■■■■■■■■■■■■■■■■ 'クラス '■■■■■■■■■■■■■■■■■■■■■■ Public Class 〇〇 Private rateList As String() = New String() {"\", "$", "€"} Private items As List(Of ExchangeItem) = New List(Of ExchangeItem) Public Event Shown(sender As Object, e As EventArgs) 'VS自動生成 Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown ComboBox1.Items.Add(rateList) ComboBox2.Items.Add(rateList) ComboBox3.Items.Add(rateList) '..アイテムリストを生成 items.Clear() items.Add(New ExchangeItem) items.Add(New ExchangeItem) items.Add(New ExchangeItem) End Sub Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click items(0).currency = ComboBox1.SelectedIndex ←ここで例外が発生 items(0).fromValue = CDec(TextBox1.Text) items(1).currency = ComboBox2.SelectedIndex items(1).fromValue = CDec(TextBox2.Text) items(2).currency = ComboBox3.SelectedIndex items(2).fromValue = CDec(TextBox3.Text) Dim toCurrency As CurrencyType = Nothing Select Case DirectCast(sender, Button).Name Case "Button1" toCurrency = CurrencyType.Yen Case "Button2" toCurrency = CurrencyType.Dollar Case "Button3" toCurrency = CurrencyType.Eur End Select For i As Integer = 0 To items.Count - 1 Dim res As Decimal = items(i).Exchange(toCurrency) Select Case i Case 0 Me.Label1.Content = res Case 1 Me.Label2.Content = res Case 2 Me.Label3.Content = res End Select Next End Sub End Class Public Class ExchangeItem Private Const rate_dollar As Decimal = 100 Private Const rate_eur As Decimal = 120 Public Property currency As CurrencyType = Nothing Public Property fromValue As Decimal = 0 Public Function Exchange(ByVal _toCurrency As CurrencyType) As Decimal Select Case Me.currency Case CurrencyType.Yen Select Case _toCurrency Case CurrencyType.Yen Return fromValue Case CurrencyType.Dollar Return Me.fromValue * rate_dollar Case CurrencyType.Eur Return Me.fromValue * rate_eur End Select Case CurrencyType.Dollar Select Case _toCurrency Case CurrencyType.Yen Return Me.fromValue * rate_dollar Case CurrencyType.Dollar Return fromValue Case CurrencyType.Eur Return rate_dollar / rate_eur End Select Case CurrencyType.Eur Select Case _toCurrency Case CurrencyType.Yen Return Me.fromValue * rate_eur Case CurrencyType.Dollar Return rate_eur / rate_dollar Case CurrencyType.Eur Return fromValue End Select End Select Return 0 End Function End Class Public Enum CurrencyType Yen = 0 Dollar = 1 Eur = 2 End Enum End Namespace
こちらがxamlのほうです。
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:core_b="clr-namespace:Cresc.Behavior;assembly=Cresc.Core" xmlns:core_i="clr-namespace:Cresc.Icon;assembly=Cresc.Core" xmlns:local="clr-namespace:Cresc.View" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="View.兼島" core_b:UserControl.HandleEvent="True" Height="104" Width="458"> <Grid Height="358" Margin="-541,-216,462,0" VerticalAlignment="Top"> <Grid.RowDefinitions> <RowDefinition Height="40*"/> <RowDefinition Height="40*"/> <RowDefinition Height="40*"/> <RowDefinition Height="59*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="163*"/> <ColumnDefinition Width="207*"/> <ColumnDefinition Width="149*"/> </Grid.ColumnDefinitions> <TextBox x:Name="TextBox1" Grid.Column="1" HorizontalAlignment="Left" Height="61" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="188"/> <TextBox x:Name="TextBox2" Grid.Column="1" HorizontalAlignment="Left" Height="61" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="188" Grid.Row="1"/> <TextBox x:Name="TextBox3" Grid.Column="1" HorizontalAlignment="Left" Height="61" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="188" Grid.Row="2"/> <Label x:Name="Label1" Content="" Grid.Column="2" HorizontalAlignment="Left" Height="61" Margin="10,10,0,0" VerticalAlignment="Top" Width="129"/> <Label x:Name="Label2" Content="" Grid.Column="2" HorizontalAlignment="Left" Height="61" Margin="10,10,0,0" VerticalAlignment="Top" Width="129" Grid.Row="1"/> <Label x:Name="Label3" Content="" Grid.Column="2" HorizontalAlignment="Left" Height="59" Margin="10,10,0,0" VerticalAlignment="Top" Width="129" Grid.Row="2"/> <Button x:Name="Button1" Content="\" HorizontalAlignment="Left" Height="50" Margin="25,10,0,0" Grid.Row="3" VerticalAlignment="Top" Width="120"/> <Button x:Name="Button2" Content="$" HorizontalAlignment="Left" Height="50" Margin="39,10,0,0" Grid.Row="3" VerticalAlignment="Top" Width="120" Grid.Column="1"/> <Button x:Name="Button3" Content="€" HorizontalAlignment="Left" Height="50" Margin="10,10,0,0" Grid.Row="3" VerticalAlignment="Top" Width="120" Grid.Column="2"/> <ComboBox x:Name="ComboBox1" HorizontalAlignment="Left" Height="50" Margin="25,20,0,0" VerticalAlignment="Top" Width="111" Grid.Row="0"> <ComboBoxItem Content="\"/> <ComboBoxItem Content="$"/> <ComboBoxItem Content="€"/> </ComboBox> <ComboBox x:Name="ComboBox2" HorizontalAlignment="Left" Height="50" Margin="25,20,0,0" VerticalAlignment="Top" Width="111" Grid.Row="1"> <ComboBoxItem Content="\"/> <ComboBoxItem Content="$"/> <ComboBoxItem Content="€"/> </ComboBox> <ComboBox x:Name="ComboBox3" HorizontalAlignment="Left" Height="50" Margin="25,19,0,0" VerticalAlignment="Top" Width="111" Grid.Row="2"> <ComboBoxItem Content="\"/> <ComboBoxItem Content="$"/> <ComboBoxItem Content="€"/> </ComboBox> </Grid> </UserControl>
こちらはコンボボックスにアイテムを追加してるのみです。
試しに例外の起こっているitems(0).currencyの部分を変数に置き換えると次の行で同じ例外ではじかれます。
WEBにてWPFについて調べてみたのですがピンとくるものに行きつきません。
ここで皆様の御助力をいただけないでしょうか。
必要な情報等あればお申し付けください。
宜しくお願い致します。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/21 07:49
2019/08/21 08:28
2019/08/21 09:02
2019/08/23 05:36
2019/08/23 05:39