Arraylist StringFormat / StringAlignment Problem vb.net vb code

I have an arraylist filled with text that is used to populate a bunch of drawable text.  I include in the arraylist a stringformat collection which is built from a file.

My problem is this, when ever I change stringformat.stringalignment.alignment, VB wants to update the value of alignment in all the text in the arraylist.  All I really want to do is to store the Near, Center and Far inforamtion and use it to format the text when the drawabletext box is created at runtime.  Any Ideas why the arraylist might be updated in the manner.
atlindAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaulHewsCommented:
>I include in the arraylist a stringformat collection which is built from a file.

How do you include it?  Sounds like you have one object that you are updating as you go instead of new objects that you are adding to the collection.  Post your code and we can fix it.
0
atlindAuthor Commented:
Thanks for the response....

It would be difficult to post the code as it spans several files.  But basically I have a text file that contains all the information I need to create a drawable text box.

ex..
ForeColor = Green
BackColor = Blue
Text = Some Text
Font = Arial, 12, Bold
Alignment = Center

I then read this file covert colors from text to Hex Color, Convert Font text to .font and convert Alignment into a StringFormat with the alignment set at what ever the text file has in it.

Now this file contains hundreds of text info blocks so I collect the information from one text block, do any conversion I need and store the converted info into an arraylist, then repeat for each text block in the file.

This issue I am having with the arraylist, is when ever I add a new set of information to the arraylist, when the stringformat inforation is added to the arraylist, all previously stored stringformat information is updated to match the most recently added info.

Example
Arraylist(0).stringformat.alignment = Center
Arraylist(1).stringformat.alignment = Center
Arraylist(2).stringformat.alignment = Far

In this example as soon as I store the info for the "Far" alignment I will get the following...
Arraylist(0).stringformat.alignment = Far
Arraylist(1).stringformat.alignment = Far
Arraylist(2).stringformat.alignment = Far

Likewise if I add Arraylist(3).stringformat.alignment = Near, then I get..
Arraylist(0).stringformat.alignment = Near
Arraylist(1).stringformat.alignment = Near
Arraylist(2).stringformat.alignment = Near
Arraylist(3).stringformat.alignment = Near

Hope this is a little more clear...
I know this may be troublesome to understand without the code, but my bose would not be none to happy If I put it on display...lol
0
PaulHewsCommented:
Right... But the problem is that each element of the arraylist points to the same object. Here's an example that demonstrates how the problem can occur, and shows how to fix it.


Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim List As New ArrayList
        Dim sf As New StringFormat
        For i As Integer = 1 To 5
            Dim TB As New TextBlock
            TB.Format = sf
            TB.Text = String.Format("Text {0}", i)
            TB.Format.Alignment = CType(i Mod 3, StringAlignment) 'Use Mod operator to cycle string alignments.'
            List.Add(TB)
        Next
        Debug.WriteLine("These will all be Far")
        For Each tb As TextBlock In List
            Debug.WriteLine(tb.Format.Alignment)  'Now they're all Far because they all point to one object'
        Next
 
        List.Clear()
 
        For i As Integer = 1 To 5
            Dim TB As New TextBlock
            TB.Format = New StringFormat
            TB.Text = String.Format("Text {0}", i)
            TB.Format.Alignment = CType(i Mod 3, StringAlignment)
            List.Add(TB)
        Next
        Debug.WriteLine("Cycling Center, Far, Near")
        For Each tb As TextBlock In List
            Debug.WriteLine(tb.Format.Alignment)  'Now they will cycle because they don't point to one object'
        Next
 
    End Sub
End Class
 
Public Class TextBlock
    Public Text As String
    Public Format As StringFormat
End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
atlindAuthor Commented:
Ah perfect I knew it had to be simple...lol

Thank you so very much.  You were absolutely correct!!!  Well Done!
0
atlindAuthor Commented:
Perfect! Thank You Very Much!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.