[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Arraylist StringFormat / StringAlignment Problem vb.net vb code

Posted on 2008-11-17
5
Medium Priority
?
873 Views
Last Modified: 2013-11-26
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.
0
Comment
Question by:atlind
  • 3
  • 2
5 Comments
 
LVL 38

Expert Comment

by:PaulHews
ID: 22986531
>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
 

Author Comment

by:atlind
ID: 22989833
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
 
LVL 38

Accepted Solution

by:
PaulHews earned 500 total points
ID: 22998950
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
 

Author Comment

by:atlind
ID: 23000516
Ah perfect I knew it had to be simple...lol

Thank you so very much.  You were absolutely correct!!!  Well Done!
0
 

Author Closing Comment

by:atlind
ID: 31517723
Perfect! Thank You Very Much!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month19 days, 15 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question