1 Replies - 5269 Views - Last Post: 16 June 2009 - 10:00 AM Rate Topic: -----

#1 Tehdead  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 16-June 09

Dynamic array of custom class

Posted 16 June 2009 - 08:16 AM

I've been programming in VB.net for a while off and on, but never really had a need to use custom classes till now.
I built a function in a module to populate a class, The class how ever is supposed to refer to one instance.

I need to create an array of this class but the amount of instances needed is not always known.
Works fine till the class goes to populate the class array.

Public Function BuildMarcoTable(ByVal MacroFile As String) As Macro()
		Dim func As String = "BuildMacroTable"
		Dim Macroarray() As Macro
		Dim str As String
		Dim s As Int32
		Dim e As Int32
		Dim x As Int32 = 0
		'Do Until e > MacroFile.LastIndexOf("End")
		Dim myMacro As Macro = New Macro
		'Get Index
		s = MacroFile.IndexOf("Macro", s) + Len("Macro") + 1
		e = MacroFile.IndexOf(Chr(34), s) - 1
		str = MacroFile.Substring(s, e - s)
		myMacro.Index = str
		Debug.Writeline(func, "Index = " & str)
		s = e
		'Get Name
		s = MacroFile.IndexOf(Chr(34), s) + 1
		e = MacroFile.IndexOf(Chr(34), s)
		str = MacroFile.Substring(s, e - s)
		Debug.Writeline(func, "Name = " & str)
		myMacro.Name = str
		s = e
		'Get Icon Name
		s = s + 2
		e = MacroFile.IndexOf(Chr(13), s)
		str = MacroFile.Substring(s, e - s)
		Debug.Writeline(func, "Icon = " & str)
		myMacro.Icon = str
		s = e
		'Get macro body
		s = s + 1
		e = MacroFile.IndexOf("END", s)
		str = MacroFile.Substring(s, e - s)
		Debug.Writeline(func, "Body = " & str)
		myMacro.Body = str
		s = e
		'Loop
		Macroarray(x) = myMacro
		Return Macroarray
	End Function



It's probably something simple, I've just starred at it to long.
Keep gettitng "Object reference not set to an instance of an object."

This post has been edited by Tehdead: 16 June 2009 - 08:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Dynamic array of custom class

#2 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Dynamic array of custom class

Posted 16 June 2009 - 10:00 AM

You should tell us what line the error is ont, but I think I know...

First, you have a typo in the Function name -> BuildM<b>ar</b>coTable

Second, you haven't defined the array size - you need to do that. the code is a chunk I put in another post, but it starts with a default size and grows by some step size once filled.

		Dim arrayMax As Integer = 10
		Dim arrayUsed As Integer = 0
		Dim growthStep As Integer = 10
		Dim myArray(arrayMax) As Integer

		Dim myData As Integer
		' read myData
		arrayUsed += 1
		If arrayUsed = arrayMax Then
			ReDim myArray(arrayMax + growthStep)
			arrayMax += growthStep
		End If
		myArray(arrayUsed) = myData


This post has been edited by mark.bottomley: 16 June 2009 - 10:02 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1