Side note: I really could not think of a good title for this.
Ok, I am working on updating a program what was put together by a developer who did not believe in code reuse. SO i have been tasked with the job of updating and making it ‘friendly’ for future changes.
EDIT I should of said: The end users want a command prompt to see each ping. I know that using the built in ping would be a better option, but it is what they want…You guys know how that is /End Edit
I am working with a class that is implemented a couple times through out the program. (formerly there were two classes that basically did the same thing)
I decided to make an interface and implement it on the class.
(Have I lost you yet)
The method really just converts a hostname (one of the method paramaters) to an ipaddress, and spawns a command prompt to ping it. I also have a SEPARATE class that implements the interface and calls the method but instaed of passing a string(hostname) it passes an ipaddress object.
My question lies here: What is best practice when dealing with parameters that can change type? Would someone look over what I have done? It complies fine, i just want to make sure it is best practice.
// Interface that is implement twice in the program
interface Icmd_Ping
{
void initilize_Proc(object target, string last_Octet);
}
class process_spawn : Icmd_Ping
{
writeLog_Delegate writeLog = new writeLog_Delegate(error_Log.write_log);
private string ipaddress;
public object Ipaddress
{
get { return ipaddress; }
set
{
IPAddress ip = value as IPAddress;
if (ip != null)
ipaddress = ip.ToString();
else
{
try
{
formatIP format = new formatIP();
ipaddress = format.convert_Ip(((string)value));
}
catch (Exception ex)
{
writeLog(ex);
}
}
}
}
public void initilize_Proc(object target, string last_Octet = null)
{
if (target == null)
throw new ArgumentNullException();
formatIP format_IP = new formatIP();
this.Ipaddress = target;
Process cmd = new Process();
ProcessStartInfo psi = new ProcessStartInfo {FileName = "cmd", UseShellExecute = false, RedirectStandardOutput = false };
cmd.StartInfo = psi;
if (last_Octet != string.Empty)
psi.Arguments = string.Format("/c ping {0} -t", format_IP.format_Ip(((string)Ipaddress), last_Octet));
if (last_Octet == string.Empty)
psi.Arguments = string.Format("/c ping {0} -t", ((string)Ipaddress));
cmd.Start();
}
}
Should I convert the IPaddress in the ipaddress property? Or just make a new method and call that?
Sorry, I know its not very elegant at this point, I just want to make sure I am in the write direction before I really dive into this.
Thank every for your help.
Firstly, I’d suggest using
System.Net.IPAddressinstead of rolling your own.http://msdn.microsoft.com/en-us/library/system.net.ipaddress.aspx
Secondly, you could use
System.Net.NetworkInformation.Pingto do the ICMP-Echohttp://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx
e.g.