Simple array question...Please HELP :(

N

NewInTheGame

I'm a beginner in VB.Net. I'm actually taking a class & so far I
understand everything, but I suck when it comes to arrays. I'm sure
that this is simple (I'm ashamed of asking :blush:ops: )...

I have some textboxes. I'm trying to store the information in an
array, but I'm lost as to how: I have textboxes for FirstName,
LastName & PhoneNumber.

My problem is that the number of arrays is unknown so our teacher
wants us to declare it as (0) to begin with...this is what I have.
Can someone PLEASE point me to the right direction?

'Declaring the arrays
Dim strFirstName() As String
Dim strLastName() As String
Dim strPhone() As String

Dim intNum As Integer

Private Sub btnSave_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSave.Click

'1st try
intNum = intNum + 1
strFirstName(intNum) = txtFirstName.Text
intNum = intNum + 1
strLastName(intNum) = txtLastName.Text
intNum = intNum + 1
strPhone(intNum) = txtPhone.Text

'2nd try
intNum = intNum + 1
For intNum = intNum To intNum
strFirstName(intNum) = txtFirstName.Text
Next

I'm clueless as to what to try next. Please HELP!!! Thanks. :cry:
:cry:
 
S

Scott M.

Not exactly sure what you are trying to do but see comments inline:


NewInTheGame said:
I'm a beginner in VB.Net. I'm actually taking a class & so far I
understand everything, but I suck when it comes to arrays. I'm sure
that this is simple (I'm ashamed of asking :blush:ops: )...

I have some textboxes. I'm trying to store the information in an
array, but I'm lost as to how: I have textboxes for FirstName,
LastName & PhoneNumber.

My problem is that the number of arrays is unknown so our teacher
wants us to declare it as (0) to begin with...this is what I have.
Can someone PLEASE point me to the right direction?

'Declaring the arrays
Dim strFirstName() As String
Dim strLastName() As String
Dim strPhone() As String

Dim intNum As Integer

Private Sub btnSave_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSave.Click

'1st try
intNum = intNum + 1

Use the shorthand operand for this (more in line with how most other
languages do it)
intNum +=1

Also, why start intNum at one when arrays start from zero? You will never
have an item zero in your array.
strFirstName(intNum) = txtFirstName.Text

If you increase intNum here then you will have a strFirstName(1) and an
strLastName(2), but never a strLastName(1).
intNum = intNum + 1
strLastName(intNum) = txtLastName.Text
intNum = intNum + 1
strPhone(intNum) = txtPhone.Text

'2nd try
intNum = intNum + 1
For intNum = intNum To intNum

This loop would not run since you are starting and ending at the same
number.
strFirstName(intNum) = txtFirstName.Text
Next

I'm clueless as to what to try next. Please HELP!!! Thanks. :cry:
:cry:

Can you be more specific about what exactly you are trying to do? You say
the number of arrays is unknown. What does this mean? Do you just want to
store the data from the three textboxes in an array?
 
R

Ricky W. Hunt

NewInTheGame said:
My problem is that the number of arrays is unknown so our teacher
wants us to declare it as (0) to begin with...this is what I have.
Can someone PLEASE point me to the right direction?

It would seem you would want a two-dimensional array. But if you're confined
to one dimension...
'Declaring the arrays
Dim strFirstName() As String
Dim strLastName() As String
Dim strPhone() As String

Dim intNum As Integer

Private Sub btnSave_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSave.Click

'1st try
intNum = intNum + 1
strFirstName(intNum) = txtFirstName.Text
intNum = intNum + 1
strLastName(intNum) = txtLastName.Text
intNum = intNum + 1
strPhone(intNum) = txtPhone.Text

If you do it this way you'll have no way of matching the firstname and
lastname, for instance. What you want is something like:

strFirstName(intNum)=txtFirstName.Text
strLastName(intNum)=txtLastName.Text
strPhoneName(intNum)=txtPhone.Text
intNum +=1

That way the "0" index of each array will hold the first person's info,
index "1" of each array will hold the second person's info, etc.
 
C

Cor Ligthert

Hi New,

I think that your teacher want to have an answer from you what array you go
using is more important than how you are using that in this case.

To help you a little bit, you have three choises you can make, you use a
standard fixed array which you can redimension everytime, you use a dynamic
array (as well named collection), or you make your own arrayclass.

It is not for us to give solutions on homework in this newsgroup, I hope
that it helps you however to give you these hints.

Cor
 
R

Ricky W. Hunt

Cor Ligthert said:
It is not for us to give solutions on homework in this newsgroup, I hope
that it helps you however to give you these hints.

Sorry, Cor. I didn't know that. In the future I'll try to provide "hints"
instead of "full code" answers. Thanks.
 
C

Cor Ligthert

Hi Ricky,

Why "Sorry", luckily my hint was clear and I tried to point it not to you,
because I was sure you did not know that. You only did try to help.

:)

Cor
 
C

Chris Dunaway

My problem is that the number of arrays is unknown so our teacher
wants us to declare it as (0) to begin with...this is what I have.
Can someone PLEASE point me to the right direction?

If you need to resize the array, please have a look at the ReDim command

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
N

NewInTheGame

:D

Thank you guys so much!!!!! :p

I'm at work now, but as soon as I go home I'm going to give these a
try.

I REALLY appreciate your response :)
 
G

Guest

Hi

I think u canuse Redim with memory allocation

even u don't know how many rec. u have to enter Do with Redim preserve arrayname()

regards

saradhi
 
N

NewInTheGame

Ok....Just in case anybody else needs to know, I needed to ReDim the
array. Found out that I was trying to do a dinamic array...Again,
thank you guys for everything.... :wink:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top