Click here to Skip to main content
15,881,833 members
Please Sign up or sign in to vote.
1.67/5 (2 votes)
I have a string that I want to pick off values using a regular expression. I need to transfer the resulting match collection to an array and change the data type to Double. The challenge I am having is converting an Object Array to a Double Array. Is this possible? If it is, how would I go about doing it?
Imports System.Text.RegularExpressions

Dim ValStr As String
ValStr = "0=1,1=4,2=3,3=2,4=7"
Dim pattern As String = "([0-9]+)(?=\,)|([0-9]+$)"
Dim re As New Regex(pattern)
Dim mc As MatchCollection
Dim mcArr() As Object
Dim ValArr() As Double

mc = re.Matches(ValStr)
ReDim mcArr(mc.Count-1)
mc.CopyTo(mcArr,0)

Is there code I can put here to convert the elements of mcArr to Double and store as a new Double Array?
Posted
Updated 10-Feb-14 12:14pm
v3
Comments
Sergey Alexandrovich Kryukov 10-Feb-14 17:41pm    
You can, but the problem is deeper. Somewhere, you ended up with an untyped array with actual runtime types of the elements being double. It means that you failed to design code and are violating OOP. This situation is unacceptable and you should redesign you code to avoid it. Yes, no "ifs" no "buts".
—SA
Matt T Heffron 10-Feb-14 17:50pm    
I don't see "an untyped array with actual runtime types of the elements being double"
From context, it appears the ReDim Arr(mc.Count-1) has a typo "Arr" for "mcArr" and, yes, it should have been of type Match
ValArr is an array of Double
Sergey Alexandrovich Kryukov 10-Feb-14 19:14pm    
Oh, maybe this is a kind of jargon: if you have an array of System.Object, you can call it "untyped", just because there is nothing "more untyped" than that in CLI, as all objects are System.Object or derived...
—SA

1 solution

I think something like this (my VB is weak compared with my c#):
VB
mc = re.Matches(ValStr)
ReDim mcArr(mc.Count-1)
mc.CopyTo(mcArr,0)
ValArr = mcArr.Select(Function(m) Double.Parse(m.ToString())).ToArray()
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 10-Feb-14 17:43pm    
You see, it should work and... it would be the coding abuse, continuing the abuse started by OP. Please see my comment to the question.
—SA
Matt T Heffron 10-Feb-14 17:46pm    
Well, for me, VB already is coding abuse... ;)
Sergey Alexandrovich Kryukov 10-Feb-14 19:12pm    
That's right. And this is actually my point: would be better not to support this abuse...
—SA
tbarrus 10-Feb-14 18:12pm    
The CopyTo command requires that the resulting Array be type Object. I would dimension Double if I could. I need to convert the Array type from Double to do some calculations. Do you have any suggestions on how to convert it using VB.NET?
Matt T Heffron 10-Feb-14 18:23pm    
What I've done is convert the values from the mcArr from Match to Double and create a NEW array of type Double() assigned to ValArr.
The final three lines above could, and probably should, be replaced with a straightforward loop.
(Yes, sometimes you just need to make the loop explicit ;-) )

[Soapbox]
Not withstanding that, there's really no excuse for the more recent .NET framework versions to still fail to also implement IEnumerable(Of T) with the specialized collection classes like MatchCollection, which would have made them directly usable by Linq-to-Object.
[/Soapbox]

[Edit]I forgot about Enumerable.Cast<T>() which solves the Soapbox issue!!
[/Edit]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900