VB>C# Translation Help

C

Craig

I'm trying to translate the CompareFileInfoEntries class from this page:
http://aspnet.4guysfromrolla.com/articles/060403-1.2.aspx

I have to just be missing punctuation or something dumb somewhere, but it
won't work for me. Here's what I have:

public enum CompareByOptions {FileName, LastWriteTime, Length};

public class CompareFileInfoEntries : IComparer

{

private CompareByOptions compareBy = CompareByOptions.FileName;

public void New(ref CompareByOptions cBy)

{

compareBy = cBy;

}

public virtual int Compare(object file1, object file2)

{

//Convert file1 and file2 to FileInfo entries

FileInfo f1 = (FileInfo) file1;

FileInfo f2 = (FileInfo) file2;

//Compare the file names

switch(compareBy)

{

case CompareByOptions.FileName:

return(String.Compare(f1.Name, f2.Name));

case CompareByOptions.LastWriteTime:

return(DateTime.Compare(f1.LastWriteTime, f2.LastWriteTime));

case CompareByOptions.Length:

int lengthDif = Convert.ToInt32(f1.Length - f2.Length);

return(lengthDif);

default:

return(0);

}

}

}



When I try to call it like this:

CompareByOptions compareMethod = CompareByOptions.FileName;

Array.Sort(files,0,files.Length, new CompareFileInfoEntries(compareMethod));

I get this error:

gallery.aspx.cs(190): No overload for method 'CompareFileInfoEntries' takes
'1' arguments


What am I missing? Can someone please help? Thanks in advance...

Craig
 
P

Paul Hetherington

Array.Sort(files,0,files.Length, new
CompareFileInfoEntries(compareMethod));

I made the same mistake. When passing a parameter to a method that is
declared with 'ref' you have to specify it when calling as well
Try this.
Array.Sort(files,0,files.Length,new CompareFileInfoEntries(ref
compareMethod));

Hope this helps
-Paul
 
C

Craig

Nevermind... There's a significant difference in syntax I didn't know
about - this helped me:
http://www.ellkay.com/ConvertVB2CSharp.htm

Here's the change I had to make:
//public void New(ref CompareByOptions cBy) //old line here, I made
incorrectly

public CompareFileInfoEntries(CompareByOptions cBy) //new line here that
converter made

{

compareBy = cBy;

}
 
M

Marco Martin

I beleive your error is because in C# the constructor is not called New as
in VB, it is called by the same name as the Class; and returns nothing

try this syntax:

public class MyClass
{
public MyClass(TypeOfVariable variable)
{
localVariableName = variable;
}
}

That should probably point you in the right direction
 

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

Similar Threads


Top