Solved

The following code is my attempt to dynamically set the size of an array and then to dynamically assign values to it. However it is incomplete and I could do with some assistance.

Posted on 2004-08-30
10
242 Views
Last Modified: 2012-05-05
The following code is my attempt to dynamically set the size of an array and then to dynamically assign values to it. However it is incomplete and I could do with some assistance.

Any feedback would be appreciated.

If someone could complete the missing links in the code I would be very thankful.

Dim MyArray() As Integer
Dim nCountOfRecords As Integer
Dim I As Integer

Dim cnn As String = (ConfigurationSettings.AppSettings("SomeWebSite"))
    Dim objConnection as new sqlConnection(cnn)
     Dim objCommand as new SqlCommand("procThatCountsTheIDs", ObjConnection)
    objCommand.CommandType = CommandType.StoredProcedure
     
     Dim ObjCountRecords as new SqlParameter("@ObjCountRecords",SqlDbType.int)
     ObjCommand.Parameters.Add(ObjCountRecords)
     ObjCountRecords.Direction=ParameterDirection.Output 'Gets the value that will be the array size

Try
     objConnection.Open()
     ObjCommand.ExecuteNonQuery()
     objConnection.Close()
     Catch ex as Exception
     Response.Write("Error:" & ex.Message & "<br/>")
     End Try

' Assign the number of records to the 'nCountOfRecords' variable

nCountOfRecords =(ObjCommand.Parameters("@ObjCountRecords").Value)   'Should this be... (ObjCommand.Parameters("@ObjCountRecords").Value)-1.....?
    ReDim MyArray(nCountOfRecords)   ' re-dimension as needed

'It seems to me, the following code dynamically sets the size of the array so do I need to do another DbQuery and then assign the actual IDs to the  array?
    ' Assignment
    For I = 1 To nCountOfRecords
    MyArray(I) = I '<- this will a assign I to MyArray
    Next

'So now I need some code that assigns the ID to the newly created array

'So code here.............


'Now display ....say....MyArray(4) and MyArray(22)
'Just use a couple of labels or something.

Cheers

PG
0
Comment
Question by:pgilfeather
  • 4
  • 2
  • 2
10 Comments
 
LVL 2

Expert Comment

by:dimitry_dimitry
ID: 11930456
Use array.createinstance to create an array of specified type and length.
use array.copy to copy elements from array 1 to array 2, specifying starting and ending bounds.
these routines can be used to implement array resizing.
You can also use ReDim statement
0
 

Author Comment

by:pgilfeather
ID: 11930492
dimitry_dimitry

No point using English....Im using VB.NET

Cheers

PG
0
 
LVL 2

Expert Comment

by:dimitry_dimitry
ID: 11930564
And what?
VB.NET does not allow dynamic array creation methods that I posted?
0
 

Author Comment

by:pgilfeather
ID: 11930810
dimitry_dimitry,

Please dont mistake my short response as sarcasm, because I genuinly wasnt trying to be sarcastic.

The reason for such a short response was simply to convey that I was looking for some actual VB.NET code. It clearly says this in my question.

Sorry if I offended you, it was never my intention.

Thanks for trying to help, I really do appreciate it.

Kind Regards

PG
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:IceColdas
ID: 11930823
try this one
Dim MyArray() As Integer
...
ReDim Preserve  MyArray(nCountOfRecords)
0
 

Author Comment

by:pgilfeather
ID: 11930846
IceColdas

So how does this fit into my question.

Cheers

PG
0
 
LVL 3

Accepted Solution

by:
IceColdas earned 500 total points
ID: 11930883
declare at first MyArray as an array of 50 elements (or whatever u want)
read every value in MyArray when u execute the DBquerry, and if the size of the array must grow, then allocate another 50 elements to it. And so on.
0
 

Author Comment

by:pgilfeather
ID: 11930919
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
count8 challlenge 13 117
nestparen challenge 4 72
How  do I get an older program to run in Windows 10? 20 103
how to update exe applicatio from internet ? 6 67
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now