C#

2020.08.25(화) - C# 윈폼

J_Bin 2020. 8. 25. 18:30

* List배열 정렬 Incomparable

 

- product.class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _2020._08._25_001
{
    class Product : IComparable
    {
        public string Name { get; set; }

        public int Price { get; set; }

        public int CompareTo(object obj)
        {
            //return Name.CompareTo((obj as Product).Name);
            return Name.CompareTo((obj as Product).Price);

        }

        public override string ToString()
        {
            return Name + ":" + Price + "원";
        }
    }
}

 

- 소스 코드

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _2020._08._25_001
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {   
            // 리스트배열 객체생성
            List<Product> alist = new List<Product>() { 
                new Product() { Name = "고메", Price = 1000},
                new Product() { Name = "군고메", Price = 3500},
                new Product() { Name = "찐고메", Price = 3000},
                new Product() { Name = "짠고메", Price = 5000},
                new Product() { Name = "맛탕", Price = 1200}
            };

            //alist.Sort();
            string result = "";

            foreach (var Temp in alist)
            {
                result += (Temp + "\n");
            }

            MessageBox.Show(result);


        }
    }
}

 

 

* 간단한 경로를 탐색하여 factory.txt 만들기

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _2020._08._25_002
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            File.WriteAllText("c:\\factory.txt", "factory");
        }
    }
}

 

- Form1.cs[디자인]

 

 

 

'C#' 카테고리의 다른 글

2020.08.27(목) - C#  (0) 2020.08.27
2020.08.26(수) - C#  (0) 2020.08.26
2020.08.24(월) - C# 야구게임 다듬기  (0) 2020.08.24
2020.08.21(금) - C# 윈폼 & C#  (0) 2020.08.21
2020.08.20(목) - C# 윈폼 & C#  (0) 2020.08.20