Arrays

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I am trying to use an array in my vb.net windows appliaction but i keep encountering an error
This is what i have done so far

I have declared 2 arrays and 1 varible (to keep track of the index position
Public MyMealTypeArray() As Strin
Public MyMealQtyArray() As Intege
Public ArrayIndex As Integer =

I then add values to my array in an onclick event of a button

'if the items are not empty push the data to my array
If Not ((CBoxMealName.Text = "") Or (txtMealQty.Text = "")) The
MyMealTypeArray(ArrayIndex) = CBoxMealName.Tex
MyMealQtyArray(ArrayIndex) = txtMealQty.Tex

'increment the arrayInde
ArrayIndex = ArrayIndex +
Els
'else display a message to state all required field are neede
MsgBox("Please ensure all the required fields are entered correctly"
End I

Once all the data has been gathered, i then try to push the data that is in my arrays into a datatable

Dim MealInsRow As DataRo

For ArrayIndex = 0 To ArrayInde
MealInsRow = dsFullBooking.Tables("dtMealBooking").NewRow(
MealInsRow("BookingNo") = "-1
MealInsRow("RoomID") = RoomI
MealInsRow("RoomDt") = dtpRoomDate.Tex
MealInsRow("Req_Session") = CBoxSession.Tex
MealInsRow("Meal_Type") = CBoxMealName.Tex
MealInsRow("Meal_Qty") = txtMealQty.Tex
'Add the new record to the table by calling the Add method of the DataRowCollection object.
dsFullBooking.Tables("dtMealBooking").Rows.Add(MealInsRow
Next ArrayInde

This is the error that i get
An unhandled exception of type 'System.NullReferenceException' occurred in Caravan.ex
Additional information: Object reference not set to an instance of an object

My application stops at this line
MyMealTypeArray(ArrayIndex) = CBoxMealName.Tex

Can anyone see where i am going wrong
Any help would be great
Thank
 
Hi Eva, Ar,, ... Bhavna,

I have seen this booking thing with every woman name there is on the world I
think.

:-))

I had a solution, but what is the sence of this operation, you do not need
the array I think in the way you describe it now.

There is a dataset.rejectchanges did you know?

Have a look for that.

But maybe I mis something.

Cor
 
Hello Cor

This is the way i have been forced to do it as i cannot add these values to my datatable until the data has been added to my parent table first (referential integrity). As the data to my parent table cannot be added until later i have chose to add the child tables data into a array until the right time

Can u help me for gettin this array to work
I know of this other person. This is a school project so i guess everyone has latched onto this site for help. :o
 
You can fix this probelm By:

1. Gave those arrays a size
Public MyMealTypeArray(50) As String
Public MyMealQtyArray(50) As Integer

or

2. use arrayList

Public MyMealTypeArray as arrayList
Public MyMealQtyArray As arrayList

then

MyMealTypeArray.add(CBoxMealName.Text)
MyMealQtyArray.add(cint(txtMealQty.Text))

good luck
 
Hi Bhavna,

I would give almost the same answer as Mike,

I would prefer the arraylist.

and if it was the array, not the 50 but make it in your sub and than use the
index for the length of the array.

But normaly we do not help for school project you know so I will keep this
in mind..

Cor
 
When you declare an array, you have to gave a size but you don't have to
initialize 50 items.

in my previous reply, the correct code to declare an arrayList is:

dim al as new arraylist()
al.add(Value)

you don't have to declare size for AL.


Bhavna said:
Hello,

I have got it working by declaring my array to be of size 50.
Public MyMealTypeArray(50) As String
Do u always need to declare the length of the array? The problem with
stating the length for me is that i do not actually know this value. It is
upto the user how many entries they make!!!
Also if i declare the array of length 50, do i need to enter 50 items into
my array or does this simply mean anything upto 50?
I tried the arraylist code that u provided but i got the same error
message i stated in my first post.
Thank u for your help guys :o)
Cor, when u say u normally cannot help student projects, is it just for
this group?? am i posting in the incorrect place?
 
Cor, when u say u normally cannot help student projects, is it just for
this group?? am i posting in the incorrect place?

No that is in most newsgroup.
Making homework is not good for the student, although helping is another
situation.

The difference is that I now watch to give samples.

But Mike gives the same advices as I would give in this case,

(Although I would go to look what the reject changes could do for me).

That undos all changes you have made to the dataset after the last let say
"update or fill".

Cor
 
Back
Top