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?
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:
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:
Perhaps you mean for it to do something like this?
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.