473,657 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Two dimension Array

I would like to store the array like as following
arItem(0,0)= "APPLE" <-- string
arItem(0,1)=30 <-- no of item , integer
arItem(1,0) = "ORANGE"
arItem(1,1) = 16
.....ETC
From the help , i know I can use arItem.setvalue ("APPLE",0,0).. . ETC
However, I don't know how to declare such array
Please help ~
Nov 21 '05 #1
4 1282
Agnes,
Have you tried:

Dim arItem(9,1) As Object
arItem(0,0)= "APPLE" <-- string
arItem(0,1)=30 <-- no of item , integer
arItem(1,0) = "ORANGE"
arItem(1,1) = 16
...
arItem(9,0)= "Banana"
arItem(9,1)=30

However seeing as each row appears to be an object. I would consider
creating an array of Objects instead.

Something like:
Public Class Item
Private Readonly m_name As String
Private m_ount As Integer
Public Sub New(name As String, count As Integer)
m_name = name
m_count = count
End Sub
Public ReadOnly Property Name As string
Get
Return m_Name
End Get
End Property
Public Property Count As Integer
Get
Return m_count
End Get
Set(value As Integer)
m_count = value
End Set
End Property
...
End Class

Dim arItem(9) As Item
arItem(0) = New Item("Apple", 30)
arItem(1) = New Item("Orange", 16)
...
arItem(9) = New Item("Banana", 30)

Note, because Item is defined to be a class you need to initialize each row
of the array as I have done above. Once each row is initialized, you can use
the properties of Item to get at its values:

' sell an apple
arItem(0).Count -= 1

Hope this helps
Jay

"Agnes" <ag***@dynamict ech.com.hk> wrote in message
news:uE******** ******@TK2MSFTN GP12.phx.gbl...
I would like to store the array like as following
arItem(0,0)= "APPLE" <-- string
arItem(0,1)=30 <-- no of item , integer
arItem(1,0) = "ORANGE"
arItem(1,1) = 16
....ETC
From the help , i know I can use arItem.setvalue ("APPLE",0,0).. . ETC
However, I don't know how to declare such array
Please help ~

Nov 21 '05 #2
Agnes,

You have so often worked with a datatable. When an array get more dimensions
than that one is in my opinion one of the easiest one.

\\\
dim dt as new datatable
dt.columns.add( "Fruit", gettype(system. string))
dt.columns.add( "Value", gettype(system. Int32))
dim dr as datarow = dt.newrow
dr("Fruit") = "APPLE"
dr("Value")=30
dt.rows.add(dr)
dt.rows.add(dt. newrow)
dt.rows(1)(0) = "ORANGE"
dt.rows(1)(1) = 16
dt.rows.add(dt. newrow)
dt.rows(2).item array = new object() {"Citrus",20 }
///
Etc in the way you like

I hope this helps?

Cor

"Agnes" <ag***@dynamict ech.com.hk>
I would like to store the array like as following
arItem(0,0)= "APPLE" <-- string
arItem(0,1)=30 <-- no of item , integer
arItem(1,0) = "ORANGE"
arItem(1,1) = 16
....ETC
From the help , i know I can use arItem.setvalue ("APPLE",0,0).. . ETC
However, I don't know how to declare such array
Please help ~

Nov 21 '05 #3
"Agnes" <ag***@dynamict ech.com.hk> schrieb:
I would like to store the array like as following
arItem(0,0)= "APPLE" <-- string
arItem(0,1)=30 <-- no of item , integer
arItem(1,0) = "ORANGE"
arItem(1,1) = 16
....ETC
From the help , i know I can use arItem.setvalue ("APPLE",0,0).. . ETC
However, I don't know how to declare such array

Instead of using a 2-dimensional array here, I would use a 1-dimensional
array of instances of a class/structure:

\\\
Public Structure Item
Public Name As String
Public Number As Integer
End Structure
..
..
..
Dim a(90) As Item
For i As Integer = 0 To a.Length - 1
a(i).Name = ...
a(i).Number = ...
Next i
///

