C
C#2y ago
.dark4

✅ How to pick from a field in a list in with priority

foreach (Device obj in devicesBlock)
{
Program.Log($"Found device {obj.Model} {obj.Serial} {GetConnectionType(obj.ConnectionType)}");
}
foreach (Device obj in devicesBlock)
{
Program.Log($"Found device {obj.Model} {obj.Serial} {GetConnectionType(obj.ConnectionType)}");
}
i'm iterating through a list of devices, the issue is my device can connect via usb and wifi, so my device comes out twice in the list. How can I make it pick usb and if usb isn't there pick wifi, etc
2 Replies
.dark4
.dark4OP2y ago
I tried this:
foreach (Device obj in devicesBlock)
{
if (obj.ConnectionType == ConnectionType.USB)
{
Program.Log($"Connecting to {GetConnectionType(obj.ConnectionType)}");
device = obj;
break;
}
else if (obj.ConnectionType == ConnectionType.Ethernet)
{
Program.Log($"Connecting to {GetConnectionType(obj.ConnectionType)}");
device = obj;
break;
}
else if (obj.ConnectionType == ConnectionType.Bluetooth)
{
Program.Log($"Connecting to {GetConnectionType(obj.ConnectionType)}");
device = obj;
break;
}
else if (obj.ConnectionType == ConnectionType.WifiDirect || obj.ConnectionType == ConnectionType.Wifi)
{
Program.Log($"Connecting to {GetConnectionType(obj.ConnectionType)}");
device = obj;
break;
}
}
foreach (Device obj in devicesBlock)
{
if (obj.ConnectionType == ConnectionType.USB)
{
Program.Log($"Connecting to {GetConnectionType(obj.ConnectionType)}");
device = obj;
break;
}
else if (obj.ConnectionType == ConnectionType.Ethernet)
{
Program.Log($"Connecting to {GetConnectionType(obj.ConnectionType)}");
device = obj;
break;
}
else if (obj.ConnectionType == ConnectionType.Bluetooth)
{
Program.Log($"Connecting to {GetConnectionType(obj.ConnectionType)}");
device = obj;
break;
}
else if (obj.ConnectionType == ConnectionType.WifiDirect || obj.ConnectionType == ConnectionType.Wifi)
{
Program.Log($"Connecting to {GetConnectionType(obj.ConnectionType)}");
device = obj;
break;
}
}
this doesn't work since it just starts at the top of the list, I need to search the entire list with similar logic i think i can do
if(devicesBlock.Any(attribute=>attribute.getName()=="ConnectionType.USB"))
{
do stuff
}
if(devicesBlock.Any(attribute=>attribute.getName()=="ConnectionType.USB"))
{
do stuff
}
how would i refer to that device object out of the list to connect to it ultimately i need to connect to the device ConnectDevice(device); right now i'm just doing Device device = devicesBlock.First(); what I want to do is search the device list List<Device> devicesBlock = _api.GetDevicesBlock(); and connect by priority of connection type, USB first, the ethernet, then Bluetooth, then wifi or wifidirect because right now when I connect via usb and wifi is on, wifi is at the top of the list and devicesBlock.First(); always picks wifi with weaker connection than usb maybe I can get the index of the list and use that? trying this
int deviceIndex;
for (int i = 0; i < devicesBlock.Count; i++)
{
if (devicesBlock[i].ConnectionType == ConnectionType.USB)
{
Program.Log($"Connecting to USB");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.Ethernet)
{
Program.Log($"Connecting to Ethernet");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.Bluetooth)
{
Program.Log($"Connecting to Bluetooth");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.WifiDirect || obj.ConnectionType == ConnectionType.Wifi)
{
Program.Log($"Connecting to Wifi");
deviceIndex = i;
break;
}
}
int deviceIndex;
for (int i = 0; i < devicesBlock.Count; i++)
{
if (devicesBlock[i].ConnectionType == ConnectionType.USB)
{
Program.Log($"Connecting to USB");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.Ethernet)
{
Program.Log($"Connecting to Ethernet");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.Bluetooth)
{
Program.Log($"Connecting to Bluetooth");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.WifiDirect || obj.ConnectionType == ConnectionType.Wifi)
{
Program.Log($"Connecting to Wifi");
deviceIndex = i;
break;
}
}
this didn't work
int deviceIndex = 0;
for (int i = 0; i < devicesBlock.Count; i++)
{
if (devicesBlock[i].ConnectionType == ConnectionType.USB)
{
Program.Log($"Connecting to USB");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.Ethernet)
{
Program.Log($"Connecting to Ethernet");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.Bluetooth)
{
Program.Log($"Connecting to Bluetooth");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.WifiDirect || devicesBlock[i].ConnectionType == ConnectionType.Wifi)
{
Program.Log($"Connecting to Wifi");
deviceIndex = i;
break;
}
}

device = (Device)devicesBlock.Select((Device, index) => index = deviceIndex);
int deviceIndex = 0;
for (int i = 0; i < devicesBlock.Count; i++)
{
if (devicesBlock[i].ConnectionType == ConnectionType.USB)
{
Program.Log($"Connecting to USB");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.Ethernet)
{
Program.Log($"Connecting to Ethernet");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.Bluetooth)
{
Program.Log($"Connecting to Bluetooth");
deviceIndex = i;
break;
}
else if (devicesBlock[i].ConnectionType == ConnectionType.WifiDirect || devicesBlock[i].ConnectionType == ConnectionType.Wifi)
{
Program.Log($"Connecting to Wifi");
deviceIndex = i;
break;
}
}

device = (Device)devicesBlock.Select((Device, index) => index = deviceIndex);
this works int deviceIndex = devicesBlock.FindIndex(d => d.ConnectionType == ConnectionType.USB); Program.Log("Device index: " + deviceIndex); just need to figure out how to select from an index to connect device = (Device)devicesBlock.Select((Device, index) => index = deviceIndex); didn't work !close
Accord
Accord2y ago
Closed!
Want results from more Discord servers?
Add your server