|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Array of Hash TablesHere is the code: Public Function ReadTilesFromFile(ByVal Path As String, Optional ByVal TilesetDir As String = "tilesets/") Dim i As Integer ' Counter for loops Dim TilesetPath As String = TilesetDir & Path ' Full path to tileset file Dim xDS As New Tileset() ' Make xDS point to Tilset Dim xRow As Tileset.TilesRow ' Make xRow point to Tileset.TilesRow If New FileInfo(TilesetPath).Exists Then ' If the tileset file exists xDS.ReadXml(TilesetPath, _ System.Data.XmlReadMode.IgnoreSchema) ' Read tiles table from file If xDS.Tiles.Rows.Count > 0 Then ' Check if there are rows in the table For i = 0 To (xDS.Tiles.Rows.Count - 1) ' For each row in the table xRow = xDS.Tiles.Rows.Item(i) ' Use the current row of the table ReDim Preserve aryTiles(i) MsgBox("die") MsgBox(aryTiles(i).Count) ' AsciiTile If Not xRow.IsAsciiTileNull() Then ' If there is data in the AsciiTile field 'aryTiles(i).Add("AsciiTile", xRow.AsciiTile) ' Put the data in the Tiles hashtable aryTiles(i).Item("AsciiTile") = xRow.AsciiTile End If MsgBox("die1") ' Image If Not xRow.IsImageNull() Then ' If there is data in the Image field aryTiles(i).Add("Image", xRow.Image) ' Put the data in the Tiles hashtable End If Next i End If Else TilesetError(1) ' Call TilesetError, MissingTilesetFile End If End Function -------------------------------- From: Glenn Grant ----------------------- Posted by a user from .NET 247 (http://www.dotnet247.com/) <Id>Ks4h5KKv7EessyelBhdorg==</Id> Glenn,
>I am trying to create an Array of type Hash Table. When I try to assign a Then there is probably an error in your program. Did it not tell where it >value to a key in the hash table i get a System.NullReferenceException >error. > was or did you debug it so that you could see it? Cor Where in this code is the declaration for the array of hashtables?
Perhaps you posted the wrong section of code by mistake? Glenn,
>I am trying to create an Array of type Hash Table. This normally occurs when you initialize an array without initializing the > When I try to assign a value to a key in the hash table > i get a System.NullReferenceException error. elements. Hashtable is a reference type, when you use: Dim list(10-1) As Hashtable or: Dim Preserve list(list.Length + 1) The elements are initialized to Nothing, you need to initialize the individual elements to a new Hashtable object, something like: Dim list(10-1) As Hashtable For index As Integer = 0 to list.Length - 1 list(index) = New Hashtable Next ReDim Preserve list(list.Length) list(list.Length - 1) = New Hashtable Hope this helps Jay Show quoteHide quote "Glenn Grant via .NET 247" <anonym***@dotnet247.com> wrote in message news:%23YdLWW5MFHA.2736@TK2MSFTNGP09.phx.gbl... >I am trying to create an Array of type Hash Table. When I try to assign a >value to a key in the hash table i get a System.NullReferenceException >error. > > Here is the code: > > Public Function ReadTilesFromFile(ByVal Path As String, Optional ByVal > TilesetDir As String = "tilesets/") > Dim i As Integer ' > Counter for loops > Dim TilesetPath As String = TilesetDir & Path ' Full > path to tileset file > Dim xDS As New Tileset() ' Make > xDS point to Tilset > Dim xRow As Tileset.TilesRow ' Make > xRow point to Tileset.TilesRow > > If New FileInfo(TilesetPath).Exists Then ' If > the tileset file exists > xDS.ReadXml(TilesetPath, _ > System.Data.XmlReadMode.IgnoreSchema) ' Read > tiles table from file > > If xDS.Tiles.Rows.Count > 0 Then ' Check > if there are rows in the table > For i = 0 To (xDS.Tiles.Rows.Count - 1) ' For > each row in the table > xRow = xDS.Tiles.Rows.Item(i) ' Use > the current row of the table > > ReDim Preserve aryTiles(i) > > MsgBox("die") > MsgBox(aryTiles(i).Count) > ' AsciiTile > If Not xRow.IsAsciiTileNull() Then ' If > there is data in the AsciiTile field > 'aryTiles(i).Add("AsciiTile", xRow.AsciiTile) ' Put > the data in the Tiles hashtable > aryTiles(i).Item("AsciiTile") = xRow.AsciiTile > End If > MsgBox("die1") > ' Image > If Not xRow.IsImageNull() Then ' If > there is data in the Image field > aryTiles(i).Add("Image", xRow.Image) ' Put > the data in the Tiles hashtable > End If > Next i > > End If > Else > TilesetError(1) ' Call > TilesetError, MissingTilesetFile > End If > End Function > > -------------------------------- > From: Glenn Grant > > ----------------------- > Posted by a user from .NET 247 (http://www.dotnet247.com/) > > <Id>Ks4h5KKv7EessyelBhdorg==</Id> Jay B. Harlow [MVP - Outlook] wrote:
> The elements are initialized to Nothing, you need to initialize the Of course, if you are ReDim'ing the array by more than a single> individual elements to a new Hashtable object, something like: > > ReDim Preserve list(list.Length) > list(list.Length - 1) = New Hashtable > element, you will need to use a loop too: Dim oldlistlength As Integer = list.length ReDim Preserve list(newlistlength - 1) For x As Integer = oldlistlength - 1 To newlistlength - 1 list(x) = New Hashtable Next I think in the For loop, it should be
For x As Integer = oldlistlength To newlistlength-1 'Code Next my mistake
FileSystemObject vs System.IO.File
Query database in VB.NET Create ListView columns dynamically from XML record locking with DBF (visual basic) table in ADO OLEDB in VB.NET Set Desktop Setup problem Progress Bar How to have embeded pictures in a project need ideas - how to determine when another machine has persisted a file VBScript tutorials |
|||||||||||||||||||||||