By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,131 Members | 1,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,131 IT Pros & Developers. It's quick & easy.

Array mixed with integers and strings?

P: n/a

....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
--
Posted via a free Usenet account from http://www.teranews.com

May 18 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 18 mayo, 09:44, "Jeff" <n...@nothingX.comwrote:
...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

--
Posted via a free Usenet account fromhttp://www.teranews.com
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

May 18 '07 #2

P: n/a

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
"Jeff" <no**@nothingX.comwrote in message
news:46***********************@free.teranews.com.. .
>
...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
--
Posted via a free Usenet account from http://www.teranews.com

May 18 '07 #3

P: n/a
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

"Jeff" <no**@nothingX.comwrote in message
news:46***********************@free.teranews.com.. .
>
...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
--
Posted via a free Usenet account from http://www.teranews.com

May 22 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.