Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

VB: Array... was mach ich falsch?

Empfohlene Antworten

Hi,

hier eine Übung, mit der ich mir die Funktionsweise von Arrays etwas näher bringen wollte... Jetzt pfunzt das Teil net... Was mach ich da falsch???:

QUELLTEXT:

Option Explicit

Dim Eingabe() As Variant

Dim i, iz As Integer

Dim Text As String

Private Sub Command1_Click()

If Not Text1.Text = "" Then

Eingabe(i) = Text1.Text

i = i + 1

Else

For iz = 0 To i Step 1

Text = Text & iz & ": " & Eingabe(iz) & " - "

Next iz

MsgBox Text

End If

End Sub

Private Sub Form_Load()

i = 0

End Sub

Wäre echt nett, wenn Ihr mir helfen könntet.

Danke,

Chris;-)

so auf die schnelle :

du musst doch angeben wie groß dein array sein soll

z.B.

Dim Eingabe(5) As Variant

oder

du kannst es später wärend der laufzeit

mit ReDim Eingabe(i+1) neu dimenensionieren

so irgendwie :D

Du solltest es mal so versuchen:

Option Explicit

Dim Eingabe() As Variant

Dim i, iz As Integer

Dim Text As String

Private Sub Command1_Click()

If Not Text1.Text = "" Then

ReDim Eingabe(i)

Eingabe(i) = Text1.Text

i = i + 1

Else

For iz = 0 To i Step 1

Text = Text & iz & ": " & Eingabe(iz) & " - "

Next iz

MsgBox Text

End If

Originally posted by neversaynever

Du solltest es mal so versuchen:

ReDim Eingabe(i)

End If

ein array von der göße 0 wird nicht so ganz hinhauen.

musst schon i+1 machen

Allerdings gehen dir Sachen die im Array drin sind verloren wenn du nur ReDim benutzt, wenn du die Daten die schon drin sind behalten willst und nur ein paar Felder dranhängen willst, dann brauchst du noch Preserve.

Also:

ReDim Preserve Variable(i +1)

Hi,

danke für die schnelle Reaktion.... Leider bringt mir VB jetzt eine Fehlermeldung..

"Typen unverträglich"... genau an der Stelle, an der ich neu dimensioniere...

Option Explicit

Dim Eingabe As Variant

Dim i, iz As Integer

Dim Text As String

Private Sub Command1_Click()

If Not Text1.Text = "" Then

i = i + 1

ReDim Preserve Eingabe(i) As Variant

Eingabe(i) = Text1.Text

Else

For iz = 0 To i Step 1

Text = Text & iz & ": " & Eingabe(iz) & " - "

Next iz

MsgBox Text

End If

End Sub

Private Sub Form_Load()

i = 0

End Sub

HELP!

THX!

Chris;-)

Originally posted by ChrisDaHub

Dim Eingabe As Variant

...

ReDim Preserve Eingabe(i) As Variant

...

Das liegt daran das Du Eingabe am Anfang als normale Variable deklarierst und später redimensionieren willst, so müßte es dann gehen:

Dim Eingabe() As Variant

gruß

Christian

globale definition :

Dim Eingabe() As Variant

und das später in deiner sub :

ReDim Preserve Eingabe(i+1)

weil das ist hier ist nicht sauber :

i = i + 1

ReDim Preserve Eingabe(i)

ein array muß immer bei index 0 anfangen und hier bist du schon am anfang bei dem index 1

gruß

MadCro

:marine

Hallo



Option Explicit

Dim aryEingabe() as String ' Variant verschwendet viel Speicher und ist nicht nötig

Dim strText as String 

Dim intIndex as Integer

Dim intCount as Integer 


Private Sub cmdOk_Click()


         If txtText1.Text <> "" then

                   aryEingabe(intIndex)=txtText1.Text

                   intIndex=intIndex+1

                   Redim Preserve aryEingabe(intIndex)

         Else

                   For intCount = 0 to intIndex ' oder Ubound(aryEingabe)

                            strText = strText & intIndex & ":" & aryEingabe(intCount)

                   Next intCount

                   MsgBox strText

        End If

End sub


Private Sub frmText_Load

         intIndex = 0

         Redim aryEingabe(0)

End Sub


Ciao

Hallo,

@daking: leider funktioniert das immer noch nicht so ganz...

Ich bekomme wieder einen Fehler... Index ungültig... An der Stelle, an der ich einen String an das Array übergeben will.. Habe deinen Code so wie er ist kopiert und eingefügt...

Gruß,

Chris

hi,

 

Option Explicit

Option Base 0

Private aryEingabe() as String 


Private Sub cmdOk_Click()

Dim strText as String 

Dim intCount as Integer 

Dim intIndex as Integer


    intIndex = UBound(aryEingabe)


    If txtText1.Text <> "" then

        intIndex = intIndex + 1

        Redim Preserve aryEingabe(intIndex)

        aryEingabe(intIndex - 1)=txtText1.Text

    Else

        For intCount = LBound(aryEingabe) to Ubound(aryEingabe)

            strText = strText & "An Position " & CStr(intCount) & ": " & aryEingabe(intCount) & vbNewLine

        Next intCount

        MsgBox strText

    End If

End sub


imho ist fuer so etwas die collection schoener als ein array...

hoffe es hilft


Option Explicit

Dim aryEingabe() As String  ' Variant verschwendet viel Speicher und ist nicht nötig

Dim strText As String

Dim intIndex As Integer

Dim intCount As Integer


Private Sub cmdOk_Click()


         If txtText1.Text <> "" Then

                   ReDim Preserve aryEingabe(intIndex)

                   aryEingabe(intIndex) = txtText1.Text

                   intIndex = intIndex + 1


         Else

                   For intCount = 0 To UBound(aryEingabe)

                            strText = strText & intCount & ":" & aryEingabe(intCount) & vbNewLine

                   Next intCount

                   MsgBox strText

        End If

End Sub


Private Sub frmText_Initialize()

         intIndex = 0

End Sub



natürlich sollte die Variable intIndex auch einen sinnvollen wert haben.

Also wen er beim Zuweisen in das Array größer ist als Das Array dimensioniert wurde ist es ganz normal das man eine Fehlermeldung bekommt mit Index ausserhalb ....

Also am besten ist du weist wie groß das Array sein soll und machst

Dim arrTest (5) as string

oder wenn du es nicht wissen solltest dann mit

dim arrTest() as string

dann im Quellcode irgendwo vor dem füllen des Arrays:

intIndex = 12

redim arrTest(intIndex)

und dann kannst du mit der 0 - intIndex-1 in das Array Werte eintragen.

Gruß

Guido

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.