Notice that this is a very basic sample, if you are storing more data for
each object, consider using a class, and add properties instead of providing
direct access to the fields holding the attributes' values.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4
Thanks All.
Dear Herfried K. Wanger,
I never know I can use 'structure' to do that , I learn one more new
thing. Thanks again

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> ???
news:uT******** *****@TK2MSFTNG P12.phx.gbl ???...
"Agnes" <ag***@dynamict ech.com.hk> schrieb:
I would like to store the array like as following
arItem(0,0)= "APPLE" <-- string
arItem(0,1)=30 <-- no of item , integer
arItem(1,0) = "ORANGE"
arItem(1,1) = 16
....ETC
From the help , i know I can use arItem.setvalue ("APPLE",0,0).. . ETC
However, I don't know how to declare such array

Instead of using a 2-dimensional array here, I would use a 1-dimensional
array of instances of a class/structure:

\\\
Public Structure Item
Public Name As String
Public Number As Integer
End Structure
.
.
.
Dim a(90) As Item
For i As Integer = 0 To a.Length - 1
a(i).Name = ...
a(i).Number = ...
Next i
///

Notice that this is a very basic sample, if you are storing more data for
each object, consider using a class, and add properties instead of

providing direct access to the fields holding the attributes' values.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1912
by: Michael Kirchner | last post by:
Hi everybody The output of my multiple dimension array is quite confusing. Im declaring an array, store some values in it and then I save the array in a session variable. On an other page I store the data of the session in a new multiple dimension array. All data are saved correctly in the array of the 1st page. But in the new array of the 2nd page there is only one entry. Does anybody knows
9
7756
by: James | last post by:
Hi, I am new to C++. I want to directly create a dynamic two-dimension double array, i.e. double pp. I found the "new" is only for one-dimension array, i.e. double *p = new p. How to "new" a two-dimension array? Is the only way to create a class? In java, I can easily create a two-dimension array by "double pp = new double". But, how to do it in C++?
6
2229
by: Adam Hartshorne | last post by:
The input to a function of a 3rd party library I want to use requires a double**, which is a multi-dimension array of doubles. I have looked on the net etc and seen several ways of supposedly doing this, but I don't seem to be able to get them to work. I was wondering if anybody can tell me what I am doing wrong. int rows = 10 ; int cols = 10 ;
4
4328
by: Bill Sun | last post by:
Hi, All I have a conventional question, How to create a 3 dimension array by C language. I see some declare like this: int *** array; int value; array = create3Darray(m,n,l);
3
4547
by: tg | last post by:
How can I sort a two-dimension array on the first dimension in the array? If there's not a way to do it, how can I return the values of the first dimension so that I can sort the values myself (bubble sort, etc.)? Thank you
4
4741
by: MattB | last post by:
I have an one dimensional array being created from a delimited list using string.split. Now I'd like to take that array and add another dimension and manually put a value in there based on the contents of the first element. I can loop through my array no problem, but I don't see how (if?) I can add another "column" to it. Seems like it should be simple. Can I do this? How? TIA! Matt
2
4560
by: Nathan Sokalski | last post by:
I have a multidimensional array declared as the following: Dim guesses(14, 5) As Integer I want to assign all values in a specific dimension to another array declared as follows:
5
7802
by: Jackson | last post by:
I have something that is stumping me. I am trying to initialize a 3 dimensional string array with the code below, but it wont compile. Can anyone explain what Im doing wrong?????????????? Im going crazy with this one! Ive tried a 2 dimensional array minus the "test3" and it works fine. public string screens; screens = new string{ {"test1","test2","test3"} };
5
2690
by: Martin Pöpping | last post by:
Hello, I want to iterate the second dimension of a 2-dim-array. Let´s say I have an array: double myArray and a given index: int i. Assume my index is given in want to iterate my array with something like this:
9
3748
by: dennis.sam | last post by:
Hi, Is there away to define a multi-dimensional array with respect to the number of dimensions the array has? For example, given a user spec of "a b c d", I want to create a 4 dimensional array with dimensional lengths of a, b, c and d. Thanx for any help.
0
8411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8323
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8613
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7351
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.