2

I'm using the simpleWifi library to implement connections to Wi-Fi access points and I'm trying to set a string value to a class object. Down below in my example, I'm trying to set the variable WifiPoint to a string value from another window form.

AccessPoint WifiPoint = frmSettings.wifi_selected_name.ToString();

SimpleWiFi class

namespace SimpleWifi
{
    public class AccessPoint
    {
        public string Name { get; }
        public uint SignalStrength { get; }
        public bool HasProfile { get; }
        public bool IsSecure { get; }
        public bool IsConnected { get; }

        public bool Connect(AuthRequest request, bool overwriteProfile = false);
        public void ConnectAsync(AuthRequest request, bool overwriteProfile = false, Action<bool> onConnectComplete = null);
        public void DeleteProfile();
        public string GetProfileXML();
        public bool IsValidPassword(string password);
        public sealed override string ToString();
    }
}

My attempt to do due this has resulted in the error showing "Cannot convert type 'string' to 'SimpleWifi.AccessPoint'

Sahil Bora
  • 173
  • 1
  • 12
  • You have set the type of WifiPoint to AccessPoint which is a class and you are trying to set a string value for a class type variable. It will always fail. – Shahid Manzoor Bhat Dec 10 '19 at 05:12
  • No, its wrong, you can't assign a string value to class object . Why can't you save it in "Name" like this AccessPoint wifiPoint =new AccessPoint(); wifiPoint.Name= frmSettings.wifi_selected_name.ToString(); – Noorul Dec 10 '19 at 05:13
  • you can override the ```ToString()``` method and Implement it yourself By your own way – Hamed Hajiloo Dec 10 '19 at 05:14
  • I tried using the ToString() method but it still failed – Sahil Bora Dec 10 '19 at 05:14
  • Also when I tried creating a new object as Noorul described it didn't work either saying that the constructor doesn't take 0 arguments – Sahil Bora Dec 10 '19 at 05:17
  • Then you have posted incomplete code here. Please post all the code if you are having any parameterized constructors in the AccessPoint class post them here – Shahid Manzoor Bhat Dec 10 '19 at 05:19
  • That's all the code I have because I installed the simpleWiFi library via the package manager command prompt – Sahil Bora Dec 10 '19 at 05:21
  • Does this Access point class belong to the same library? – Shahid Manzoor Bhat Dec 10 '19 at 05:22
  • I'm pretty sure it belongs to the same library – Sahil Bora Dec 10 '19 at 05:24
  • You need to insspect using intellisense what needs to be passed to its constructor to create an object of the AccessPoint class var obj = new AccessPoint(Inspect what params it needs); then assign the resultant obj.Name = frmSettings.wifi_selected_name.ToString(); – Shahid Manzoor Bhat Dec 10 '19 at 05:27
  • There are only "get" is available in those properties, you can't "set" anything. Thats why you were not able to assign any value to "Name". – Noorul Dec 10 '19 at 05:35
  • That probably explains why, I can't modify the file because it's in the metadata – Sahil Bora Dec 10 '19 at 05:37
  • yeah, that may be with purpose. If so you can only get the values from the lib, could not set . – Noorul Dec 10 '19 at 05:43
  • It would be possible with **implicit operator** but not the way to solve this – Sir Rufo Dec 10 '19 at 05:47

1 Answers1

0

You need to take a look on the intent of the library.

The constructor code is

internal AccessPoint(WlanInterface interfac, 
              WlanAvailableNetwork network)
    {
        _interface = interfac;
        _network = network;
    }

The intent of the author is for library consumers, to never create an instance of AccessPoint directly.

In the main example, he does the following to create then

    IEnumerable<AccessPoint> accessPoints = wifi.GetAccessPoints().OrderByDescending(ap => ap.SignalStrength);

        int i = 0;
        foreach (AccessPoint ap in accessPoints)
            Console.WriteLine("{0}. {1} {2}% Connected: {3}", i++, ap.Name, ap.SignalStrength, ap.IsConnected);

        return accessPoints;

So, as he does, find available wifi networks, get the access points and check from your config values, if they are the target ones

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • Hey thanks for explaining but in my application it's not a console project. I'm still trying to find a way to work around setting the AccessPoint as a string value. I've tried using AccessPoint WiFiPoint = (AccessPoint)cbWifiname.Tag but still no luck. – Sahil Bora Dec 10 '19 at 23:27
  • @SahilBora what Athanasios told you is correct, all you need to do is find the access point object from the list of access points where the access point name equals the string you have. Using Linq: `AccessPoint accessPoint = accessPoints.First(ap => ap.Name == frmSettings.wifi_selected_name.ToString())`. – Denis G. Labrecque Aug 16 '22 at 20:03