Solved

Array & Arraylist

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

please give a sample code for both
0
Comment
Question by:Basic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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