Click here to Skip to main content
15,897,518 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Please fix my code! Pin
Richard Deeming18-Sep-18 3:38
mveRichard Deeming18-Sep-18 3:38 
GeneralRe: Please fix my code! Pin
Eddy Vluggen18-Sep-18 5:05
professionalEddy Vluggen18-Sep-18 5:05 
QuestionOpen / Display SVG images within Visual Basic 6 Pin
davesmithmiap13-Sep-18 1:19
davesmithmiap13-Sep-18 1:19 
AnswerRe: Open / Display SVG images within Visual Basic 6 Pin
Richard Deeming13-Sep-18 2:33
mveRichard Deeming13-Sep-18 2:33 
AnswerRe: Open / Display SVG images within Visual Basic 6 Pin
Member 1396504228-Sep-18 3:15
Member 1396504228-Sep-18 3:15 
RantRe: Open / Display SVG images within Visual Basic 6 Pin
Richard Deeming28-Sep-18 4:38
mveRichard Deeming28-Sep-18 4:38 
GeneralRe: Open / Display SVG images within Visual Basic 6 Pin
Darrell Hagan 202113-Jun-21 12:53
Darrell Hagan 202113-Jun-21 12:53 
QuestionCould someone explain why For Each and For Next loops behave differently with lists? Pin
MattiasW768-Sep-18 14:13
MattiasW768-Sep-18 14:13 
Hi everyone.
I think this might be my first post here. I rarely post questions, normally I like to search for my answers but this I thought I try to ask here.

Anyway, I noticed something that I found strange while using lists and For loops.

I'm pasting an example code here:
What it does is trying to scale rectangles saved within a list using loops.
VB
Sub TestForLoop()
    ' Declare a list of rectangles and add 2 rectangles to it
    Dim lstRectangles As New List(Of Rectangle)
    lstRectangles.Add(New Rectangle(100, 200, 1000, 2000))
    lstRectangles.Add(New Rectangle(500, 700, 1500, 2000))

    ' This is the number used to scale the rectangle
    Dim gScale As Single = 0.1

    ' Before doing changes, write the rectangles dimensions to debug window.
    ShowRectDimensions("Before scaling:               ", lstRectangles)
    Debug.WriteLine("")

    ' Jump thru the elements, update and scale each rectangles dimensions. 
    For Each rect As Rectangle In lstRectangles
        With rect
            rect = New Rectangle(CInt(.Left * gScale), CInt(.Top * gScale),
                                 CInt(.Width * gScale), CInt(.Height * gScale))
            ' Write the changes to debug window while looping thru the list.
            Debug.WriteLine("Result inside For Each Loop:  {0}x{1},{2}x{3}", .Left, .Top, .Width, .Height)
        End With
    Next

    ' After the loop, write the rectangles dimensions to debug window once again.
    ShowRectDimensions("Result outside For Each Loop: ", lstRectangles)
    Debug.WriteLine("")


    ' Go thru the list, update and scale each rectangles dimension. 
    For i As Integer = 0 To lstRectangles.Count - 1
        With lstRectangles(i)
            lstRectangles(i) = New Rectangle(CInt(.Left * gScale), CInt(.Top * gScale),
                                             CInt(.Width * gScale), CInt(.Height * gScale))
            ' Write the changes to debug window while looping thru the list.
            Debug.WriteLine("Result inside For Next Loop:  {0}x{1},{2}x{3}", .Left, .Top, .Width, .Height)
        End With
    Next

    ' After the loop, write the rectangles dimensions to debug window once again.
    ShowRectDimensions("Result outside For Next Loop: ", lstRectangles)
    Debug.WriteLine("")
End Sub

' Created this sub to save some repetition. It writes the dimensions of the rectangles in a list.
Private Sub ShowRectDimensions(sText As String, lstRectangles As List(Of Rectangle))
    For Each rect As Rectangle In lstRectangles
        With rect
            Debug.WriteLine("{0}{1}x{2},{3}x{4}", sText, CStr(.Left), CStr(.Top), CStr(.Width), CStr(.Height))
        End With
    Next
