創作內容

7 GP

C# dictionary

作者:貓貓風 ฅ●ω●ฅ│2017-03-29 21:32:25│巴幣:14│人氣:753
.















這方法就跟它的名稱一樣,叫做字典,主要功能就是可以對其加入索引(index)跟值(value),之後只要輸入該索引就可以取得想要的內容,以下範例為取得檔案中的內容存至List中,接著再分別塞入dictionary。事先就先定義好每個索引的列舉,然後在讀入檔案的內容會去跟列舉做比對,如果有與事先定義的不同就會顯示錯誤,可以在編譯階段就找出問題,降低在run time時產生錯誤的危險性。
最後就是列出每個讀入的項目為一個清單,點選個別清單會取得相對應的內容。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Dictionary
{
     public partialclass Form1 : Form
     {
          privateList<point_name.name> _action = new List<point_name.name>();
          string[]_action_name = new string[100];
          point_name_point_name = new point_name();
          Dictionary<point_name.name,point_info> _dictionary =
               newDictionary<point_name.name, point_info>();
          int_action_count = 0;
          int _c =0;
          publicForm1()
          {
               InitializeComponent();
          }
          privatevoid Form1_Load(object sender, EventArgs e)
          {
                get_point_file();
                get_action_file();
                this.comboBox1.Items.Clear();
                for (int i = 0; i < _c; i++)
                {
                     this.comboBox1.Items.Add(newdropdownList(_action_name[i], i));
                }
                this.comboBox1.SelectedIndex = 0;
          }
          privatevoid get_point_file()
          {
               point_infopi = new point_info();
               stringline;
               string[]data = { };
               char[]split = {'\t'};
               stringstr = System.Windows.Forms.Application.StartupPath;
               stringfile_name = str + "\\action_point.txt";
                FileStream file_WR = newFileStream(file_name, FileMode.Open,
                FileAccess.Read, FileShare.ReadWrite);
               System.IO.StreamReaderfile = new System.IO.StreamReader(file_WR);
               while((line = file.ReadLine()) != null)
               {
                    data= line.Split(split);
                    pi= new point_info();  
                    pi.type=  data[1];
                    pi.reference= data[2];
                    pi.x= Int32.Parse(data[3]);
                    pi.y= Int32.Parse(data[4]);
                    pi.z= Int32.Parse(data[5]);
                    pi.explanation= data[6];
                    try
                    {
                     _dictionary.Add((point_name.name)Enum.Parse(typeof(point_name.name),                         data[0]),                      pi);
                    }
                    catch
                    {
                         MessageBox.Show("indexerror1");
                         return;
                    }
               }
               file.Close();
               file_WR.Close();
          }
          privatevoid  get_action_file()
          {
               stringline;
               string[]data = { };
               char[]split = { '\t' };
               stringstr = System.Windows.Forms.Application.StartupPath;
               stringfile_name = str + "\\action_mode.txt";
               FileStream file_WR = newFileStream(file_name, FileMode.Open,
               FileAccess.Read, FileShare.ReadWrite);
               System.IO.StreamReaderfile = new System.IO.StreamReader(file_WR);
               while((line = file.ReadLine()) != null)
               {
                    data= line.Split(split);
                    _action_name[_c]= data[0];
                    for(inti=1 ; i<data.Length-1 ; i++)
                    {
                         try
                         {
                              _action.Add(((point_name.name)Enum.Parse(typeof(point_name.name),                                 data[i])));
                         }
                         catch
                         {
                              MessageBox.Show("indexerror2");
                              return;
                         }
                    }
                    _c++;
               }
               _action_count= data.Length - 1;
               file.Close();
               file_WR.Close();
          }
          privatevoid button1_Click(object sender, EventArgs e)
          {
               richTextBox1.Text="";
               dropdownListddL = (dropdownList)this.comboBox1.SelectedItem;
               intselect_index;
               for(int i = 0; i < _action_count; i++)
               {
                    if(ddL.value == i)
                    {
                         select_index= i * _action_count;
                         for(intj=1 ; j <_action_count;j++)
                         {
                              richTextBox1.Text+= "m " +_action_name[i] + " n"
                              +_dictionary[_action[j + select_index]].type + " "
                              +_dictionary[_action[j + select_index]].reference + " "
                              +_dictionary[_action[j + select_index]].x + " "
                              +_dictionary[_action[j + select_index]].y + " "
                              +_dictionary[_action[j + select_index]].z + " "
                              +_dictionary[_action[j + select_index]].explanation + " "
                              +"\n";
                         }
                    }
               }
          }
     }
}
讀取的檔案





執行結果





引用網址:https://home.gamer.com.tw/TrackBack.php?sn=3527340
All rights reserved. 版權所有,保留一切權利

相關創作

同標籤作品搜尋:涼涼風|C#

留言共 4 篇留言

(゚∀ ゚)✄╰U╯
讚喔

03-29 23:55

貓貓風 ฅ●ω●ฅ
桑Q ^^03-29 23:58
小刀
我看不懂的說,不過很棒~~[e35]

04-01 22:32

貓貓風 ฅ●ω●ฅ
有興趣我可以教你唷owo04-01 22:52

好複雜XDD

04-03 00:05

貓貓風 ฅ●ω●ฅ
就把他想成字典~ 例如我查APPLE 他就會丟何謂蘋果的解釋給你~ 大概是這樣XD
04-03 00:07
死者書生
原來有這種方法阿QQ~
小生還用dataList.Find(x => x[0].Equals(selectName))[1];
去做出一樣的效果說=口=

04-05 09:52

貓貓風 ฅ●ω●ฅ
感覺搞得很複雜XDD04-05 19:15
我要留言提醒:您尚未登入,請先登入再留言

7喜歡★s1234567 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:C# get set... 後一篇:幸福的歸宿 - 白紗下的...

追蹤私訊切換新版閱覽

作品資料夾

leon770530巴友
我的小屋首頁共有4篇健身相關證照心得文,對健身有興趣的朋友可以來看看喔!看更多我要大聲說昨天10:01


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】