線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1119
推到 Plurk!
推到 Facebook!

請教 Class 問題

答題得分者是:sos_admin
dan59314
中階會員


發表:121
回覆:107
積分:86
註冊:2002-08-16

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-08-01 11:24:00 IP:211.23.xxx.xxx 未訂閱
請教各位先進,為什麼會出現 Access violation 我寫了一個 Class 目的是將一段字串根據分割字元分割成數段字串。 tSary.GetStrAry(Edit1.text, Edit2.Text[1]); //這樣是可以的。------------------------------- ListBox1.Items.Add(tSary.Str(0)); ListBox1.Items.Add(tSary.Str(1)); ListBox1.Items.Add(tSary.Str(2)); ListBox1.Items.Add(tSary.Str(3)); ListBox1.Items.Add(tSary.Str(4)); ListBox1.Items.Add(tSary.Str(5)); //可是這樣卻會當掉-------------------------------- for i:=0 to 5 do ListBox1.Items.Add(tSary.Str(i)); Class 內容如下: unit UtilityClasses; interface uses Windows,SysUtils; type ArrayStr=Array of String; type TStringArray=Class constructor Create; destructor Destroy; private StrAry:^ArrayStr; Public function StrCount:integer; function Str(i:integer):String; procedure ClearStrAry; procedure GetStrAry(s:String; splitch:Char); end; var gSAry:ArrayStr; implementation constructor TStringArray.Create; begin Setlength(gSAry,0); StrAry:=@gSAry; end; destructor TStringArray.Destroy; begin Setlength(gSAry,0); StrAry:=nil; end; procedure TStringArray.ClearStrAry; var i:integer; begin if (gSAry<>nil) then for i:=0 to High(gsAry) do gsAry[i]:=''; end; procedure TStringArray.GetStrAry(s:String; splitch:Char); var i, id, ipos, idot:integer; begin SetLength(gSAry,0); idot:=0; for i:=1 to length(s) do if s[i]=',' then inc(idot); SetLength(gSAry,idot 1); id:=0; ipos:=pos(',',s); while (ipos<>0) do begin gsAry[id]:=copy(s,1,ipos-1); s:=copy(s,ipos 1, Length(s)-ipos); ipos:=pos(',',s); inc(id); end; gsAry[id]:=s; StrAry:=@gSAry; end; function TStringArray.StrCount:integer; begin result:=High(StrAry^) 1; end; function TStringArray.Str(i:integer):String; begin if i in [0..High(StrAry^)] then str:=StrAry^[i] else str:=''; end; end. 軟還要更軟,我的Artwork http://delphi.ktop.com.tw/topic.php?TOPIC_ID=27674 Chinese Girl in Rhino3D http://rhino3d.com/gallery/figure/girl.htm
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-08-01 11:47:06 IP:147.8.xxx.xxx 未訂閱
You are pointing internal field to a global variable (gSAry). And gSAry get allocated and deallocated upon Create and Destroy of EVERY TStringArrayClass... so freeing any single instance of your TStringArrayClass would make all remaining TStringArrayClass unworkable.....
sos_admin
版主


發表:121
回覆:697
積分:768
註冊:2003-07-23

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-08-01 11:56:00 IP:61.155.xxx.xxx 未訂閱
//使用如下: var tsary:TStringArray; i:integer; begin tsary:=TStringArray.Create ; tSary.GetStrAry(Edit1.text, Edit2.Text[1]); for i:=0 to 5 do ListBox1.Items.Add(tSary.Str(i)); tsary.Destroy ; end;
dan59314
中階會員


發表:121
回覆:107
積分:86
註冊:2002-08-16

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-08-01 12:03:14 IP:211.23.xxx.xxx 未訂閱
Thanks to William. Sorry to William. I think sos_admin need the credit. So I give it to him. Wish you won't mind. :) 軟還要更軟,我的Artwork http://delphi.ktop.com.tw/topic.php?TOPIC_ID=27674 Chinese Girl in Rhino3D http://rhino3d.com/gallery/figure/girl.htm
系統時間:2024-06-29 8:36:23
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!