! 0Falco
! 0Falco
Explore posts from servers
CC#
Created by ! 0Falco on 5/10/2024 in #help
How to refer to the mainwindow class from another class.
thanks I'll go ahead and do that
5 replies
CC#
Created by ! 0Falco on 5/10/2024 in #help
How to refer to the mainwindow class from another class.
ah yes true, that would be a lot cleaner
5 replies
CC#
Created by ! 0Falco on 5/10/2024 in #help
How to refer to the mainwindow class from another class.
I tried the following, suggested by ChatGPT
private MainWindow mainWindow;

public FilterManager(MainWindow mainWindow)
{
this.mainWindow = mainWindow;
}
private MainWindow mainWindow;

public FilterManager(MainWindow mainWindow)
{
this.mainWindow = mainWindow;
}
But I still cannot access the listview in my WPF UI like this
weatherListView.Items.Clear();
weatherListView.ItemsSource = FilteredWeatherData;
weatherListView.Items.Clear();
weatherListView.ItemsSource = FilteredWeatherData;
5 replies
CC#
Created by ! 0Falco on 5/9/2024 in #help
How to get rid of nullable warning?
class ApiManager
{
public async Task<T?> ApiRequest<T>(string apiUrl, string apiKey = "")
{
using (var client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync(apiUrl);

if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();

T model = JsonConvert.DeserializeObject<T>(responseBody);
return model;
}
else
{
Console.WriteLine($"Failed to make request. Status code: {response.StatusCode}");
return default; // Return default value for type T
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
return default; // Return default value for type T
}
}
}
}
class ApiManager
{
public async Task<T?> ApiRequest<T>(string apiUrl, string apiKey = "")
{
using (var client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync(apiUrl);

if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();

T model = JsonConvert.DeserializeObject<T>(responseBody);
return model;
}
else
{
Console.WriteLine($"Failed to make request. Status code: {response.StatusCode}");
return default; // Return default value for type T
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
return default; // Return default value for type T
}
}
}
}
I have the same issue for this line T model = JsonConvert.DeserializeObject<T>(responseBody); Using a if function to check if responsebody is null does not seem to work
15 replies
CC#
Created by ! 0Falco on 5/9/2024 in #help
How to get rid of nullable warning?
Any other options?
15 replies
CC#
Created by ! 0Falco on 5/9/2024 in #help
How to get rid of nullable warning?
I dont like that solution
15 replies
CC#
Created by ! 0Falco on 5/9/2024 in #help
How to get rid of nullable warning?
Some nested if statements just to check if each object is not null
15 replies
CC#
Created by ! 0Falco on 5/9/2024 in #help
How to get rid of nullable warning?
So I have this code where I get data from an api which is nested in some objects I can retrieve it using: weatherData.Hourly.Time So this is a nested object but if I want the time value, I cannot just check if weatherData is null. I also have to check if weatherData.Hourly is null. But then my code ends up like this
if (weatherData != null) {
if (weatherData.Hourly != null) {
if (weatherData.Hourly.Time != null) { }
}
if (weatherData != null) {
if (weatherData.Hourly != null) {
if (weatherData.Hourly.Time != null) { }
}
15 replies
CC#
Created by ! 0Falco on 4/17/2024 in #help
Practicing OOP in c#, is this logical implementation of an Interface?
That's okay no worries:)
65 replies
CC#
Created by ! 0Falco on 4/17/2024 in #help
Practicing OOP in c#, is this logical implementation of an Interface?
Thx I'll check itg out
65 replies
CC#
Created by ! 0Falco on 4/17/2024 in #help
Practicing OOP in c#, is this logical implementation of an Interface?
No description
65 replies
CC#
Created by ! 0Falco on 4/17/2024 in #help
Practicing OOP in c#, is this logical implementation of an Interface?
So I could also do this Instead of using IOutputHandler as a interface, I can just call each handler from Main() like this.
C#
ConsoleOutputHandler consoleHandler = New DatabaseOutputHandler();
consoleHandler.Write()
FileOutputHandler fileHandler = New DatabaseOutputHandler();
fileHandler.Write()
C#
ConsoleOutputHandler consoleHandler = New DatabaseOutputHandler();
consoleHandler.Write()
FileOutputHandler fileHandler = New DatabaseOutputHandler();
fileHandler.Write()
But an interface would be more practical because of abstraction, I dont have to create a tightly coupled instance with each specific handler classes to access it. So it's better to just call the interface instead, like this:
c#
IOutputHandler consoleHandler = new ConsoleOutputHandler();
consoleHandler.Write("Hello from console!");

IOutputHandler fileHandler = new FileOutputHandler();
fileHandler.Write("Hello to file!");
c#
IOutputHandler consoleHandler = new ConsoleOutputHandler();
consoleHandler.Write("Hello from console!");

IOutputHandler fileHandler = new FileOutputHandler();
fileHandler.Write("Hello to file!");
65 replies
CC#
Created by ! 0Falco on 4/17/2024 in #help
Practicing OOP in c#, is this logical implementation of an Interface?
But it will become fully clear once I have to implement this myself
65 replies
CC#
Created by ! 0Falco on 4/17/2024 in #help
Practicing OOP in c#, is this logical implementation of an Interface?
Ah okay I think I get it
65 replies
CC#
Created by ! 0Falco on 4/17/2024 in #help
Practicing OOP in c#, is this logical implementation of an Interface?
why is it more useful than not using an inteface
65 replies
CC#
Created by ! 0Falco on 4/17/2024 in #help
Practicing OOP in c#, is this logical implementation of an Interface?
and why is that useful
65 replies
CC#
Created by ! 0Falco on 4/17/2024 in #help
Practicing OOP in c#, is this logical implementation of an Interface?
I don't understand what you mean with and you dont need to know what specific implementation that is
65 replies
CC#
Created by ! 0Falco on 4/17/2024 in #help
Practicing OOP in c#, is this logical implementation of an Interface?
And that's not what I'm doing
65 replies
CC#
Created by ! 0Falco on 4/17/2024 in #help
Practicing OOP in c#, is this logical implementation of an Interface?
Okay so I'm using the same function over multiple classes
65 replies
CC#
Created by ! 0Falco on 4/17/2024 in #help
Practicing OOP in c#, is this logical implementation of an Interface?
Is this what you mean
c#
using System;

public interface IOutputHandler
{
void Write(Guid id, string data);
}

public class ConsoleOutputHandler : IOutputHandler
{
public void Write(Guid id, string data)
{
Console.WriteLine($"Console: {id} - {data}");
}
}

public class FileOutputHandler : IOutputHandler
{
public void Write(Guid id, string data)
{
// Write to a file implementation
Console.WriteLine($"File: {id} - {data}");
}
}

public class DatabaseOutputHandler : IOutputHandler
{
public void Write(Guid id, string data)
{
// Write to a database implementation
Console.WriteLine($"Database: {id} - {data}");
}
}`
c#
using System;

public interface IOutputHandler
{
void Write(Guid id, string data);
}

public class ConsoleOutputHandler : IOutputHandler
{
public void Write(Guid id, string data)
{
Console.WriteLine($"Console: {id} - {data}");
}
}

public class FileOutputHandler : IOutputHandler
{
public void Write(Guid id, string data)
{
// Write to a file implementation
Console.WriteLine($"File: {id} - {data}");
}
}

public class DatabaseOutputHandler : IOutputHandler
{
public void Write(Guid id, string data)
{
// Write to a database implementation
Console.WriteLine($"Database: {id} - {data}");
}
}`
65 replies