您的位置:程序门 -> .net技术 -> vb.net



请问怎么将数组对象实例化


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


请问怎么将数组对象实例化[已结贴,结贴人:yejiangang77]
发表于:2008-03-14 14:35:34 楼主
       dim i as integer = 1
        dim j as integer
        dim itemp as string()

        dim sline as string

        dim objreader as new io.streamreader("f:\计算\njh\jnz" & cstr(n) & ".txt")

        dim objwriter as new io.streamwriter("f:\计算\njh\jnd" & cstr(n) & ".txt")

        do

            sline = objreader.readline
              itemp = sline.split(chr(9))
 显示 itemp = sline.split(chr(9)) 未将对象引用设置到对象的实例
 

发表于:2008-03-14 14:38:101楼 得分:0
dim itemp as string() 
没有定义大小。
发表于:2008-03-14 14:44:422楼 得分:0
 定义后 dim itemp as string() 还是那个错误!

  在另外的一个过程中却可以,不知道为什么
      dim fs as new system.io.filestream("f:\计算\njh\jnz.txt", io.filemode.open,   io.fileaccess.read)
        dim fw as new system.io.streamwriter("f:\计算\njh\jnz" & cstr(i) & ".txt")

        dim sr as new system.io.streamreader(fs)
        dim itemp as string()
        dim strtemp as string

        do
            strtemp = sr.readline
            itemp = strtemp.split(chr(9))
            if itemp(2) = cstr(i) then
                fw.writeline(strtemp)
            end if


        loop while sr.peek  <>  -1  
发表于:2008-03-14 14:47:413楼 得分:0
because sline is nothing
发表于:2008-03-14 14:51:124楼 得分:5
sr.readline did read lin anything. 
发表于:2008-03-14 15:01:125楼 得分:0
这个读文件代码有错误吗
发表于:2008-03-14 15:01:136楼 得分:5
vb.net code
dim fs as new system.io.filestream("f:\计算\njh\jnz.txt", io.filemode.open, io.fileaccess.read) dim fw as new system.io.streamwriter("f:\计算\njh\jnz" & cstr(i) & ".txt") dim sr as new system.io.streamreader(fs) dim itemp as string() dim strtemp as string do strtemp = sr.readline '这里不为空,那下面有就实例,如果为null,就没有实例化字符串数组 itemp = strtemp.split(chr9)) '这里你给dim itemp as string() 赋值strtemp.split(chr(9)) 。 if itemp(2) = cstr(i) then fw.writeline(strtemp) end if loop while sr.peek <> -1

如果你要新见一个空数组的话 那就  
dim itemp as string()
itemp=new string(*)'*为空或定义的大小
发表于:2008-03-14 15:01:237楼 得分:5
数组实例化
一.dim itemp(10) as string() 第一定定义数组范围

二 dim itemp as string
   redim itemp(10) as string
重定义

三 dim itemp as string={"0","1","2","3",....}
初始化

四 dim itemp as string
   itemp=returnstrarray()

private function returnstrarray() as string()
...
return ..

en function  
发表于:2008-03-14 15:02:328楼 得分:0
使用前必须预定义其大小的(也叫维度大小)
发表于:2008-03-14 15:03:099楼 得分:0
strtemp = sr.readline  
关键就这句吧,如果是空的化就会出错滴!
最好判断一下是否为空!
发表于:2008-03-14 15:03:2510楼 得分:0
数组实例化 
一.dim itemp(10) as string() 第一定定义数组范围 

二 dim itemp as string()
   redim itemp(10) as string 
重定义 

三 dim itemp as string={"0","1","2","3",....} 
初始化 

四 dim itemp as string 
   itemp=returnstrarray() 

private function returnstrarray() as string() 
... 
return .. 

en function   
发表于:2008-03-14 15:06:5511楼 得分:5
you just change your code like below
vb.net code
strtemp = sr.readline if not isnothing(strtemp) then itemp = strtemp.split(chr9)) if itemp(2) = cstr(i) then fw.writeline(strtemp) end if end if
发表于:2008-03-14 15:12:2912楼 得分:0
明白了!谢谢大家指点!


快速检索

最新资讯
热门点击