I am trying to make a Library for my own use. It's going to be a System.Net.Socket with support for SOCKS4/5 Proxy Servers. I have different variables, so i wanted one Struct for the Server i am connecting to and another struct for the proxy server i am connecting through. However i have two projects, one is the console and one is the library. I am not sure if i can do what i want to do.
This is an example of what i want to do, but i am sort of learning about Inheritance and Polymorphism as i go lol
Code:
using System;
namespace Test
{
class Class1 : Class2
{
static void Main()
{
myClass Class2 = new Class2();
variables myClass.myStruct = new myClass.myStruct();
variables.x = 5;
}
}
class Class2
{
struct myStruct
{
int x;
int y;
}
}
}
This is what i am doing
Code:
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using Test.Socket;
class Test {
static void Main()
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.ProxyEndPoint = new IPEndPoint(IPAddress.Parse("SERVER"), PORT);
s.ProxyUser = "username";
s.ProxyPass = "password";
s.ProxyType = ProxyTypes.Socks5;
s.Connect("SERVER", PORT);
}
}
Re: Structure of Properties within a Seperate Class?
i want to create a Structure of Properties exactly like you said. However i am using a Derived Class as a Asynchrous Socket. I can't figure out how to access the Structure of my derived class. I am not really sure i need to, but that's what i am thinking.
Thanks
- Joel
There are only two kinds of people who are truely fascinating: people who know absolutely everything, and people who know absolutely nothing.
Re: Structure of Properties within a Seperate Class?
Code:
using System;
using System.Net;
namespace Test
{
public class Parent
{
struct ProxyInfo
{
public enum Types {None = 0, Socks4 = 4, Socks5 = 5}
private IPEndPoint m_EndPoint;
private Types m_Type;
private string m_Username;
private string m_Password;
private string m_Server;
private int m_Port;
public IPEndPoint EndPoint
{
get
{
return m_EndPoint;
}
set
{
m_EndPoint = value;
}
}
public Types Type
{
get
{
return m_Type;
}
set
{
m_Type = value;
}
}
public string Username
{
get
{
return m_Username;
}
set
{
if (value == null)
throw new ArgumentNullException();
m_Username = value;
}
}
public string Password
{
get
{
return m_Password;
}
set
{
if (value == null)
throw new ArgumentNullException();
m_Password = value;
}
}
public string Server
{
get
{
return m_Server;
}
set
{
if (value == null)
throw new ArgumentNullException();
m_Server = value;
}
}
public int Port
{
get
{
return m_Port;
}
set
{
m_Port = value;
}
}
}
}
public class Child
{
static void Main()
{
Parent child = new Parent();
child.ProxyInfo Proxy = new child.ProxyInfo();
Console.ReadLine();
}
}
}
For some reason the Intellisense never shows the struct or any of it's properties when i access it via 'child'.
I want to access the methods INSIDE the Parent class as well as have a seperate struct object derived from the structure inside the same derived class. (wow does that sound horrible)
Maybe i am just horribly confused and someone knows a better way to do this. I didn't want to have a whole bunch of Properties with a Prefix describing what they are for, i just wanted to use a struct to make it more organized.
Then i can do my project like this:
Code:
public class Child
{
static void Main()
{
ProxySocket s = new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.ProxyInfo Proxy = new s.ProxyInfo();
Proxy.Server = "255.255.255.255";
Proxy.Port = 1080;
Proxy.EndPoint = new IPEndPoint(IPAddress.Parse(Proxy.Server), Proxy.Port);
Proxy.Username = "username";
Proxy.Password = "password";
Proxy.Type = Types.Socks5;
s.Connect
Console.ReadLine();
}
}
This is the best explination i can give, i just hope someone understands my logic.
Thanks Everybody For Your Help
- Joel Zimmerman
There are only two kinds of people who are truely fascinating: people who know absolutely everything, and people who know absolutely nothing.
Re: Structure of Properties within a Seperate Class?
Your structure is a nested type, NOT a member of an instance of the class. 'child' is an instance of the Parent class, so you should be referring to 'Parent.ProxyInfo', not 'child.ProxyInfo'.
Re: Structure of Properties within a Seperate Class?
Further, if you want the Parent class to have a member variable that is of that type then you would need to declare it so. In the code you posted previously the Parent class has no properties whatsoever. It sounds like what you may be wanting is to give the Parent class a property of the ProxyInfo type. To do that you should declare a private variable of that type and then define a public property to access that variable.
Re: Structure of Properties within a Seperate Class?
Thanks for the reply. Imma study what you said because i didn't really understand it at first glance. I'm going to play with some things and look on MSDN some and i'll get back to you if i have any more problems.
Thanks
- Joel
There are only two kinds of people who are truely fascinating: people who know absolutely everything, and people who know absolutely nothing.
Re: Structure of Properties within a Seperate Class?
In the screen shot in my previous post, an instance of the SomeClass class does not have a member property that is a SomeStruct object. If you wanted that class to have a property of that type it would have to look like this:
Code:
public class SomeClass
{
// This is a nested type. It has nothing to do with instances of the class.
public struct SomeStruct
{
public string SomeField;
}
// This is a private variable of the nested type. It is a member an individual instance.
private SomeStruct myVar;
// This property exposes the instance variable.
public SomeStruct MyProperty
{
get { return myVar; }
set { myVar = value; }
}
}
Re: Structure of Properties within a Seperate Class?
This is not going as well as i had hoped. I am having trouble Setting the variables.
Code:
private ProxyInfo m_ProxyVar;
public ProxyInfo Proxy
{
get
{
return m_ProxyVar;
}
set
{
m_ProxyVar = value;
}
}
public struct ProxyInfo
{
private IPEndPoint m_EndPoint;
private ProxyTypes m_Type;
private string m_Username;
private string m_Password;
private string m_Server;
private int m_Port;
public IPEndPoint EndPoint
{
get
{
return m_EndPoint;
}
set
{
m_EndPoint = value;
}
}
public ProxyTypes Type
{
get
{
return m_Type;
}
set
{
m_Type = value;
}
}
public string Username
{
get
{
return m_Username;
}
set
{
if (value == null)
throw new ArgumentNullException();
m_Username = value;
}
}
public string Password
{
get
{
return m_Password;
}
set
{
if (value == null)
throw new ArgumentNullException();
m_Password = value;
}
}
public string Server
{
get
{
return m_Server;
}
set
{
if (value == null)
throw new ArgumentNullException();
m_Server = value;
}
}
public int Port
{
get
{
return m_Port;
}
set
{
m_Port = value;
}
}
}
And
Code:
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using Test.ProxySocket;
class Console {
static void Main()
{
ProxySocket s = new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Proxy.Server = "255.255.255.255";
s.Proxy.Port = 1080;
s.Proxy.EndPoint = new IPEndPoint(IPAddress.Parse(Proxy.Server), Proxy.Port);
s.Proxy.Username = "username";
s.Proxy.Password = "password";
s.Proxy.Type = ProxyTypes.Socks5;
}
}
I get "Cannot Modify the return value of 'Test.ProxySocket.ProxySocket.Proxy' because it is not a variable"
That ProxySocket.ProxySocket looks very suspicious but i have no idea why it happens.
Any help is appreciated
- Joel
There are only two kinds of people who are truely fascinating: people who know absolutely everything, and people who know absolutely nothing.
Re: Structure of Properties within a Seperate Class?
When you get an instance of a class through a property you are getting a reference to the same underlying object. That's the way reference types work. When you get an instance of a structure through a property you are getting a temporary copy of the existing object. That's the way value types work. You are not allowed to change the properties of that copy because there is no valid reason for doing so because it is immediately lost. What you need to do is assign the copy to a variable, change whatever properties you want to and then assign the object back to the property.