C
C#•11mo ago
Pannekoekje

Variable scoping

Considering this code:
BackgroundWorker backgroundWorker = new();


client.Initialize();
await client.Authenticate();
client.AddCallback<TestObject>(ShowOrders);

var testString = "a";
XX test = new()
{
Test = "xx"
};

void ShowOrders(TestObject response)
{
test.Test = "xx";
testString = "b";
}

for (var x = 0; x<9; x++)
{
await client.GetAccount();
await Task.Delay(500);
}

backgroundWorker.RunWorkerAsync(client.Receive());
await Task.Delay(5000);

System.Console.WriteLine(testString);

public class XX {

public string? Test { get; set; }

}
BackgroundWorker backgroundWorker = new();


client.Initialize();
await client.Authenticate();
client.AddCallback<TestObject>(ShowOrders);

var testString = "a";
XX test = new()
{
Test = "xx"
};

void ShowOrders(TestObject response)
{
test.Test = "xx";
testString = "b";
}

for (var x = 0; x<9; x++)
{
await client.GetAccount();
await Task.Delay(500);
}

backgroundWorker.RunWorkerAsync(client.Receive());
await Task.Delay(5000);

System.Console.WriteLine(testString);

public class XX {

public string? Test { get; set; }

}
Trying to compile, within the "ShowOrders" function, the client.AddCallback<TestObject>(ShowOrders); line gives the error : Use of unassigned local variable 'test' Why does it not complain about testString ?
16 Replies
Angius
Angius•11mo ago
$scopes
MODiX
MODiX•11mo ago
scope A {
thing a;
scope B {
thing b;
}
}
scope A {
thing a;
scope B {
thing b;
}
}
thing a is available in scope A and scope B thing b is available only in scope B
Angius
Angius•11mo ago
Also, you have to understand top-level code, and that what it actually does, is puts your code into a class and a method So your
var foo = 69;

void Bar()
{
Console.WriteLine($"{foo} is nice");
}
var foo = 69;

void Bar()
{
Console.WriteLine($"{foo} is nice");
}
really becomes something akin to
internal class Program
{
private static void Main(string[] args)
{
int foo = 69;
}

private static void Bar()
{
Console.WriteLine($"{foo} is nice");
}
}
internal class Program
{
private static void Main(string[] args)
{
int foo = 69;
}

private static void Bar()
{
Console.WriteLine($"{foo} is nice");
}
}
Or, well, wait In your case it's actually a local function
Moods
Moods•11mo ago
yeah you shot yourself in the foot by going the top-level statement approach
Pannekoekje
PannekoekjeOP•11mo ago
I'm just trying out some lib with a simple program 🙂
Moods
Moods•11mo ago
yeah i think the middle function becomes a local one but i wouldnt be sure i avoid tls
Angius
Angius•11mo ago
You can use TLS, you just need to understand what it does Generally speaking, move your functions to the end of the file
Pannekoekje
PannekoekjeOP•11mo ago
The client is the lib, therefor the test.Test would be accessed via there?
Angius
Angius•11mo ago
After the top-level code
Pannekoekje
PannekoekjeOP•11mo ago
Ok, so considering this
namespace Application
{
class Test
{
static async Task Main()
{
var client = Client.Create
.WithApiKey("xx")
.WithApiSecret("xx");

BackgroundWorker backgroundWorker = new();


client.Initialize();
await client.Authenticate();
client.AddCallback<PrivateGetAccountResponse>(ShowOrders);

var testString = "a";
XX test = new()
{
Test = "xx"
};

void ShowOrders(PrivateGetAccountResponse response)
{
test.Test = "xx";
testString = "b";
}

for (var x = 0; x < 9; x++)
{
await client.GetAccount();
await Task.Delay(500);
}

backgroundWorker.RunWorkerAsync(client.Receive());
await Task.Delay(5000);

System.Console.WriteLine(testString);
}

public class XX
{
public string? Test { get; set; }
}
}
}
namespace Application
{
class Test
{
static async Task Main()
{
var client = Client.Create
.WithApiKey("xx")
.WithApiSecret("xx");

BackgroundWorker backgroundWorker = new();


client.Initialize();
await client.Authenticate();
client.AddCallback<PrivateGetAccountResponse>(ShowOrders);

var testString = "a";
XX test = new()
{
Test = "xx"
};

void ShowOrders(PrivateGetAccountResponse response)
{
test.Test = "xx";
testString = "b";
}

for (var x = 0; x < 9; x++)
{
await client.GetAccount();
await Task.Delay(500);
}

backgroundWorker.RunWorkerAsync(client.Receive());
await Task.Delay(5000);

System.Console.WriteLine(testString);
}

public class XX
{
public string? Test { get; set; }
}
}
}
Angius
Angius•11mo ago
Also, I wonder about that .AddCallback(), smells of a callback-hell-ish thing from pre-async times I'd need to know what the lib is to tell for sure, tho
Pannekoekje
PannekoekjeOP•11mo ago
Its something I wrote a while ago, with the callback added to an object to invoke them later on
Angius
Angius•11mo ago
Well, in any case, you're passing a local function as a delegate So chances are it's non-capturing
Pannekoekje
PannekoekjeOP•11mo ago
Alright, I think that's it. thx 🙂
jcotton42
jcotton42•11mo ago
You should not be using BackgroundWorker
jcotton42
jcotton42•11mo ago
Task.Run vs BackgroundWorker: Intro
This is an introductory post for a new series that I’ll be doing comparing BackgroundWorker to Task.Run (in an async style). I always recommend Task.Run, and I have already written a long post describing why, but I still see some developers resisting the New Way of Doing Things (TM). So this will be a short series where I’ll compare the code sid...
Want results from more Discord servers?
Add your server