3

This is my VB code:

If TxtStr(i) = "#" And TxtStr(i + 1) = "]" Then
    RefStr = RefStr & "]"
    ReDim Preserve RefStrLinks(1, RefStrLinkIndex)
    RefStrLinks(0, RefStrLinkIndex) = RefStr
    RefStr = RefStr.Replace("[#", String.Empty)
    RefStr = RefStr.Replace("#]", String.Empty)
    RefStrLinks(1, RefStrLinkIndex) = RefStr
    RefStrLinkIndex = RefStrLinkIndex + 1
    RefStr = String.Empty
    RefStrFound = False
End If

This is my converted code in C#; RefStrLinks is declared as:

string[,] RefStrLinks = null;

But this gives a compile error because of ReDim Preserve whenever I run this:

if (TxtStr[i].ToString() == "#" & TxtStr[i + 1].ToString() == "]")
{
    RefStr = RefStr + "]";
    Array.Resize<string>(ref RefStrLinks, RefStrLinkIndex + 1);
    RefStrLinks[0, RefStrLinkIndex] = RefStr;
    RefStr = RefStr.Replace("[#", string.Empty);
    RefStr = RefStr.Replace("#]", string.Empty);
    RefStrLinks(1, RefStrLinkIndex) = RefStr;
    RefStrLinkIndex = RefStrLinkIndex + 1;
    RefStr = string.Empty;
    RefStrFound = false;
}

Does anybody understand why?

Bugs
  • 4,491
  • 9
  • 32
  • 41
  • 1
    Please read: [ask] and take the [tour] we are not a conversion website – Mederic Jun 07 '17 at 09:13
  • *What* is the exact error you get and *where* do you get it? Anyway you should consider to use a `List` instead of an `Array` to be able to add and remove elements from your collection as needed. – MakePeaceGreatAgain Jun 07 '17 at 09:14
  • 1
    why would your C# code give 'compile error because of "ReDim Preserve"', when your C# *doesn't include* 'ReDim Preserve"? I'm confused... but I don't think that's the actual error that you're seeing... – Marc Gravell Jun 07 '17 at 09:14
  • 3
    ReDim Preserve is super inefficient to use on adding an item. Try another structure like List. – Szer Jun 07 '17 at 09:15
  • Can you show where `RefStrLinks` is declared? I have a feeling that it isn't a vector... – Marc Gravell Jun 07 '17 at 09:18
  • @MarcGravell I am unable to covert "ReDim Preserve" keyword in C#. This line of code "Array.Resize(ref RefStrLinks, RefStrLinkIndex + 1);" giving error, please help – Praveen Kumar Jun 07 '17 at 09:21
  • @PraveenKumar bingo... I called it :) yeah, see my answer as to why that isn't going to work – Marc Gravell Jun 07 '17 at 09:24
  • thanks to @MarcGravell and all other giving their valuable time to help me . I am understanding answer given by Marc Gravel – Praveen Kumar Jun 07 '17 at 09:30

1 Answers1

5

Right; I think the real issue here is that you have a 2-dimensional array; RefStrLinks is not a string[], but rather is a string[,], with dimension 2 on the first axis. Array.Resize only works with vectors (a "vector" is a one-dimensional array with base index 0, i.e. string[]).

Frankly, I would replace all of this (re-dimming an array or using Array.Resize per element is absurdly expensive) with something like:

List<SomeBasicType> list = ...
...
// where "foo" and "bar" are the two values that you intend to store per item
var item = new SomeBasicType(foo, bar);
list.Add(item);

where perhaps SomeBasicType is an immutable struct that takes two strings. Or more simply, in C# "current": value-type tuples:

// declare the list (change the names to something meaningful for your code)
var list = new List<(string name, string url)>();

// ... add values efficiently

string name = "whatever"; // your per-item code goes here
string url = "some value"; // and here
list.Add((name, url));

// ... show that we have the data

foreach(var item in list)
{
    Console.WriteLine($"{item.name} / {item.url}");
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900