Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
C#
IPNetwork ipnetwork = IPNetwork.Parse(item);
                        /*Log.Text = "First Ip: " + ipnetwork.FirstUsable;
                        Log.Text = "Last Ip: " + ipnetwork.LastUsable;*/
                        startIP = "" + ipnetwork.FirstUsable;
                        lastIp = "" + ipnetwork.LastUsable;

                        
                        int iOne = 255;
                        int iTow = 255;
                        int iThree = 255;
                        int iFour = 255;
                        string[] tokens = startIP.Split('.');
                        int value1 = Int32.Parse(tokens[0]);   // 192
                        int value2 = Int32.Parse(tokens[1]);   // 168
                        int value3 = Int32.Parse(tokens[2]);   // 0
                        int value4 = Int32.Parse(tokens[3]);   // 1

                        string[] tokensTow = lastIp.Split('.');
                        int valueOne = Int32.Parse(tokens[0]);   // 192
                        int valueTow = Int32.Parse(tokens[1]);   // 168
                        int valueThree = Int32.Parse(tokens[2]);   // 0
                        int valueFour = Int32.Parse(tokens[3]);   // 1


                        if (value3 == valueThree)
                        {
                            for (iOne = value4; iOne < valueFour; iOne++)
                            {
                                string IpOne = value1 + "." + value2 + "." + value3 + "." + iOne + "\n";
                                Enumurator.Text = IpOne + "\n";
                            }
                        }
Posted
Comments
PIEBALDconsult 21-Nov-15 16:06pm    
No way to know unless you tell us what the problem is.
What do you expect to happen that isn't?
What is happening that shouldn't?
Shawky Morgan 21-Nov-15 16:46pm    
so nice first thanks for answer and second i know my in values it not readable inside my for statement so from i a few hour's ago i still tiring to do it i try to convert it back to string but for not working just need int value i do it by many ways in php but because i still have no experience in c# i trying by google and code project also i found to much answers on it and i still trying until i will do it ... so any something you want to say
Sergey Alexandrovich Kryukov 21-Nov-15 17:07pm    
Sorry, you are answering wrong questions, so your reply is not very happy. I'll ask it in a different way:

What do you want to achieve? What seems to be wrong, exactly? If the behavior of your code does not match what you expected, use the debugger and fix it. If you failed to do so, describe what did you want to achieve, what's expected behavior, what is observed behavior, why do you feel it's wrong. If you have some exceptions, describe steps to reproduce, complete exception information. Comment the points in code related to exception throwing/propagation. Post all what's relevant. See also: http://www.sscce.org.

—SA
Shawky Morgan 21-Nov-15 17:20pm    
thanks alot
Sergey Alexandrovich Kryukov 21-Nov-15 17:29pm    
You are welcome.

Besides, you don't need to Google for code; it will be a waste of time at this moment. Instead, write code yourself. For this purpose, learn all what happens when you add and write all your code files, compile and run the project. Learn each and every feature your language has, and get some general ideas on what the libraries do, in this case, .NET FCL. You don't have to memorize it all, just make sure you understand the fundamentals and know how to find things in the documentation. You may want to look at code samples written by others later, when you have some particular problem or want to improve your work and see how other people solved similar problems. Use the debugger comprehensively if you have even slightest concern of your runtime behavior.

And ask your questions based on your experience. Formulate the problem, and… — see my advice above.

—SA

1 solution

Start by looking at your code with teh debugger, and follow it through to see exactly what is happening.
Then, you will see that it's quite simple:
C#
string[] tokens = startIP.Split('.');
int value1 = Int32.Parse(tokens[0]);   // 192
int value2 = Int32.Parse(tokens[1]);   // 168
int value3 = Int32.Parse(tokens[2]);   // 0
int value4 = Int32.Parse(tokens[3]);   // 1

string[] tokensTow = lastIp.Split('.');
int valueOne = Int32.Parse(tokens[0]);   // 192
int valueTow = Int32.Parse(tokens[1]);   // 168
int valueThree = Int32.Parse(tokens[2]);   // 0
int valueFour = Int32.Parse(tokens[3]);   // 1

You need to use the result of the second split, instead of the first split again:
C#
string[] tokens = startIP.Split('.');
int value1 = Int32.Parse(tokens[0]);   // 192
int value2 = Int32.Parse(tokens[1]);   // 168
int value3 = Int32.Parse(tokens[2]);   // 0
int value4 = Int32.Parse(tokens[3]);   // 1

string[] tokensTow = lastIp.Split('.');
int valueOne = Int32.Parse(tokensTow [0]);   // 192
int valueTow = Int32.Parse(tokensTow [1]);   // 168
int valueThree = Int32.Parse(tokensTow [2]);   // 0
int valueFour = Int32.Parse(tokensTow [3]);   // 1

And don't forget that if you set the same value repeatedly in a loop, it will only ever contain the last value set when the loop exits:
C#
for (iOne = value4; iOne < valueFour; iOne++)
{
    string IpOne = value1 + "." + value2 + "." + value3 + "." + iOne + "\n";
    Enumurator.Text = IpOne + "\n";
}
It's possible that you want to use "+=" instead of "=" in that last line. (Or better, a StringBuilder)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900