PDA

View Full Version : تبدیل حلقه For به دستور If



fox2030
یک شنبه 29 مرداد 1391, 01:07 صبح
سلام
کد زیر که از 2 حلقه for و یک دستور if تشکیل شده رو میخوام طوری تغییر بدم که فقط از دستور if استفاده بشه.
ولی نمیدونم چطور ان کار رو انجام بدم. لطفا راهنمایی کنید.

ممنون


Dim NetworkAdapters() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces
Dim myAdapterProps As IPInterfaceProperties = Nothing
Dim myGateways As GatewayIPAddressInformationCollection = Nothing
For Each adapter As NetworkInterface In NetworkAdapters
myAdapterProps = adapter.GetIPProperties
myGateways = myAdapterProps.GatewayAddresses
For Each Gateway As GatewayIPAddressInformation In myGateways
If Gateway.Address.ToString = ip Then
ListView1.Items(ListView1.Items.Count() - 1).ForeColor = Color.Blue
End If
Next
Next

arman_Delta2002
دوشنبه 30 مرداد 1391, 09:07 صبح
به این صورت اگه درست نوشته باشم

dim adapter as NetworkInterface
Dim Gateway As GatewayIPAddressInformation
:12 if adapter in NetworkAdapters
myAdapterProps = adapter.GetIPProperties myGateways = myAdapterProps.GatewayAddresses
:11 if Gateway in myGateways
If Gateway.Address.ToString = ip Then ListView1.Items(ListView1.Items.Count() - 1).ForeColor = Color.Blue
End If
else
goto 11
end if

else
goto 12
end if





فقط باید 2 تا متقیر شمارنده بزارین که اگه تعداد تعداد جاری شمارنده با تعداد کل myGateways یا NetworkAdapters کوچکتر مساوی بود دستور Goto رو اجرا کنه وگرنه ردش کنه

fox2030
دوشنبه 30 مرداد 1391, 15:39 عصر
ممنون، ولی کد مشکل داره. :ناراحت:
البته این رو هم بگم که نیازی به حلقه نیست، چون دستور فقط 1 بار باید بررسی بشه.

مهرداد صفا
دوشنبه 30 مرداد 1391, 21:39 عصر
سلام
کد زیر که از 2 حلقه for و یک دستور if تشکیل شده رو میخوام طوری تغییر بدم که فقط از دستور if استفاده بشه.
ولی نمیدونم چطور ان کار رو انجام بدم. لطفا راهنمایی کنید.

ممنون


Dim NetworkAdapters() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces
Dim myAdapterProps As IPInterfaceProperties = Nothing
Dim myGateways As GatewayIPAddressInformationCollection = Nothing
For Each adapter As NetworkInterface In NetworkAdapters
myAdapterProps = adapter.GetIPProperties
myGateways = myAdapterProps.GatewayAddresses
For Each Gateway As GatewayIPAddressInformation In myGateways
If Gateway.Address.ToString = ip Then
ListView1.Items(ListView1.Items.Count() - 1).ForeColor = Color.Blue
End If
Next
Next

سلام
از یک متغیر به عنوان شمارنده استفاده کنید و در هنگام next چک کنید که اگر کمتر از تعداد اعضای مجموعه بود به آغاز حلقه باز گردد، به این صورت:
()
Dim NetworkAdapters() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces
Dim myAdapterProps As IPInterfaceProperties = Nothing
Dim myGateways As GatewayIPAddressInformationCollection = Nothing
Dim i As Integer = 0
Dim j As Integer = 0
ForI:
myAdapterProps = NetworkAdapters(i).GetIPProperties
myGateways = myAdapterProps.GatewayAddresses
j = 0
ForJ:
If myGateways(j).Address.ToString = ip Then
ListView1.Items(ListView1.Items.Count() - 1).ForeColor = Color.Blue
End If
j += 1
If j < myGateways.Count Then GoTo ForJ
i += 1
If i < NetworkAdapters.Length Then GoTo ForI


موفق باشید!