LoginSignup
0
0

More than 5 years have passed since last update.

Visual Studio / WPF > Error: CS0747 初期化子のメンバー宣言子が無効です。 > 引数の指定間違い

Last updated at Posted at 2017-06-08
動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community

http://gushwell.ldblog.jp/archives/52334146.html
を元に学習中。

途中まで写経していてひっかかった。

MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
//
using System.Collections.ObjectModel;

namespace _170608_t1820_treeview
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
    public class Category : ObservableCollection<Category>
    {
        public Category()
        {
        }
        public Category(string name)
        {
            Name = name;
        }
        public string Name { get; set; }
        public Category Children { get; set; }
    }
    public class OSAndBrowser
    {
        public Category Categories { get; set; }
        public OSAndBrowser()
        {
            Categories = new Category()
            {
                new Category("OS")
                {
                    Children = new Category("Windows")
                    {
                        Children = new Category
                        {
                            new Category("Windows 10"),
                            new Category("Windows 8.1"),
                        }
                    },
                    new Category("Mac OS X")
                }
            };
        }
    }
}

2017-06-08_18h33_53.png

https://msdn.microsoft.com/ja-jp/library/windows/apps/bb384261(v=vs.90)
によると、{}でくくるなかで本来あるべきでないところにnew Category()をしてしまっているようだ。

見直したところ、"Windows"の指定部分での間違いに気づいた。
以下のように訂正したところ、エラーは出なくなった。

   public class OSAndBrowser
    {
        public Category Categories { get; set; }
        public OSAndBrowser()
        {
            Categories = new Category()
            {
                new Category("OS")
                {
                    Children = new Category
                    {
                        new Category("Windows")
                        {
                            Children = new Category
                            {
                                new Category("Windows 10"),
                                new Category("Windows 8.1"),
                            }
                        },
                        new Category("Mac OS X")
                    }
                }
            };
        }
    }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0