C# Equivalent of this VB String Array Code?

D

David Pendleton

Hello all.

I'm trying to find the best C# equivalent for the following VB.

If myCollection.Count > 0 Then

Dim myStrings() As New String((myCollection.Count - 1)) {}

myCollection.CopyTo(myStrings, 0)

End If

I have been unable to do something like the following because a constant is
required when initializing the array.

string[] myStrings = new string[(myCollection.Count - 1)] {}

Can someone point me in the right direction on this?

Thanks all.
 
G

Guest

Actually, the code you posted does not compile - you can't use "As New" with
arrays.
The correct VB code is:
If myCollection.Count > 0 Then
Dim myStrings() As String = New String((myCollection.Count - 1)) {}
myCollection.CopyTo(myStrings, 0)
End If

And the C# equivalent (via Instant C#) is:
if (myCollection.Count > 0)
{
string[] myStrings = new string[myCollection.Count];
myCollection.CopyTo(myStrings, 0);
}
note that the length for the array is specified in C#, not the upper bound
as in VB.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
 
D

David Pendleton

Yes, you're correct. I was switching too much between dialects. I'm trying
to convert a VB application to C# side-by-side and it's easy to type weird
stuff (especially outside of VS).

As it turns out, it was the {} in my translation that was giving me problems
(not to mention the upper bound). I thought for sure I had to initialize the
array also, like:

string[] myStrings = new string[(myCollection.Count - 1)] {};

Which does a number of things wrong. Thanks for the solution.

Thanks again.

David Anton said:
Actually, the code you posted does not compile - you can't use "As New"
with
arrays.
The correct VB code is:
If myCollection.Count > 0 Then
Dim myStrings() As String = New String((myCollection.Count - 1)) {}
myCollection.CopyTo(myStrings, 0)
End If

And the C# equivalent (via Instant C#) is:
if (myCollection.Count > 0)
{
string[] myStrings = new string[myCollection.Count];
myCollection.CopyTo(myStrings, 0);
}
note that the length for the array is specified in C#, not the upper bound
as in VB.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter


David Pendleton said:
Hello all.

I'm trying to find the best C# equivalent for the following VB.

If myCollection.Count > 0 Then

Dim myStrings() As New String((myCollection.Count - 1)) {}

myCollection.CopyTo(myStrings, 0)

End If

I have been unable to do something like the following because a constant
is
required when initializing the array.

string[] myStrings = new string[(myCollection.Count - 1)] {}

Can someone point me in the right direction on this?

Thanks all.
 
G

Guest

I'm just currious if there is a reason not to use:
myStrings = myCollection.ToArrray();
 

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