1

I have this code:

    #region AimBot
    WebClient web2 = new WebClient(); //do not change this at all or delete
    string aimbotvalue = web2.DownloadString("https://vacnet.club/admin/aimbotfov.txt"); //gets the string value from your website
    public bool bAimbotEnabled = true;
    public bool bVisibleCheck = true;
    public bool bTargetOnGroundCheck = true;
    public int iAimbotDeathBreak = 350;
    public float flAimbotFov = aimbotvalue;  //sets the value from your website
    public float flAimbotSmooth = 30f;
    #endregion`

and consistently get these errors no matter what I try:

Error   CS0236  A field initializer cannot reference the non-static field, method, or property 'WeaponConfig.web2'  ExternalCSGO    C:\Users\laz\Desktop\ExternalCSGO-master
- Copy\External-CSGO\Settings\Config.cs 38  Active

Error   CS0236  A field initializer cannot reference the non-static field, method, or property 'WeaponConfig.aimbotvalue'   ExternalCSGO    C:\Users\laz\Desktop\ExternalCSGO-master
- Copy\External-CSGO\Settings\Config.cs 43  Active

Errors

What I am trying to do is have public float flAimbotFov = 3f read the 3f bit off of a text file on my website instead of just reading 3f.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • You should add the relevant program code, starting with Config.cs – Jeroen Heier Jul 20 '18 at 04:20
  • Note that the 'f' on the end of the 3f number is just *syntactic sugar* for the compiler so it knows the value is a float, rather than an int. Your text file should contain just the value (without the f), which you should parse as a float - you can't just assign a float to have the value of a string. – Richardissimo Jul 20 '18 at 05:56

2 Answers2

0

Please see Deciding between HttpClient and WebClient

Ahh... I understood your problem when I clicked on the link to the image.

You structure is

public class A {
 WebClient web2 = new WebClient(); //do not change this at all or delete
 string aimbotvalue = web2.DownloadString("https://vacnet.club/admin/aimbotfov.txt"); 
}

Your code would work inside a method e.g.

public class A
{
    string GetAimBotValue()
    {
        WebClient web2 = new WebClient(); //do not change this at all or delete
        string aimbotvalue = web2.DownloadString("https://vacnet.club/admin/aimbotfov.txt");
        return aimbotvalue;
        //BTW. You're missing any error handling
    } 

}

You could do something like this:

public class A
{
    private double aimbotvalue;

    public A()
    {
        aimbotvalue = GetAimBotValue();
    }
    double GetAimBotValue()
    {
        //This returns string value such as '10f'
        string aimbotvalueAsString = new WebClient().DownloadString("https://vacnet.club/admin/aimbotfov.txt");

        return double.Parse(aimbotvalueAsString);
        //BTW. You're missing error handling
    } 

}

or in your class:

    public WeaponConfig()
    {
        aimbotvalue = GetAimBotValue();
    }
    double GetAimBotValue()
    {
        //This returns string value such as '10f'
        string aimbotvalueAsString = new Http­Client()("https://vacnet.club/admin/aimbotfov.txt");

        return double.Parse(aimbotvalueAsString);
        //BTW. You're missing error handling
    } 
tymtam
  • 31,798
  • 8
  • 86
  • 126
  • what would you recommend to use then for my purposes? –  Jul 20 '18 at 04:17
  • after doing that, it eliminated the first error, but the name aimbotvalue still doesnt exist in the current context –  Jul 20 '18 at 04:27
  • In your case WebClient is fine. For the `aimbotvalue` problem you would have to show more code. – tymtam Jul 20 '18 at 04:29
  • yeah i tried that, for some reason this error keeps popping up: https://i.imgur.com/RMEFDez.png –  Jul 20 '18 at 04:30
  • I didn't mean for your to paste the whole `class A` ;/ – tymtam Jul 20 '18 at 04:32
  • After pasting the whole `class A` you now have 2 `aimbotvalue`. Get rid of `A` and just paste the `WeaponConfig` snipped from my answer. – tymtam Jul 20 '18 at 04:34
  • I have tried that, it is still not recognising aimbot value. I have renamed and tried several different reworks of the code. The one im left on is this: https://i.imgur.com/fxKh7ML.png –  Jul 20 '18 at 04:50
0

Make use of constructor for this kind of initializations:

WebClient web2;
string aimbotvalue; 
public float flAimbotFov;
public WeaponConfig()
{
    web2 = new WebClient();
    aimbotvalue = web2.DownloadString("https://vacnet.club/admin/aimbotfov.txt"); 
    flAimbotFov = aimbotvalue;
}

So that the variable initializations will happen at the time of the creation of objects for this class. So that the functionalities won't break.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88