Komischer Array-Fehler

  • VB.NET

Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von ErfinderDesRades.

    Komischer Array-Fehler

    Hallo

    Ich bekomme bei folgenden Codezeilen Fehler:

    VB.NET-Quellcode

    1. Dim Bound As New Array(-10, 0, 15, 25, 30, 35, 40, 45)
    2. Dim R As New Array(20, 23, 15, 20, 136, 145, 91, 88, 142)
    3. Dim G As New Array(14, 15, 85, 113, 136, 88, 55, 88, 142)
    4. Dim B As New Array(171, 199, 9, 11, 56, 41, 26, 88, 142)


    Fehler: "New" darf nicht für eine Klasse verwendet werden die als "MustInherit" deklariert ist.

    Wenn ich aber die "New(s)" vor "Array" entferne bekomm ich bei der ersten Zahl folgenden Fehler:

    Fehler: Arraygrenzen können nicht in Typbezeichnungen stehen

    Kann mir jemand helfen
    :) :) :) :)

    MFG
    PhoenixBlaster :)
    So deklariert man keine Arrays.

    VB.NET-Quellcode

    1. Dim Bound As Integer() = {-10, 0, 15, 25, 30, 35, 40, 45}
    2. Dim R As Integer() = {20, 23, 15, 20, 136, 145, 91, 88, 142}
    3. Dim G As Integer() = {14, 15, 85, 113, 136, 88, 55, 88, 142}
    4. Dim B As Integer() = {171, 199, 9, 11, 56, 41, 26, 88, 142}
    Ich hab noch ein paar mehr Fehler:

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Class Form1
    2. Const ITERATIONS = 8 ' Increase this value to increase the size of the map
    3. Const ROW = (2 ^ ITERATIONS) + 1 ' The width of the map when drawn. Used in calculations.
    4. Const SLOPE = 15 ' This value affects how "jagged" the terrain is
    5. Dim Heights(ROW - 1, ROW - 1) As Single ' Array of height values
    6. Dim R, G, B, Bound As Array ' Variant array variables
    7. ' SetPixelV API call is faster than PSet
    8. Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
    9. Private Sub Form_Load()
    10. Dim Seed As Single ' Seed value for the random number generator
    11. ' This array defines the boundaries for different colors
    12. Dim Bound As Integer() = {-10, 0, 15, 25, 30, 35, 40, 45}
    13. Dim R As Integer() = {20, 23, 15, 20, 136, 145, 91, 88, 142}
    14. Dim G As Integer() = {14, 15, 85, 113, 136, 88, 55, 88, 142}
    15. Dim B As Integer() = {171, 199, 9, 11, 56, 41, 26, 88, 142}
    16. ' Change these values to create different effects. For a more defined
    17. ' coastline, make the first two Bound values the same. If you create
    18. ' more color boundaries, the terrain can become more detailed.
    19. Randomize(Seed) ' It should be possible to obtain previous maps by using their seed values here
    20. End Sub
    21. Private Sub Generate_Heights()
    22. ' This sub generates the height map which is placed in the Heights array. The
    23. ' heights define which color is shown when the map is drawn.
    24. '
    25. ' The method it uses is quite difficult to explain, but here goes...
    26. '
    27. ' The process of generation is best described by the following progression:
    28. '
    29. ' Initial | i = 0 | i = 1
    30. ' state: | |
    31. ' 1 1 | 1 2 1 | 1 3 2 3 1
    32. ' | | 3 3 3 3 3
    33. ' | 2 2 2 | 2 3 2 3 2 etc.
    34. ' | | 3 3 3 3 3
    35. ' 1 1 | 1 2 1 | 1 3 2 3 1
    36. '
    37. ' Initially, the four corners are given random height values. Then, in each
    38. ' iteration height values are created for the points that are halfway between
    39. ' the points in the last iteration, and the points in the middle of four values.
    40. ' So, when i = 0, the "2"s in the diagram represent the added height values, and
    41. ' when i = 1, the "3"s in the diagram are the next lot of added height values.
    42. ' This is repeated until the required number of iterations is reached. The
    43. ' height values for the new points are determined by taking the average of
    44. ' surrounding points and adding a random displacement to this. The amount of
    45. ' displacement from the average is determined by the SLOPE constant, and also
    46. ' by the iteration counter, i. The higher the value of i, the lower the
    47. ' displacement, so that the first few iterations have a large influence on the
    48. ' shape of the terrain but later iterations do not, producing only minor
    49. ' variations on the present state of the terrain. This makes the terrain less
    50. ' jagged and more rounded.
    51. '
    52. ' Using this method, it is theoretically possible to zoom in on the map
    53. ' indefinetely to uncover new levels of detail on the same map.
    54. Dim i As Long ' Iteration counter
    55. Dim x As Long ' Coordinate
    56. Dim y As Long ' counters
    57. Dim Step1 As Long
    58. Erase Heights
    59. ' Inistialise the heights in each of the corners
    60. Heights(0, 0) = Rnd() * -10
    61. Heights(0, ROW - 1) = Rnd * -10
    62. Heights(ROW - 1, 0) = Rnd * -10
    63. Heights(ROW - 1, ROW - 1) = Rnd * -10
    64. ' The main iteration loop. See above for explanation.
    65. ' There may be a better algorithm for this, but this is the fasted one
    66. ' that I could come up with.
    67. For i = 0 To ITERATIONS - 1
    68. Step1 = (ROW - 1) / (2 ^ (i + 1))
    69. For y = 0 To ROW - 1 Step Step1
    70. For x = 0 To ROW - 1 Step Step1
    71. If (y / Step1) Mod 2 = 0 Then
    72. If Heights(x, y) = 0 Then
    73. Heights(x, y) = (Heights(x - Step1, y) + Heights(x + Step1, y)) / 2 + (Rnd() * SLOPE - (SLOPE / 2)) * ((ITERATIONS - i) / (i + 1))
    74. End If
    75. Else
    76. If (x / Step1) Mod 2 = 0 Then
    77. Heights(x, y) = (Heights(x, y - Step1) + Heights(x, y + Step1)) / 2 + (Rnd() * SLOPE - (SLOPE / 2)) * ((ITERATIONS - i) / (i + 1))
    78. Else
    79. Heights(x, y) = (Heights(x - Step1, y - Step1) + Heights(x - Step1, y + Step1) + Heights(x + Step1, y - Step1) + Heights(x + Step1, y + Step1)) / 4 + (Rnd() * SLOPE - (SLOPE / 2)) * ((ITERATIONS - i) / (i + 1))
    80. End If
    81. End If
    82. Next
    83. Next
    84. Next
    85. End Sub
    86. Private Sub Draw()
    87. ' Draws the terrain onto the form based on the values in the height map. The
    88. ' displayed colours for the pixels corresponding to values in the height map
    89. ' are based on which two height boundaries the height value is between. The
    90. ' actual colour displayed at the pixel comes from a gradient between the
    91. ' boundary colours, so that the terrain blends together and looks more
    92. ' realistic.
    93. '
    94. ' The height map could also be used to generate 3D terrain.
    95. Dim Color As Long
    96. Dim i As Integer
    97. ' Iterate through all points in the height map
    98. For y = 0 To ROW - 1
    99. For x = 0 To ROW - 1
    100. ' Check the height value against each boundary to see where it lies
    101. For i = 0 To UBound(Bound)
    102. If Heights(x, y) <= Bound(i) Then Exit For
    103. Next
    104. If i = UBound(Bound) + 1 Then
    105. Color = RGB(R(i), G(i), B(i))
    106. ElseIf i = 0 Then
    107. Color = RGB(R(0), G(0), B(0))
    108. Else
    109. ' Calculate the gradient between the two boundary colors
    110. Color = RGB(R(i) + (R(i + 1) - R(i)) * ((Heights(x, y) - Bound(i - 1)) / (Bound(i) - Bound(i - 1))), G(i) + (G(i + 1) - G(i)) * ((Heights(x, y) - Bound(i - 1)) / (Bound(i) - Bound(i - 1))), B(i) + (B(i + 1) - B(i)) * ((Heights(x, y) - Bound(i - 1)) / (Bound(i) - Bound(i - 1))))
    111. End If
    112. SetPixelV(1, 256 \ 2 - ROW \ 2 + x, 256 \ 2 - ROW \ 2 + y, Color)
    113. Next
    114. Next
    115. End Sub
    116. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    117. Generate_Heights()
    118. Draw()
    119. End Sub
    120. End Class



    Was ist an diesen Code falsch? Ich bekomm ein paar "NullReferenceExceptions" und so.
    Das istn ganz böser VB6-Code.
    Verwende statt Rnd die Random-Klasse. Was "Erase" macht, weiß ich gar nicht, aber auf jeden Fall irgend was richtig unschönes (aus .Net-Sicht), da musst du selbst mal schauen, wie du das gegebenenfalls mit Arrays und Listen lösen kannst.