Solved

Array & Arraylist

Posted on 2004-08-09
12
2,716 Views
Last Modified: 2008-01-09
whats diffrence between array and arraylist

please give a sample code for both
0
Comment
Question by:Basic
  • 6
  • 6
12 Comments
 
LVL 10

Expert Comment

by:Clif
ID: 11755628
Briefly

Array is a fixed-capacity construct that can have one or more dimensions. The .NET Framework supports multi-dimensional arrays similar to Pascal arrays, and jagged arrays such as you find in C.

ArrayList is a one-dimensional, expandable list that provides some advanced capabilities like the ability to add, insert, or remove multiple items in a single operation.

And not so briefly

Array:
http://www.informit.com/guides/content.asp?g=dotnet&seqNum=115

ArrayList:
http://www.informit.com/guides/content.asp?g=dotnet&seqNum=119
0
 

Author Comment

by:Basic
ID: 11755989
i am creating afunction which returns a array
but it doest working out can u help me
0
 
LVL 10

Expert Comment

by:Clif
ID: 11756070
Show your code.

What error is being thrown (if any)?
What is it doing that it's not supposed to be doing?
What's it not doing that it should be doing?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:Basic
ID: 11764618
  Function getArray(ByVal id As Integer) As Array
       
       ....................
          ........................
         
            cmd = New OracleCommand(sql, con)
            dr = cmd.ExecuteReader()
            i = 0
            While dr.Read()
                        myArray(i) = dr("ID")

                       i = i + 1
            End While
            dr.Close()
            con.Close()
       
            Return CType(myArray,Array)


   End Function
0
 
LVL 10

Expert Comment

by:Clif
ID: 11764703
You don't declare myArray (unless that's part of the code that's not shown).
Once you declare it, you shouldn't have to use CType (It should already be an array).
Remember when you return the array, you don't need the parenthesis.

    Good:
    Return myArray

    Bad:
    Return myArray()

0
 

Author Comment

by:Basic
ID: 11764999

i changed

 Return CType(myArray,Array) to

 Return myArray

i am getting this error

Value cannot be null. Parameter name: Argument 'Array' is Nothing.
0
 
LVL 10

Expert Comment

by:Clif
ID: 11765051
Do you declare myArray within the function getArray()?

How do you declare it?

0
 

Author Comment

by:Basic
ID: 11765144
here is the compelte code

 Function getArray(ByVal id As Integer) As Array
       Dim myArray As Array
        Dim sql As String
        openConection()
        sql = "select ID from myTable where empID=" & id
         
            cmd = New OracleCommand(sql, con)
            dr = cmd.ExecuteReader()
            i = 0
            While dr.Read()
                        myArray(i) = dr("ID")

                       i = i + 1
            End While
            dr.Close()
            con.Close()
       
            Return myArray


   End Function
0
 
LVL 10

Accepted Solution

by:
Clif earned 50 total points
ID: 11765481
I think there is more than you are showing.

I tried your code and get a "Object variable or With block variable not set" exception at:
myArray(i) = dr("ID")

Basically you need to instantiate you array before you use it.

I would suggest the following code:

    Function getArray(ByVal id As Integer) As Array
        Dim myArray() As String
        Dim sql As String
        Try
            openConection()
            sql = "select ID from myTable where empID=" & id

            cmd = New OracleCommand(sql, con)
            dr = cmd.ExecuteReader()
 
            Dim i As Integer = 0
            While dr.Read()
                ReDim Preserve myArray(i)
                myArray(i) = dr.Item("ID")
                i = i + 1
            End While
            dr.Close()
            con.Close()
 
            If Not IsArray(myArray) Then
                ReDim myArray(0)
            End If

            Return myArray

        Catch ex As Exception
            Throw ex
        End Try
    End Function
0
 

Author Comment

by:Basic
ID: 11765542
Bingo!........its working
Clif ..thank you for helping even though i have few points to give
0
 

Author Comment

by:Basic
ID: 11765568
0
 
LVL 10

Expert Comment

by:Clif
ID: 11765702
Basic,
I'm sorry, but such a question is highly involved and I just don't have a lot of time at the moment.

No offense, but I think "System.Configuration.ConfigurationSettings.GetConfig" is a bit in depth for you at this point.

Good luck, though.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

765 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