全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:8411
推到 Plurk!
推到 Facebook!

C# 有類似 TStringList 的物件嗎 ? 如何使用

尚未結案
digitraveler
初階會員


發表:89
回覆:91
積分:46
註冊:2005-06-01

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-06-08 21:53:49 IP:122.123.xxx.xxx 訂閱
BCB 範例為
TStringList *TmpList = New TStringList;
.....
.....
delete TmpList;



那 C# 的物件名及完整使用範例又為何呢 ?
感謝

--------------------------------
座右銘 : 用問的比較快 ...
--------------------------------
digitraveler
初階會員


發表:89
回覆:91
積分:46
註冊:2005-06-01

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-06-08 22:16:40 IP:122.123.xxx.xxx 訂閱
找到的有

using System;
using System.Collections;

class Test
{
static void Main() {
ArrayList stringList = new ArrayList();

stringList.Add("one");
stringList.Add("two");
stringList.Add("three");

foreach (string s in stringList) {
Console.WriteLine("Item: {0}", s);
}

Console.Read();
}
}

You could convert this ArrayList to a string array with:
string[] array = stringList.ToArray(typeof(string)) as string[];

又找到另一個說 C# 的 TStringList 就是
StringCollection 類別 (using System.Collections.Specialized)

digitraveler
初階會員


發表:89
回覆:91
積分:46
註冊:2005-06-01

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-06-08 22:32:37 IP:122.123.xxx.xxx 訂閱
有個閒人前輩自己寫的類別 , 連 SaveToFile , LoadFromFile 都有 , 就是這個光 ....

using System;
using System.Collections;
using System.IO;

namespace TextFileOP
{
/**//**//**////
/// Strings 的摘要说明。
///

public class Strings:CollectionBase
{
public String this[ int index ]
{
get
{
return( (string) List[index] );
}
set
{
List[index] = value;
}
}
public int Add( string value )
{
return( List.Add( value ) );
}

public int IndexOf( string value )
{
return( List.IndexOf( value ) );
}

public void Insert( int index, string value )
{
List.Insert( index, value );
}

public void Remove( string value )
{
List.Remove( value );
}

public bool Contains( string value )
{
return( List.Contains( value ) );
}

protected override void OnInsert( int index, Object value )
{
if ( value.GetType() != Type.GetType("System.String") )
throw new ArgumentException( "value 参数应为 string 类型.", "value" );
}

protected override void OnRemove( int index, Object value )
{
if ( value.GetType() != Type.GetType("System.String") )
throw new ArgumentException( "value 参数应为 string 类型.", "value" );
}

protected override void OnSet( int index, Object oldValue, Object newValue )
{
if ( newValue.GetType() != Type.GetType("System.String") )
throw new ArgumentException( "value 参数应为 string 类型.", "newValue" );
}

protected override void OnValidate( Object value )
{
if ( value.GetType() != Type.GetType("System.String") )
throw new ArgumentException( "value 参数应为 string 类型." );
}

public void LoadFromFile(string filename)
{
try
{
using (StreamReader sr = new StreamReader(filename))
{
String line;
while ((line = sr.ReadLine()) != null)
{
this.Add(line);
}
}
}
catch (Exception e)
{
throw(e);
}

}

public void SaveToFile(string filename,bool overwrite)
{
if (File.Exists(filename) && (!overwrite))
{
throw(new Exception("文件已存在!"));
}
StreamWriter sr = null;
try
{
sr = File.CreateText(filename);
for(int i=0;i sr.WriteLine(this[i]);
}
catch(Exception e)
{
throw(e);
}
finally
{
sr.Close();
}
}
}
}
carloyan
一般會員


發表:35
回覆:14
積分:9
註冊:2004-05-26

發送簡訊給我
#4 引用回覆 回覆 發表時間:2008-01-31 10:23:27 IP:124.10.xxx.xxx 訂閱
樓上的,您的 SaveToFile 不完整,幫您補一下

SaveToFile( filename, overwrite)
if&&!{
( Exception(檔案已存在!));
}

StreamWriter sr
;try{
sr
File.CreateText(filename);forint=;ithis
this
(Exception e)
throw


}


HikaruGo
中階會員


發表:22
回覆:69
積分:88
註冊:2007-12-09

發送簡訊給我
#5 引用回覆 回覆 發表時間:2008-05-08 20:33:41 IP:61.64.xxx.xxx 訂閱
插花一下


ArrayList stringList = new ArrayList();



For .NET 2.0

ArrayList 建議改成 List 泛型

boxing unboxing Speed 問題
系統時間:2024-04-24 17:40:26
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!