Sebastian
Sebastian
CC#
Created by [ECH]JamHighlight on 4/3/2025 in #help
Accessing data for multiple days from weather api
To see why this is important (for HttpClient specifically) look here (if interested): https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-net-http-httpclient#instancing
19 replies
CC#
Created by [ECH]JamHighlight on 4/3/2025 in #help
Accessing data for multiple days from weather api
This is as opposed to what (if I remember correctly) you had before:
class MyProgram
{
async void FetchWeatherForecast()
{
using var client = new HttpClient(); // Not good!
}
}
class MyProgram
{
async void FetchWeatherForecast()
{
using var client = new HttpClient(); // Not good!
}
}
19 replies
CC#
Created by [ECH]JamHighlight on 4/3/2025 in #help
Accessing data for multiple days from weather api
Well, regardless. You should be able to create an instance of HttpClient and save the reference to a field/property of the class you are working in. For example:
class MyProgram
{
private readonly HttpClient client = new();

async void FetchWeatherForecast()
{
// Use 'client' as many times as you need
}
}
class MyProgram
{
private readonly HttpClient client = new();

async void FetchWeatherForecast()
{
// Use 'client' as many times as you need
}
}
19 replies
CC#
Created by [ECH]JamHighlight on 4/3/2025 in #help
Accessing data for multiple days from weather api
Is this in winforms or?
19 replies
CC#
Created by [ECH]JamHighlight on 4/3/2025 in #help
Accessing data for multiple days from weather api
Create the instance once and then reuse it
19 replies
CC#
Created by MrScautHD on 4/4/2025 in #help
Physics
Have you read the documentation? https://docs.unity3d.com/6000.0/Documentation/ScriptReference/MonoBehaviour.FixedUpdate.html
MonoBehaviour.FixedUpdate has the frequency of the physics system; it is called every fixed frame-rate frame. Compute Physics system calculations after FixedUpdate. 0.02 seconds (50 calls per second) is the default time between calls. Use Time.fixedDeltaTime to access this value.
This would seem to be what you want:
private void FixedUpdate()
{
World.Step(Time.fixedDeltaTime, _settings.Multithreaded);
}
private void FixedUpdate()
{
World.Step(Time.fixedDeltaTime, _settings.Multithreaded);
}
2 replies
CC#
Created by [ECH]JamHighlight on 4/3/2025 in #help
Accessing data for multiple days from weather api
Here are some additional pointers: 1. Move the API key out of your code entirely, load it from a local configuration file. Make sure that the configuration file is not tracked by a versioning system (so if you are using git, add it to .gitignore) 2. Don't initialize HttpClient repeatedly. This is especially bad with this type. 3. Always return early if you can (google "c# return early" if you don't know what I mean) 4. You were checking whether the HttpClient was null right after creating it (so it should never be null, the condition is useless) 5. Ideally you should replace JObject with something else 6. You forgot to dispose the object in response variable
private const string ApiKey = "..."; // TODO Load from configuration file instead, keeping this in code is a bad idea

private readonly HttpClient client = new();

async void FetchWeatherForecast()
{
string city = Citysearch.Text;
string url = $"https://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={ApiKey}";

try
{
using HttpResponseMessage response = await client.GetAsync(apiUrl);
response.EnsureSuccessStatusCode();
string content = await response.ReadAsStringAsync();
if (content is null)
{
Console.WriteLine("Received no content from wheather API");
return;
}

JObject weatherData = JObject.Parse(content);
// Handle weather data
}
catch (Exception ex)
{
Console.WriteLine($"Error while fetching from weather API: {ex.Message}");
}
}
private const string ApiKey = "..."; // TODO Load from configuration file instead, keeping this in code is a bad idea

private readonly HttpClient client = new();

async void FetchWeatherForecast()
{
string city = Citysearch.Text;
string url = $"https://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={ApiKey}";

try
{
using HttpResponseMessage response = await client.GetAsync(apiUrl);
response.EnsureSuccessStatusCode();
string content = await response.ReadAsStringAsync();
if (content is null)
{
Console.WriteLine("Received no content from wheather API");
return;
}

JObject weatherData = JObject.Parse(content);
// Handle weather data
}
catch (Exception ex)
{
Console.WriteLine($"Error while fetching from weather API: {ex.Message}");
}
}
19 replies
CC#
Created by [ECH]JamHighlight on 4/3/2025 in #help
Accessing data for multiple days from weather api
Also remove the API key from your message and keep it private
19 replies
CC#
Created by [ECH]JamHighlight on 4/3/2025 in #help
Accessing data for multiple days from weather api
You probably need to call a different API endpoint See the docs here https://openweathermap.org/api
19 replies
CC#
Created by Zilly on 4/1/2025 in #help
✅ Grid Layout
You did a fine job
100 replies
CC#
Created by Zilly on 4/1/2025 in #help
✅ Grid Layout
Works similarly to how int() works in Python int value = int.Parse("100");
100 replies
CC#
Created by Zilly on 4/1/2025 in #help
✅ Grid Layout
Python also has bool afaik
def get_true() -> bool:
return True
def get_true() -> bool:
return True
100 replies
CC#
Created by Zilly on 4/1/2025 in #help
✅ Grid Layout
bool
100 replies
CC#
Created by Zilly on 4/1/2025 in #help
✅ Grid Layout
There is a Contains method
100 replies
CC#
Created by Zilly on 4/1/2025 in #help
✅ Grid Layout
Depends on what you are trying to query. An array? A list? An enumerable? A dictionary?
100 replies
CC#
Created by Zilly on 4/1/2025 in #help
✅ Grid Layout
Sounds like a good way of getting to know C# syntax
100 replies
CC#
Created by Zilly on 4/1/2025 in #help
✅ Grid Layout
void BlahBlah()
{
// Codestuff
}

BlahBlah();
void BlahBlah()
{
// Codestuff
}

BlahBlah();
Pretty similar
100 replies
CC#
Created by Zilly on 4/1/2025 in #help
✅ Grid Layout
Yes
100 replies
CC#
Created by Zilly on 4/1/2025 in #help
✅ Grid Layout
A block of code; takes something in (arguments), does something (in method body) and returns some value (or void=nothing)
100 replies