End Sub


This produces this result in the debug window:
Before scaling:               100x200,1000x2000
Before scaling:               500x700,1500x2000

Result inside For Each Loop:  10x20,100x200
Result inside For Each Loop:  50x70,150x200
Result outside For Each Loop: 100x200,1000x2000
Result outside For Each Loop: 500x700,1500x2000

Result inside For Next Loop:  100x200,1000x2000
Result inside For Next Loop:  500x700,1500x2000
Result outside For Next Loop: 10x20,100x200
Result outside For Next Loop: 50x70,150x200


As you can see, while being on the inside of a for each loop the changes to the elements is readable and reusable. But when getting on the outside it is clear that none of the changes got saved back to the list.

However, when using the traditional For Next loop, no changes can be seen to the list until you get to the outside. This is the opposite behaviour. Confused | :confused:

This just feels weird and forces me to copy the value when I want to do more advanced calculations and the write it back to the item after. And as far as I can see the Inumerable loop seems worthless when wanting to write data back..

Am I doing something wrong or is there a logic to this?

[Edited a typo..]

modified 9-Sep-18 7:09am.

AnswerRe: Could someone explain why For Each and For Next loops behave differently with lists? Pin
Eddy Vluggen8-Sep-18 23:51
professionalEddy Vluggen8-Sep-18 23:51 
QuestionRe: Could someone explain why For Each and For Next loops behave differently with lists? Pin
MattiasW769-Sep-18 1:04
MattiasW769-Sep-18 1:04 
AnswerRe: Could someone explain why For Each and For Next loops behave differently with lists? Pin
Eddy Vluggen9-Sep-18 2:11
professionalEddy Vluggen9-Sep-18 2:11 
GeneralRe: Could someone explain why For Each and For Next loops behave differently with lists? Pin
MattiasW7620-Sep-18 9:09
MattiasW7620-Sep-18 9:09 
AnswerRe: Could someone explain why For Each and For Next loops behave differently with lists? Pin
Dave Kreskowiak9-Sep-18 4:54
mveDave Kreskowiak9-Sep-18 4:54 
GeneralRe: Could someone explain why For Each and For Next loops behave differently with lists? Pin
MattiasW7627-Sep-18 2:59
MattiasW7627-Sep-18 2:59 
QuestionAdding Custom TabPage to TabControl: Closed Pin
mo14927-Sep-18 11:12
mo14927-Sep-18 11:12 
QuestionHow to declare Crypto? Pin
Member 139535037-Sep-18 6:04
Member 139535037-Sep-18 6:04 
AnswerRe: How to declare Crypto? Pin
Richard Deeming7-Sep-18 7:46
mveRichard Deeming7-Sep-18 7:46 
GeneralRe: How to declare Crypto? Pin
Member 1395350314-Sep-18 3:19
Member 1395350314-Sep-18 3:19 
QuestionCurrency textbox Pin
Member 139744835-Sep-18 16:08
Member 139744835-Sep-18 16:08 
AnswerRe: Currency textbox Pin
Mycroft Holmes5-Sep-18 18:34
professionalMycroft Holmes5-Sep-18 18:34 
AnswerRe: Currency textbox Pin
dan!sh 5-Sep-18 19:56
professional dan!sh 5-Sep-18 19:56 
Questionwindows version in vb.net Pin
JR2123-Sep-18 0:20
JR2123-Sep-18 0:20 
AnswerRe: windows version in vb.net Pin
Eddy Vluggen3-Sep-18 1:45
professionalEddy Vluggen3-Sep-18 1:45 
AnswerRe: windows version in vb.net Pin
Dave Kreskowiak3-Sep-18 13:31
mveDave Kreskowiak3-Sep-18 13:31 
QuestionHow to multi Highlight text different 2 textbox in datagridview VB.net? Pin
CodieCalm2-Sep-18 22:26
CodieCalm2-Sep-18 22:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.