Click here to Skip to main content
14,865,273 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()
   
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]
tbarrus 10-Feb-14 20:26pm
   
Thanks for your suggestion. I already have a solution using a loop which I was trying to avoid because of performance reasons, but I appreciate you confirming that it is not possible without doing the loop.
tbarrus 10-Feb-14 20:26pm
   
Thanks for your suggestion. I already have a solution using a loop which I was trying to avoid because of performance reasons, but I appreciate you confirming that it is not possible without doing the loop.
Sergey Alexandrovich Kryukov 10-Feb-14 19:17pm
   
Not true! Not even close. No, it does not require array of object. You need to understand the difference between run-time and compile-time types and such thing as covariance and contravariance...
—SA
tbarrus 10-Feb-14 19:22pm
   
If I dimension mcArr As Double mc.CopyTo(mcArr,0) there is an error. I guess you don't understand Collections in VB.NET? How do you explain this?
Sergey Alexandrovich Kryukov 10-Feb-14 19:43pm
   
I don't?... He-he... You did not make any statement I need to "explain".
You have "errors" (always provide error information) because you've written quite apparent gibberish...
Have a great day...
—SA
Matt T Heffron 10-Feb-14 21:35pm
   
Considering that you need to conversion to Double at the same time as you are extracting from the MatchCollection there's NO performance gain using mc.CopyTo().
The reason that the mc.CopyTo(mcArr,0) gave an error when mcArr was dimensioned as Double is because there's no implicit conversion from Match to a Double! The Match has no "knowledge" that the sequence of characters it "matched" could be interpreted as a Double; and a Double can't try to convert from every possible type being assigned to it.
tbarrus 10-Feb-14 21:55pm
   
That makes sense. I was planning on using a For loop and copy each of the elements of the match collection. I agree with your soap box statement above.
Matt T Heffron 10-Feb-14 22:20pm
   
Don't just "copy" the MatchCollection elements, convert them to Double in the same For loop!
(Use Double.Parse() like above, or Double.TryParse())

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