-
Sep 21st, 2015, 11:24 AM
#1
Thread Starter
Hyperactive Member
[Resolved] [vb2010] Convert a string of numbers directly in a integer array
Is possible to convert a string of numbers DIRECTLY into a READONLY array of integer?
Something like:
Str = "11,22,43,44,51,67,78,28,69,............................."
Readonly StrArr() as Integer = Str.split..... ???
(obviously ReadOnly StrArr() As Integer = Str.Split((","c)) gets an error)
Last edited by phil2000; Sep 21st, 2015 at 02:48 PM.
-
Sep 21st, 2015, 11:36 AM
#2
Re: [vb2010] Convert a string of numbers directly in a integer array
Array.ConvertAll?...
Code:
Dim s = "11,22,43,44,51,67,78,28,69"
Dim i() = Array.ConvertAll(s.Split(","c), Function(n) CInt(n))
EDIT oh missed the ReadOnly part.
Last edited by Edgemeal; Sep 21st, 2015 at 11:55 AM.
-
Sep 21st, 2015, 01:43 PM
#3
Re: [vb2010] Convert a string of numbers directly in a integer array
Sort of, this is quite an odd request.
ReadOnly variables can only be assigned at declaration or in the constructor. So if you wanted to do this, the string of numbers also has to be ReadOnly. But in the context of an array, it doesn't really make the array impossible to modify, it just means you can't replace it with another. I'm not sure ReadOnly is relevant to your problem. Let's ignore it for now.
String.Split() will get you the individual tokens of the array, but those will still be Strings. Edgemeal's solution works, there's other ways with, say, Select() but they're functionally identical.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Sep 21st, 2015, 02:14 PM
#4
Re: [vb2010] Convert a string of numbers directly in a integer array
You can use a ReadOnlyCollection(Of Integer). It's more like a List than an Array, but still usable in mostly the same way:
Code:
Imports System.Collections.ObjectModel
Public Class Form1
Dim myIntegers As ReadOnlyCollection(Of Integer)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim s = "11,22,43,44,51,67,78,28,69"
Dim i() = Array.ConvertAll(s.Split(","c), Function(n) CInt(n))
myIntegers = New ReadOnlyCollection(Of Integer)(i)
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 21st, 2015, 02:46 PM
#5
Thread Starter
Hyperactive Member
Re: [vb2010] Convert a string of numbers directly in a integer array
Originally Posted by Edgemeal
Array.ConvertAll?...
Code:
Dim s = "11,22,43,44,51,67,78,28,69"
Dim i() = Array.ConvertAll(s.Split(","c), Function(n) CInt(n))
EDIT oh missed the ReadOnly part.
Code:
Dim s = "11,22,43,44,51,67,78,28,69"
Public Readonly i() = Array.ConvertAll(s.Split(","c), Function(n) CInt(n))
works fast and perfectly.. Ty.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|