views:

93

answers:

3

Right now I am porting a VB6 project over to C# and keep getting this error. Can't seem to fix it in the namespace.

Problem:

//Expected class, delegate, enum, interface, or struct
public string GetHostByAddress(long addr)
{
    dynamic phe = null;
    dynamic Ret = null;
    HOSTENT heDestHost = default(HOSTENT);
    dynamic hostname = null;

    phe = gethostbyaddr(addr, 4, PF_INET);
    if (phe) {
        MemCopy(heDestHost, phe, hostent_size);
        hostname == new String[256, 0];
        MemCopy(hostname, heDestHost.h_name, 256);
        GetHostByAddress == Strings.Left(hostname, Strings.InStr(hostname, Strings.Chr(0)) - 1);
    } else {
        GetHostByAddress = WSA_NoName;
    }
}

Original Method looks like this.

Public Function GetHostByAddress(ByVal addr As Long) As String

  Dim phe&, Ret&
  Dim heDestHost As HOSTENT
  Dim hostname&

    phe = gethostbyaddr(addr, 4, PF_INET)
    If phe Then
        MemCopy heDestHost, ByVal phe, hostent_size
        hostname = String$(256, 0)
        MemCopy ByVal hostname, ByVal heDestHost.h_name, 256
        GetHostByAddress = Left$(hostname, InStr(hostname, Chr$(0)) - 1)
      Else
        GetHostByAddress = WSA_NoName
    End If

End Function

Failing to understand why its not working and getting frustrated. Any Suggestions?

+2  A: 

It looks like you're trying to define a method outside the scope of any class.

In C#, as well as in VB.NET, all methods must belong to a class. Enclose your code in some arbitrary helper class to make the posted compiler error go away:

public static class MyHelperClass
{
    // put your method definition here
}

You may still have some errors after this, though, from the look of it. For example I don't think this line is doing what you think:

hostname == String[256, 0];

Perhaps you mean for it to do something like this?

hostname = new string[256, 0]; // Note: one '=' symbol, 'new' keyword
                               // (Is allocation of a multidimensional array
                               // of strings what you want?)

These are very superficial observations based purely on the details of your code alone. For much more useful comments on the more fundamental issue of what you're trying to achieve here, I urge you to treat Xiaofu's and Christopher Painter's answers as more valuable than mine.

Dan Tao
`String$(256, 0)` is roughly equivalent to `new String('\0', 256)` (in that it allocates a string containing a specific character repeated a certain number of times). However, one doesn't allocate fixed-size strings in .NET, then `MemCopy` over them to initialise their contents.
Bradley Grainger
@Dan, Thanks! That was exactly it. There was a few missing "{}"s now to go figure out the "With" function.
Nightforce2
+3  A: 

All of what Dan Tao said if you want to start cleaning up the code, but you're missing too much stuff that was obviously defined elsewhere in your VB6 project to make this work as-is.

But I think this may be failing to address the underlying problem here. It looks like you're trying to do a literal port of the code without rewriting it to work properly with or take advantage of the .NET Base Class Libraries.

Take a look at the System.Net.Dns class in MSDN.

If you haven't already, I would suggest reading some tutorials or books on it to get started on C# and .NET. It's quite different to VB6 and you're just going to have a bad day trying to relate it to C#.

Xiaofu
Well thing is i know how to use .NET's advantages I was trying to port it over the way it was to see if there was to see functionality differences this code is more then 4,500 lines of code and I have manually translated most of it just this function and VB6's "With" function I am having trouble with. This function is actually in a namespace connected with the rest of the classes. So I don't see how its missing anything. Technically it should now be giving me that problem.
Nightforce2
Fair enough. I still think it's asking for trouble doing such a literal port of the VB6 code, especially with the rather suspect use of dynamic, but I'm sure you have your reasons. :)
Xiaofu
+5  A: 

You are reinventing the wheel. Try:

string ipAddress = "x.x.x.x";
string hostName = System.Net.Dns.GetHostEntry(ipAddress).HostName;
Christopher Painter
This is evident... But its for a conversion not for reinventing.
Nightforce2