1

I need to insert unique values once into an array without looping like below statement(I'm using visual basic 6.0)

Dim Marks(0 To 9) As Integer = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,}

but it has got compile error. how should be changed above statement ??

1
  • 1
    Short answer: VB6 doesn't do array literals. Longer answer: stackoverflow.com/questions/17443632/… (yes its for strings, but you can see how it would apply to ints) Commented Jun 24, 2015 at 8:39

2 Answers 2

2

From Alex K's answer, but converted to int:

Function ArrayInt(ParamArray tokens()) As Integer()
    ReDim arr(UBound(tokens)) As Integer
    Dim i As Long
    For i = 0 To UBound(tokens)
        arr(i) = tokens(i)
    Next
    ArrayInt = arr
End Function

Usage:

Dim Marks() As Integer
Marks = ArrayInt(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
Sign up to request clarification or add additional context in comments.

Comments

0

this is the easier way that I could found

Dim marks 
marks = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

1 Comment

That doesn't actually do what you want (quite). The function array() returns an array of variants. And by declaring (dim) marks with no type at all, you have declared it as a variant itself. This leaves you vulnerable to a whole load of problems in your code, as it is possible that that marks will not be an array at all, or that it may contain values that are not integers. At the very least, declare dim marks as variant()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.