Array mixed with integers and strings?

J

Jeff

....still new to vb.net 2005

I understand the concept of arrays, and have used them in other languages,
but was hoping that someone could get me started with something.

I have a fairly long list of values that start in textboxes and listboxes
that are both integers and strings. I need to store them in an array to pass
to subs and stored procedures used in a database. Can I mix the integers and
strings (both of varying lengths) in the same array? If so, could someone
provide a simple example that I could build upon or point me in the right
direction?

Thanks

Jeff
 
D

diAb0Lo

...still new to vb.net 2005

I understand the concept of arrays, and have used them in other languages,
but was hoping that someone could get me started with something.

I have a fairly long list of values that start in textboxes and listboxes
that are both integers and strings. I need to store them in an array to pass
to subs and stored procedures used in a database. Can I mix the integers and
strings (both of varying lengths) in the same array? If so, could someone
provide a simple example that I could build upon or point me in the right
direction?

Thanks

Jeff

Hi there,

You can define the array as String, Store everything as String and do
the convert to Int for all the items in the array. If you get an error
trying to convert, it means that is a String. For example:

Dim myArray(50) as String
Dim tmpConv as Integer

On Error Goto errStrConv
for i = 1 to 50
tmpConv = CInt(myArray(i))
' if passyu know that is a integer
Exit Sub
errIntConv:
' here you know is a String
next

Or

Dim myArray(50) as String
Dim tmpConv as Integer

for i = 1 to 50
Try
tmpConv = CInt(myArray(i))
' if passyu know that is a integer
Catch ex As Exception
' here you know is a String
End Try
next

Cheers
 
R

Robin Tucker

Hi Jeff,


You could use a list of objects to do this. On a design level, if the two
sets of values are distinct (i.e. your integers refer to one thing, your
strings to another) then I would separate them out into two lists. I
wouldn't be too comfortable lugging lists of Object around an application.
It's got unhandled exception written all over it ;). Although this was the
way with .NET 1.1, generics make it pointless. I suppose if you are
disciplined you can get away with it. Alternatively you could store them
all as string, parsing out the values into integers if needed. In the case
of object:

Dim myList As New List(Of Object)

myList.Add ( 1 )
myList.Add ( "One" )

For Each theObject As Object In myList

If TypeOf (theObject) Is Integer Then

' It's an integer

Dim theInteger = DirectCast(theObject, Integer)

ElseIf TypeOf (theObject) Is String Then

' It's a string

Dim theString As String = DirectCast(theObject, String)

Else

Throw New InvalidOperationException ( "Object type not supported" )

End If

Next




and for strings:



Dim myList As New List(Of String)

myList.Add ( 1.ToString )
myList.Add ( "one" )

For Each theString As String In myList

Dim theInteger As Integer

If Integer.TryParse ( theString, theInteger ) Then
' It's an integer string
Else
' It's a string
End

Robin
 
M

Miro

I tried this too jeff when i first started in vb.net

something like
Array := { "Hello", 2 }

It cant be done easily...

so the solution I used, is I created a Class and the created 2 properties of
it.
1 a string and the other a numeric
Then I arrayified the Class...and worked like a charm.

Another solution I think will work is to create a variable that is a
datagrid, and save the data in there like data lines.

Miro
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top