stirng from an array

  • Thread starter Thread starter Keith G Hicks
  • Start date Start date
K

Keith G Hicks

I'm not great at vb.net and I don't particularly like arrays so I use them
as little as possible. I tend to think in terms of VBA or even old clunky
xBase ideas when I do things like this and I know there are some great
features in vb.net that make this sort of thing easier than I'm probably
making it. I'm kind of stumblign around on this issue. If there's a better
way to handle this then feel free to let me know.

I have a 2 dimensional array. I'm storing ID #'s of database rows in one
column and the # of times an insert is attempted in another column. I need
to get the first column into a column delimited string. How can I do that?

Dim aNoticeIDsThatAreBad(1, 0) as Int32
aNoticeIDsThatAreBad(0, 0) = 123456
aNoticeIDsThatAreBad(1, 0) = 1
ReDim Preserve aNoticeIDsThatAreBad(1, 1)
aNoticeIDsThatAreBad(0, 1) = 123457
aNoticeIDsThatAreBad(1, 1) = 2
ReDim Preserve aNoticeIDsThatAreBad(1, 2)
aNoticeIDsThatAreBad(0, 2) = 123458
aNoticeIDsThatAreBad(1, 2) = 1

I need to get a string like this "123456,123457,123458".

Is there a function that can do this or do I have to loop through the array
and build it?

In addition to the above, I need to do a few other things with this array.

1. I need to look up NoticeID's in the array and see if they are in there
and if so, what is the related insertion attempt #.
2. If the insertion attempt fails, then I need to increment the # of
insertion attempts.
3. I also need to remove items from the array if an attempt made on that
item succeeds.

Any kind help would be greatly appreciated.

Thanks,

Keith
 
Keith G Hicks said:
I'm not great at vb.net and I don't particularly like arrays so I use them
as little as possible. I tend to think in terms of VBA or even old clunky
xBase ideas when I do things like this and I know there are some great
features in vb.net that make this sort of thing easier than I'm probably
making it. I'm kind of stumblign around on this issue. If there's a better
way to handle this then feel free to let me know.

I have a 2 dimensional array. I'm storing ID #'s of database rows in one
column and the # of times an insert is attempted in another column. I need
to get the first column into a column delimited string. How can I do that?

Dim aNoticeIDsThatAreBad(1, 0) as Int32
aNoticeIDsThatAreBad(0, 0) = 123456
aNoticeIDsThatAreBad(1, 0) = 1
ReDim Preserve aNoticeIDsThatAreBad(1, 1)
aNoticeIDsThatAreBad(0, 1) = 123457
aNoticeIDsThatAreBad(1, 1) = 2
ReDim Preserve aNoticeIDsThatAreBad(1, 2)
aNoticeIDsThatAreBad(0, 2) = 123458
aNoticeIDsThatAreBad(1, 2) = 1

I need to get a string like this "123456,123457,123458".

Is there a function that can do this or do I have to loop through the
array and build it?

In addition to the above, I need to do a few other things with this array.

1. I need to look up NoticeID's in the array and see if they are in there
and if so, what is the related insertion attempt #.
2. If the insertion attempt fails, then I need to increment the # of
insertion attempts.
3. I also need to remove items from the array if an attempt made on that
item succeeds.

Any kind help would be greatly appreciated.

Thanks,

Keith

For your first question, you can simply use the ToString() method defined in
each datatype class to convert and format the value in a variable.

http://msdn.microsoft.com/en-us/library/system.int32.tostring.aspx
 
Hello, Keith,

Take a look at the Dictionary class. I think that it will serve your needs
much better than an array. You could use your NoticeID as the key, and the
attempt # as the value. This will make look-ups trivial, and unlike arrays,
A dictionary is a collection so is designed for insertions and deletions.

Sorry, but I don't know of any other way than iterating through the keys to
create your comma delimited string.

Cheers,
Randy
 
I'm not great at vb.net and I don't particularly like arrays so I use
them as little as possible. I tend to think in terms of VBA or even
old clunky xBase ideas when I do things like this and I know there are
some great features in vb.net that make this sort of thing easier than
I'm probably making it. I'm kind of stumblign around on this issue. If
there's a better way to handle this then feel free to let me know.

You just reminded me how much I dislike VB syntax. LOL

First, I would not use the array method. It is a bit unwieldy. You can
accomplish the solution with a generic with few lines of code.

First, create your own generic dictionary. This is so you can create an
overriden ToString()

------------------------------------------------------------
Imports System.Collections.Generic
Imports System.Text

Public Class MyCustomDictionary(Of T, I)
Inherits Dictionary(Of T, I)

End Class
------------------------------------------------------------

Next, override ToString so it gives the output you want (this can also
be a custom method if you need ToString().

------------------------------------------------------------
Public Overrides Function ToString() As String

Dim builder As New StringBuilder()

Dim isFirstItem As Boolean = True

Dim key As T

For Each key In MyBase.Keys
If Not isFirstItem Then
builder.Append(","c)
End If
builder.Append(key)
isFirstItem = False
Next

Return builder.ToString
End Function
------------------------------------------------------------

And here is the usage:

------------------------------------------------------------
Module Module1

Sub Main()
Dim dictionary As New MyCustomDictionary(Of Integer, Integer)
dictionary.Add(123456, 1)
dictionary.Add(123457, 2)
dictionary.Add(123458, 1)
Console.WriteLine(dictionary.ToString)
Console.Read()
End Sub

End Module
------------------------------------------------------------

This works as long as the first item will always be unique, as in your
example.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
I worked out the code to use an array. It's working fine. I had some trouble
doing it wiht a 2D array so I used a 1D array instead. I got all that done
before your replies but thanks for the info. Next time I need something
similar I'll check into the dictionary idea.

Keith
 
Keith said:
I'm not great at vb.net and I don't particularly like arrays so I use them
as little as possible. I tend to think in terms of VBA or even old clunky
xBase ideas when I do things like this and I know there are some great
features in vb.net that make this sort of thing easier than I'm probably
making it. I'm kind of stumblign around on this issue. If there's a better
way to handle this then feel free to let me know.

I have a 2 dimensional array. I'm storing ID #'s of database rows in one
column and the # of times an insert is attempted in another column. I need
to get the first column into a column delimited string. How can I do that?

Dim aNoticeIDsThatAreBad(1, 0) as Int32
aNoticeIDsThatAreBad(0, 0) = 123456
aNoticeIDsThatAreBad(1, 0) = 1
ReDim Preserve aNoticeIDsThatAreBad(1, 1)
aNoticeIDsThatAreBad(0, 1) = 123457
aNoticeIDsThatAreBad(1, 1) = 2
ReDim Preserve aNoticeIDsThatAreBad(1, 2)
aNoticeIDsThatAreBad(0, 2) = 123458
aNoticeIDsThatAreBad(1, 2) = 1

I need to get a string like this "123456,123457,123458".

Is there a function that can do this or do I have to loop through the array
and build it?

There's a one-liner for everything. :)

Dim ids As String = String.Join(",", _
IEnumerable.Range(0, aNoticeIDsThatAreBad.GetLength(1)) _
.Select(Function(n) aNoticeIDsThatAreBad(0, n).ToString()) _
.ToArray() _
)
In addition to the above, I need to do a few other things with this array.

1. I need to look up NoticeID's in the array and see if they are in there
and if so, what is the related insertion attempt #.
2. If the insertion attempt fails, then I need to increment the # of
insertion attempts.
3. I also need to remove items from the array if an attempt made on that
item succeeds.

Using a Dictionary(Of Integer, Integer) would simplify all these.
 
Back
Top