Reputation: 1476
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?
Upvotes: 0
Views: 454
Reputation: 128317
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.
Upvotes: 2
Reputation: 55571
You are reinventing the wheel. Try:
string ipAddress = "x.x.x.x";
string hostName = System.Net.Dns.GetHostEntry(ipAddress).HostName;
Upvotes: 5
Reputation: 15889
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#.
Upvotes: 3