1
Private Function getFoo(ByVal _FileInfo As FileInfo) As foo()
    Dim provider As New ExcelStorage(GetType(foo))
    provider.StartRow = 2
    provider.StartColumn = 1
    provider.FileName = _FileInfo.FullName
    Dim res() As foo = provider.ExtractRecords()
    Return res
End Function

I have the above code in vb.net that i'm trying to convert to C#. I'm using FileHelper library to extract data from Excel. This is my conversion to C#.

public static foo GetFoo(FileInfo fInfo)
{
var provider = new ExcelStorage(typeof(foo));
provider.StartRow = 2;
provider.StartColumn = 1;
provider.FileName = fInfo.FullName;
foo res[] = provider.ExtractRecords();
return res;
}

What am I doing wrong here. I'm getting Bad array declator. Do I have to declare the size the array first?

Thanks

edit: I change the code as suggested. However, I'm getting this error.

"Cannot implicitly convert type 'object[]' to 'foo[]'. An explicit conversion exists (are you missing a cast?)"

I though I already set the type to foo in the ExcelStorage as typeof(foo). Nevermind, I did it with casting.

7 Answers 7

4

Should be: foo[] res = provider.ExtractRecords()

Sign up to request clarification or add additional context in comments.

Comments

2
foo res[] = provider.ExtractRecords(); 

should be

foo[] res = provider.ExtractRecords(); 

Similarly, as you are returning an array of foos, the declaration should be:

public static foo[] GetFoo(FileInfo fInfo) 

Comments

1

In C# the array syntax is attached to the type and not the variable

foo[] res = provider.ExtractRecords();

Comments

1

In C#, you declare array variables like so Type[] VariableName not like this Type VariableName[]

Comments

1

It's foo[] res instead of foo res[].

In VB you can use either the syntax where being an array is a property of the variable:

Dim x() As Integer

or where being an array is a property of the type:

Dim x As Integer()

The former makes more sense in VB 6, where arrays are a special kind of varaibles, and the latter makes more sense in VB.NET where arrays are objects.

In C# being an array is always part of the type:

int[] x;

Comments

0

Plus your method needs to return an array as well

public static foo[] GetFoo(FileInfo fInfo)

Comments

0

Fixed code:

public static foo[] GetFoo(FileInfo fInfo)
{
  var provider = new ExcelStorage(typeof(foo));
  provider.StartRow = 2;
  provider.StartColumn = 1;
  provider.FileName = fInfo.FullName;
  foo[] res = provider.ExtractRecords();
  return res;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.