简体   繁体   中英

If-statement inside Add() method?

I'm trying to add each line of a textfile to different columns in a listView . However, I've run into a problem.

This is how I did a method:

public void OpenFile()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            string line = "";
            int index = 0;
            if (openFileDialog.ShowDialog() == true)
            using (StreamReader sr = File.OpenText(openFileDialog.FileName))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    index++;
                    if (index == 1)
                        InvoiceNumbertxt.Text = line;
                    else if (index == 2)
                        InvoiceDatetxt.Text = line;
                    else if (index == 3)
                        DueDatetxt.Text = line;
                    else if (index == 4 || index == 5 || index == 6 || index == 7 || index == 8 || index == 9)
                        PersonInfolst.Items.Add(line);
                    else if (index == 10)
                    {
                        Items.Add(new ItemProperties 
                        {     
                            Item = line
                            if(index == 11)// <---- If-statement inside Add?
                            Description = line;


                        });
                        itemlst.ItemsSource = Items;
                    }
                    else
                        break;
                }
            }
        }

As you can see index is just a convenient flag (variable) to insert the lines in order, and not overlap multiple lines into the same control.

The problem I have is I want to check if index is a value inside the Add() method so that I can add the new textFile line to the same row but different column in the list.

UPDATE:

public partial class MainWindow : Window
    {
        ObservableCollection<ItemProperties> Items =
        new ObservableCollection<ItemProperties>();
        public MainWindow()
        {
            InitializeComponent();
        }
        public ObservableCollection<ItemProperties> GameCollection
        {
            get
            {
                if (Items == null)
                {
                    Items = new ObservableCollection<ItemProperties>();
                }
                return Items;
            }
        } 

        private void btnOpenFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFile();
        }

        public void OpenFile()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            string line = "";
            int index = 0;
            if (openFileDialog.ShowDialog() == true)
            using (StreamReader sr = File.OpenText(openFileDialog.FileName))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    index++;
                    if (index == 1)
                        InvoiceNumbertxt.Text = line;
                    else if (index == 2)
                        InvoiceDatetxt.Text = line;
                    else if (index == 3)
                        DueDatetxt.Text = line;
                    else if (index == 4 || index == 5 || index == 6 || index == 7 || index == 8 || index == 9)
                        PersonInfolst.Items.Add(line);
                    else if (index == 10)
                    {
                        Items.Add(new ItemProperties { Item = line });
                        itemlst.ItemsSource = Items;
                    }
                    else if (index == 11)
                    {
                        //??

                    }
                    else
                        break;
                }
            }
        }

        private void btnOpenImage_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openfile = new Microsoft.Win32.OpenFileDialog();
            openfile.DefaultExt = "*.jpg";
            openfile.Filter = "Image Files|*.jpg";
            Nullable<bool> result = openfile.ShowDialog();
            if (result == true)
            {
                imagefile.Source = new BitmapImage(new Uri(openfile.FileName));
            }
        }

        public class ItemProperties
        {
            public string Item { get; set; }
            public string Description { get; set; }
            public string Quantity { get; set; }
            public string UnitPrice { get; set; }
            public string Tax { get; set; }
        }
    }

您可以使用三元运算符执行内联条件检查并设置值

Description = (index == 11) ? line : "";

You are doing it not in the Add()-Method but inside of the object initializer of ItemProperties . It is not possible there!

The best solution is to write an extra Add method and create the new ItemProperties object there.

private MyAdd(string line, int index)
{
   if(index == 11)
      Items.Add(new ItemProperties {Item = line, Description = "line11"});
   else
      Items.Add(new ItemProperties {Item = line, Description = "other"});
}

and then

else if (index == 10)
{
    MyAdd(line, index);
    itemlst.ItemsSource = Items;
}

Firstly, you should consider using switch , because it looks an ideal use in this case. Multiple ifs aren't really great for your needs.

Secondly, you can use simple ternary operator on assigning the value, some like this:

Description = id == 11 ? line : null;
else if (index == 10)
{
    Items.Add(new ItemProperties{Item = line});
     itemlst.ItemsSource = Items;
}
else if (index == 11)
{
     Description = line;
}

forget the loop

InvoiceNumbertxt.Text = sr.ReadLine();
InvoiceDatetxt.Text = sr.ReadLine();
...
Items.Add(new ItemProperties 
                        {     
                            Item = sr.ReadLine();
                            Description = sr.ReadLine();
                        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM