11 Replies - 532 Views - Last Post: 17 June 2009 - 06:57 AM Rate Topic: -----

#1 loloalssum  Icon User is offline

  • New D.I.C Head

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

Error in array

Post icon  Posted 16 June 2009 - 12:32 AM

Hello

Iam trying to write code that read integer numbers from user (intered in form) and put it in array and then i will do some calcuation.

I declare The array z and index of array p at begining of the class (global), I dont specify the size of arry couz that it depend on user.

If CInt(l1.Text) >= 0 Then
			z(p) = CInt(l1.Text.ToUpper)
			l1.Clear()
			l1.Focus()
			p = p + 1
		Else
			MsgBox("not a Number", MsgBoxStyle.Critical, "Error")
		End If



when I run the code there is an error in
z(p)=CInt(l1.Text.ToUpper)



"Object reference not set to an instance of an object."

I try to solve this problem but :/

please help me :blink:

Is This A Good Question/Topic? 0
  • +

Replies To: Error in array

#2 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 590
  • Joined: 04-May 09

Re: Error in array

Posted 16 June 2009 - 02:54 AM

Hi,

The ToUpper method is not for numbers but for strings and will not working.

Try this:

z(p)=CInt(l1.Text.ToString)

Was This Post Helpful? 0
  • +
  • -

#3 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 175
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Error in array

Posted 16 June 2009 - 03:54 AM

You don't need to use the toUpper() function to convert a string into a number.
The CInt() function will work, however it is a legacy VB6 function and might be deprecated from VB.NET in future versions.

Hence you should use the Integer.Parse() function instead like this:
z(p) = Integer.Parse(l1.Text)

Was This Post Helpful? 0
  • +
  • -

#4 loloalssum  Icon User is offline

  • New D.I.C Head

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

Re: Error in array

Posted 16 June 2009 - 04:52 AM

Thank you very much

but it does not work

still the same errer

I attach the picture for error

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#5 AlienCoder  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 31
  • Joined: 05-May 09

Re: Error in array

Posted 16 June 2009 - 05:04 AM

@ loloalssum, I'am not sure if I understand your problem quite well but I think I do. The problem is with the array giving you an error "Object reference not set to an instance of an object." I also had the same problem a couple of times ago but never really found a proper solution to it. This error occurs because you're trying to specify an array position or index that does not exist since you din't specify the number of elements it is going to hold. The fact is that the system needs to know the number of elements before you can start accessing them by use of an index.

Like I said I never actually found a permanent\immediate solution to this problem but I have found my own work-around. The problem with your solution is that I don't know when you want to stop collecting the numbers\integers so try the following code:
Public Class Form1

	Dim array() As String = Nothing
	Dim MyString As String

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		'Check to see if entered value is a number not text and if it's greater than zero(0)
		If IsNumeric(Trim(TextBox1.Text)) And Val(Trim(TextBox1.Text)) > 0 Then
			'Add it to the our string plus a white space for seperation
			MyString += Trim(TextBox1.Text) & " "

			TextBox1.Clear()
			TextBox1.Focus()

		End If

	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		'After we're done collecting all the numbers we want, make sure we have collected something
		If MyString <> "" Then
			'Split our string to fill the array
			array = MyString.Split(" ")
			'Just a test to see what we collected. Replace with some useful code like your calculations
			For i = 0 To array.Length - 1

				MsgBox(array(i))

			Next

		End If

	End Sub

End Class



What I do is use a string variable to hold all the values I need by seperating them with something like a white space (" ") or any symbol e.g ("*"), ("/"). After I have have fininshed collecting what I need depending on the situation I split the string variable into an array. In this case th system will know the number of elements by counting each seperate value after the seperator (" "). From there on you can do your thing!!

Hope It was helpful!!
Was This Post Helpful? 0
  • +
  • -

#6 loloalssum  Icon User is offline

  • New D.I.C Head

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

Re: Error in array

Posted 16 June 2009 - 06:48 AM

Thank you very much :D

its work.. :^:
Was This Post Helpful? 0
  • +
  • -

#7 AlienCoder  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 31
  • Joined: 05-May 09

Re: Error in array

Posted 16 June 2009 - 07:12 AM

View Postloloalssum, on 16 Jun, 2009 - 05:48 AM, said:

Thank you very much :D

its work.. :^:


I'am glad I could help. The green link at the botton of my post ( :^: This post was helpful ) is enough thanks, lol.. :D

This post has been edited by AlienCoder: 16 June 2009 - 07:14 AM

Was This Post Helpful? 0
  • +
  • -

#8 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: Error in array

Posted 16 June 2009 - 07:20 AM

I suspect it is a simple problem that you pointed out in your original post. The array needs a size before it can be used. You can default the array to a size of say 10 and then as elements are added, if the user tries to add an 11th element, then you can either tell the user the limit has been reached, or you can redim the array to be larger - I would typically change it by adding 10 more, not just 1.
		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 - 07:21 AM

Was This Post Helpful? 0
  • +
  • -

#9 AlienCoder  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 31
  • Joined: 05-May 09

Re: Error in array

Posted 17 June 2009 - 02:59 AM

View Postmark.bottomley, on 16 Jun, 2009 - 06:20 AM, said:

I suspect it is a simple problem that you pointed out in your original post. The array needs a size before it can be used. You can default the array to a size of say 10 and then as elements are added, if the user tries to add an 11th element, then you can either tell the user the limit has been reached, or you can redim the array to be larger - I would typically change it by adding 10 more, not just 1.
		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



A quick experiment with the code will yield that using the 'ReDim' statement will completely wipe all the current data from the array! :v: :rolleyes:
Was This Post Helpful? 0
  • +
  • -

#10 AlienCoder  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 31
  • Joined: 05-May 09

Re: Error in array

Posted 17 June 2009 - 03:11 AM

:D

This post has been edited by AlienCoder: 17 June 2009 - 03:21 AM

Was This Post Helpful? 0
  • +
  • -

#11 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Error in array

Posted 17 June 2009 - 05:04 AM

ReDim Preserve FTW

I do not recommend AlienCoder's workaround. Learn proper array declaration and manipulation or use a different collection such as an ArrayList or List(Of).
Was This Post Helpful? 0
  • +
  • -

#12 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: Error in array

Posted 17 June 2009 - 06:57 AM

Add the Preserve keyword to the ReDim...

ReDim Preserve ....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1