468,457 Members | 1,701 Online

# Cant assign array values ! ? ! ?

I am so lost on this one.

Dim Ary as Integer = New Integer(4) {0,1,2,3}

FAILS with the error:
Array initializer has 1 too few elements.

So I try
Dim Ary as Integer = New Integer(3) {0,1,2,3}
Since arrays start at 0 (I am thinking)

and I get this error:
Value of type '1-dimensional array of Integer' cannot be converted to 'Integer'.

I can friggin assign values to the array AT ALL ?

What is wrong with this ?

So I guess the question is why dosent this work ?

How can I dimension and assign values ?

Chris
Nov 20 '05 #1
7 2504
If you want an array, you need to declare an array...
( note the () after the Ary )
Dim Ary() As Integer = New Integer(3) {0, 1, 2, 3}

For i As Integer = 0 To Ary.Length - 1

Trace.WriteLine(Ary(i).ToString())

Next
"Chris Wertman" <cw******@yahoo.com> wrote in message
I am so lost on this one.

Dim Ary as Integer = New Integer(4) {0,1,2,3}

FAILS with the error:
Array initializer has 1 too few elements.

So I try
Dim Ary as Integer = New Integer(3) {0,1,2,3}
Since arrays start at 0 (I am thinking)

and I get this error:
Value of type '1-dimensional array of Integer' cannot be converted to 'Integer'.
I can friggin assign values to the array AT ALL ?

What is wrong with this ?

So I guess the question is why dosent this work ?

How can I dimension and assign values ?

Chris

Nov 20 '05 #2
Yes, and when initializing, you don't have to specify the number of elements
in the array, thus
Dim Ary() As Integer = New Integer() {0, 1, 2, 3}
will work as well.

"Jim Hughes" <NO*********@Hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
If you want an array, you need to declare an array...
( note the () after the Ary )
Dim Ary() As Integer = New Integer(3) {0, 1, 2, 3}

For i As Integer = 0 To Ary.Length - 1

Trace.WriteLine(Ary(i).ToString())

Next
"Chris Wertman" <cw******@yahoo.com> wrote in message
I am so lost on this one.

Dim Ary as Integer = New Integer(4) {0,1,2,3}

FAILS with the error:
Array initializer has 1 too few elements.

So I try
Dim Ary as Integer = New Integer(3) {0,1,2,3}
Since arrays start at 0 (I am thinking)

and I get this error:
Value of type '1-dimensional array of Integer' cannot be converted to

'Integer'.

I can friggin assign values to the array AT ALL ?

What is wrong with this ?

So I guess the question is why dosent this work ?

How can I dimension and assign values ?

Chris

Nov 20 '05 #3
<you don't have to specify the number of elements in the array>

EXCEPT when you are not specifying the values at the time of creating the
array. In such a case you DO have specify the number of elements ahead of
time. Just something to keep in mind.

public sub test()
dim ary(0) as integer
ary(0) = 1

'to add another element--but this will overwrite previous elements
redim ary(1)
ary(1)=2

msgbox(ary(0).tostring +vbcrlf + ary(0).tostring) '<-this will display 0
even though you previously set it to 1

'set it back to 1
ary(0) = 1

'here we add another element while leaving the previous elements untouched.
redim preserve ary(2)
ary(2) = 3

msgbox(ary(0).tostring + vbcrlf + ary(1).tostring + vbcrlf +
ary(2).tostring)
end sub

--

RDI

(remove the exclamation from the email address)

"Chris Botha" <chris_s_botha@AT_h.o.t.m.a.i.l.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Yes, and when initializing, you don't have to specify the number of elements in the array, thus
Dim Ary() As Integer = New Integer() {0, 1, 2, 3}
will work as well.

"Jim Hughes" <NO*********@Hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
If you want an array, you need to declare an array...
( note the () after the Ary )
Dim Ary() As Integer = New Integer(3) {0, 1, 2, 3}

For i As Integer = 0 To Ary.Length - 1

Trace.WriteLine(Ary(i).ToString())

Next
"Chris Wertman" <cw******@yahoo.com> wrote in message
I am so lost on this one.

Dim Ary as Integer = New Integer(4) {0,1,2,3}

FAILS with the error:
Array initializer has 1 too few elements.

So I try
Dim Ary as Integer = New Integer(3) {0,1,2,3}
Since arrays start at 0 (I am thinking)

and I get this error:
Value of type '1-dimensional array of Integer' cannot be converted to

'Integer'.

I can friggin assign values to the array AT ALL ?

What is wrong with this ?

So I guess the question is why dosent this work ?

How can I dimension and assign values ?

Chris

Nov 20 '05 #4
That's what I said, "when initializing, you don't have to specify ...",

"rdi" <rd**@writeme.com> wrote in message
news:O9**************@TK2MSFTNGP09.phx.gbl...
<you don't have to specify the number of elements in the array>

EXCEPT when you are not specifying the values at the time of creating the
array. In such a case you DO have specify the number of elements ahead of
time. Just something to keep in mind.

public sub test()
dim ary(0) as integer
ary(0) = 1

'to add another element--but this will overwrite previous elements
redim ary(1)
ary(1)=2

msgbox(ary(0).tostring +vbcrlf + ary(0).tostring) '<-this will display 0
even though you previously set it to 1

'set it back to 1
ary(0) = 1

