PDA

Click to See Complete Forum and Search --> : Iteration Problem


BaDDBLooD
Aug 5th, 2006, 03:48 PM
using System;
using System.Collections.Generic;
using System.Text;
using System.Resources;

namespace Proxy
{
class Program
{
static void Main(string[] args)
{
ProxyInfo[] Proxy = new ProxyInfo();
foreach (string p in Resource.Proxies)
{

}

}
}

public struct ProxyInfo
{
private string Username;
private string Password;
private string Address;
private short Port;
private int Type;

public string Username
{
get
{
return Username;
}
set
{
Username = value;
}
}
public string Password
{
get
{
return Password;
}
set
{
Password = value;
}
}
public string Address
{
get
{
return Address;
}
set
{
Address = value;
}
}
public short Port
{
get
{
return Port;
}
set
{
Port = value;
}
}
public int Type
{
get
{
return Type;
}
set
{
Type = value;
}
}
}
}



my Proxies Resource has this in it



201.12.189.79:1080@SOCKS4
71.206.139.211:5219@SOCKS4
24.147.237.50:10961@SOCKS4
59.148.66.123:32167@SOCKS4
59.30.182.9:4769@SOCKS4
69.242.39.99:5740@SOCKS5
201.12.150.87:1080@SOCKS5
24.147.237.50:10961@SOCKS5
201.53.226.112:1080@SOCKS5
68.188.194.36:5882@SOCKS5



I am trying to get it so it's like Proxy[p].Username, password, etc... I can't seem to figure out how to split up the line effectively and assign it to the array. I also wanted to make an Enumerator for SOCKSv4, SOCKSv4a, SOCKSv5 for the Type Property but i couldn't get that to work out either.

Thanks for the help

- Joel

jmcilhinney
Aug 5th, 2006, 07:25 PM
It's an "enumeration", not an "enumerator". An enumerator is something else entirely.public enum SocksType
{
SOCKS4,
SOCKS4a,
SOCKS5
}Your structure isn't going to work because you've given the private fields and public properties the same name. You'd normally start the field names with a lower case letter and the property names with an upper case letter. I also like to add an underscore to the front of the field name to make the difference more obvious.public struct ProxyInfo
{
private string _userName;
private string _password;
private string _address;
private short _port;
private SocksType _type;

public string UserName
{
get { return _userName; }
set { _userName = value; }
}

public string Password
{
get { return _password; }
set { _password = value; }
}

public string Address
{
get { return _address; }
set { _address = value; }
}

public short Port
{
get { return _port; }
set { _port = value; }
}

public SocksType Type
{
get { return _type; }
set { _type = value; }
}

public ProxyInfo(string proxyString)
{
this._userName = string.Empty;
this._password = string.Empty;

// You should probably use a Regex to validate the string but I'll leave that to you.

string[] proxyParts = proxyString.Split(':', '@');

this._address = proxyParts[0];
this._port = short.Parse(proxyParts[1]);
this._type = (SocksType)(Enum.Parse(typeof(SocksType), proxyParts[2]));
}
}Note the addition of the constructor that will parse your string and set the properties of the ProxyInfo object accordingly. You now have to change your Main method, which is syntactically incorrect. The first line is trying to create a single ProxyInfo object and assign it to a ProxyInfo array variable, which isn't going to work:public void Main()
{
ProxyInfo[] proxies = new ProxyInfo[Resource.Proxies.Length];

for (int i = 0; i < proxies.Length; i++)
{
proxies[i] = new ProxyInfo(Resource.Proxies[i]);
}
}I'm not 100% sure what Resource.Proxies is, so you may have to adjust that slightly.

jmcilhinney
Aug 5th, 2006, 07:30 PM
Note that you can use the in-built "prop" code snippet to help you define a property. Just type "prop" and then hit the Tab key twice to generate a generic property definition. You can the set the type and name of private field to update the property defintion to the correct type and to use the correct field name. Use the Tab key to move amongst the highlighted parts of the snippet.

BaDDBLooD
Aug 5th, 2006, 08:31 PM
Yeah, i was planning on using a regex match for validation but i haven't got that far.

You always have the best posts with most usefull stuff in them, Thank you very very much :)~

Also, what would be the best way to format the proxy address. like username:password@255.255.255.255:1080 should i put like @ SOCKS4/5 at the end... or what do you think?

nebulom
Aug 6th, 2006, 09:19 PM
Note that you can use the in-built "prop" code snippet to help you define a property. Just type "prop" and then hit the Tab key twice to generate a generic property definition. You can the set the type and name of private field to update the property defintion to the correct type and to use the correct field name. Use the Tab key to move amongst the highlighted parts of the snippet.
This maybe an out of topic post but I just want to give a smile of the prop Tab key feature in VS.NET. Code refactoring in Netbeans still is a good thing, better than VS.

:)