Solved

Array & Arraylist

Posted on 2004-08-09
12
2,706 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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

20 Experts available now in Live!

Get 1:1 Help Now