'here we add another element while leaving the previous elements untouched. redim preserve ary(2)
ary(2) = 3

msgbox(ary(0).tostring + vbcrlf + ary(1).tostring + vbcrlf +
ary(2).tostring)
end sub

--

RDI

(remove the exclamation from the email address)

"Chris Botha" <chris_s_botha@AT_h.o.t.m.a.i.l.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Yes, and when initializing, you don't have to specify the number of

elements
in the array, thus
Dim Ary() As Integer = New Integer() {0, 1, 2, 3}
will work as well.

"Jim Hughes" <NO*********@Hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
If you want an array, you need to declare an array...
( note the () after the Ary )
Dim Ary() As Integer = New Integer(3) {0, 1, 2, 3}

For i As Integer = 0 To Ary.Length - 1

Trace.WriteLine(Ary(i).ToString())

Next
"Chris Wertman" <cw******@yahoo.com> wrote in message
> I am so lost on this one.
>
> Dim Ary as Integer = New Integer(4) {0,1,2,3}
>
> FAILS with the error:
> Array initializer has 1 too few elements.
>
> So I try
> Dim Ary as Integer = New Integer(3) {0,1,2,3}
> Since arrays start at 0 (I am thinking)
>
> and I get this error:
> Value of type '1-dimensional array of Integer' cannot be converted to 'Integer'.
>
> I can friggin assign values to the array AT ALL ?
>
> What is wrong with this ?
>
> So I guess the question is why dosent this work ?
>
> How can I dimension and assign values ?
>
> Chris

Nov 20 '05 #5
Hi Chris

Dim Ary as Integer = New Integer(4) {0,1,2,3}

Dim Ary() as Integer = {0,1,2,3}

I hope this helps?

Cor
Nov 20 '05 #6
* cw******@yahoo.com (Chris Wertman) scripsit:
I am so lost on this one.

Dim Ary as Integer = New Integer(4) {0,1,2,3}

FAILS with the error:
Array initializer has 1 too few elements.

So I try
Dim Ary as Integer = New Integer(3) {0,1,2,3}
Since arrays start at 0 (I am thinking)

and I get this error:
Value of type '1-dimensional array of Integer' cannot be converted to 'Integer'.

I can friggin assign values to the array AT ALL ?

What is wrong with this ?

\\\
Dim i() As Integer = {1, 2, 3}
///

- or -

\\\
Dim i As Integer() = {1, 2, 3}
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #7
I DID read your message and I didn't say you were wrong. The OP sounded
like he was new to arrays and I thought he might take
"you don't have to specify the number of elements in the array when
initializing, you don't have to specify" as meaning that he didn't need to
specify the number of arrays anymore.

I meant no offense to anyone and like I said, I wasn't trying to say that
you were wrong.
--

RDI

(remove the exclamation from the email address)

"Chris Botha" <chris_s_botha@AT_h.o.t.m.a.i.l.com> wrote in message
news:O7**************@TK2MSFTNGP09.phx.gbl...
That's what I said, "when initializing, you don't have to specify ...",

"rdi" <rd**@writeme.com> wrote in message
news:O9**************@TK2MSFTNGP09.phx.gbl...
<you don't have to specify the number of elements in the array>

EXCEPT when you are not specifying the values at the time of creating the
array. In such a case you DO have specify the number of elements ahead of time. Just something to keep in mind.

public sub test()
dim ary(0) as integer
ary(0) = 1

'to add another element--but this will overwrite previous elements
redim ary(1)
ary(1)=2

msgbox(ary(0).tostring +vbcrlf + ary(0).tostring) '<-this will display 0
even though you previously set it to 1

'set it back to 1
ary(0) = 1

'here we add another element while leaving the previous elements

untouched.
redim preserve ary(2)
ary(2) = 3

msgbox(ary(0).tostring + vbcrlf + ary(1).tostring + vbcrlf +
ary(2).tostring)
end sub

--

RDI

(remove the exclamation from the email address)

"Chris Botha" <chris_s_botha@AT_h.o.t.m.a.i.l.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Yes, and when initializing, you don't have to specify the number of

elements
in the array, thus
Dim Ary() As Integer = New Integer() {0, 1, 2, 3}
will work as well.

"Jim Hughes" <NO*********@Hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
> If you want an array, you need to declare an array...
> ( note the () after the Ary )
> Dim Ary() As Integer = New Integer(3) {0, 1, 2, 3}
>
> For i As Integer = 0 To Ary.Length - 1
>
> Trace.WriteLine(Ary(i).ToString())
>
> Next
>
>
> "Chris Wertman" <cw******@yahoo.com> wrote in message
> > I am so lost on this one.
> >
> > Dim Ary as Integer = New Integer(4) {0,1,2,3}
> >
> > FAILS with the error:
> > Array initializer has 1 too few elements.
> >
> > So I try
> > Dim Ary as Integer = New Integer(3) {0,1,2,3}
> > Since arrays start at 0 (I am thinking)
> >
> > and I get this error:
> > Value of type '1-dimensional array of Integer' cannot be converted

to > 'Integer'.
> >
> > I can friggin assign values to the array AT ALL ?
> >
> > What is wrong with this ?
> >
> > So I guess the question is why dosent this work ?
> >
> > How can I dimension and assign values ?
> >
> > Chris
>
>

Nov 20 '05 #8

### This discussion thread is closed

Replies have been disabled for this discussion.