Beginner: Return properties from another Class
-
Hi! I'm a beginner in C# and need some help. In Winforms I've got a button. When I press the button I'd like to get some data from TMDB and insert it to MS SQL. I've redacted some properties to cut down unnecessary properties. This is what I've got: Forms.cs:
public void btn\_GetNewTVShowData\_Click(object sender, EventArgs e) { GetTMDB GetData = new GetTMDB(); GetData.GetTvShowData(); }
DataClasses\Common\GetTMDB.cs
DataClasses\Common\GetTMDB.cs
namespace WinFormsApp1.DataClasses.Common
{
internal class GetTMDB
{
public async Task GetTvShowData()
{HttpClient client = new HttpClient(); var content = await client.GetStringAsync("https://api.themoviedb.org/3/tv/136311?api\_key=XXXXXXX&language=en-US"); TVShow myDeserializedClass = JsonConvert.DeserializeObject(content); return object myDeserializedClass; <-- Not working, I know } public class TVShow { public string backdrop\_path { get; set; } = default!; public List created\_by { get; set; } = default!; public string name { get; set; } = default!; } public class CreatedBy { public int id { get; set; } = default!; public string credit\_id { get; set; } = default!; public string name { get; set; } = default!; public int gender { get; set; } = default!; public string profile\_path { get; set; } = default!; } }
}
DataClasses\Common\DataAccess.cs (psedocode, just for showing I want it in seperate class)
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
So, I want to press a button, use the class GetTMDB, get the json from TMDB, Serialize it (with NewtonSoft.json), return the data back to my forms1.cs (I guess? Or should I call DataAccess.cs?). Is it possible to let another class access the properties? Let's say I want multiple classes to access the Properties to do different kind of stuff with my properties.
-
Hi! I'm a beginner in C# and need some help. In Winforms I've got a button. When I press the button I'd like to get some data from TMDB and insert it to MS SQL. I've redacted some properties to cut down unnecessary properties. This is what I've got: Forms.cs:
public void btn\_GetNewTVShowData\_Click(object sender, EventArgs e) { GetTMDB GetData = new GetTMDB(); GetData.GetTvShowData(); }
DataClasses\Common\GetTMDB.cs
DataClasses\Common\GetTMDB.cs
namespace WinFormsApp1.DataClasses.Common
{
internal class GetTMDB
{
public async Task GetTvShowData()
{HttpClient client = new HttpClient(); var content = await client.GetStringAsync("https://api.themoviedb.org/3/tv/136311?api\_key=XXXXXXX&language=en-US"); TVShow myDeserializedClass = JsonConvert.DeserializeObject(content); return object myDeserializedClass; <-- Not working, I know } public class TVShow { public string backdrop\_path { get; set; } = default!; public List created\_by { get; set; } = default!; public string name { get; set; } = default!; } public class CreatedBy { public int id { get; set; } = default!; public string credit\_id { get; set; } = default!; public string name { get; set; } = default!; public int gender { get; set; } = default!; public string profile\_path { get; set; } = default!; } }
}
DataClasses\Common\DataAccess.cs (psedocode, just for showing I want it in seperate class)
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
So, I want to press a button, use the class GetTMDB, get the json from TMDB, Serialize it (with NewtonSoft.json), return the data back to my forms1.cs (I guess? Or should I call DataAccess.cs?). Is it possible to let another class access the properties? Let's say I want multiple classes to access the Properties to do different kind of stuff with my properties.
You have way too many things going on (for a "beginner"), and that are wrong. You need to pick some aspect and ask for help on that instead of asking for what amounts to a course in C# programming and systems design.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
Hi! I'm a beginner in C# and need some help. In Winforms I've got a button. When I press the button I'd like to get some data from TMDB and insert it to MS SQL. I've redacted some properties to cut down unnecessary properties. This is what I've got: Forms.cs:
public void btn\_GetNewTVShowData\_Click(object sender, EventArgs e) { GetTMDB GetData = new GetTMDB(); GetData.GetTvShowData(); }
DataClasses\Common\GetTMDB.cs
DataClasses\Common\GetTMDB.cs
namespace WinFormsApp1.DataClasses.Common
{
internal class GetTMDB
{
public async Task GetTvShowData()
{HttpClient client = new HttpClient(); var content = await client.GetStringAsync("https://api.themoviedb.org/3/tv/136311?api\_key=XXXXXXX&language=en-US"); TVShow myDeserializedClass = JsonConvert.DeserializeObject(content); return object myDeserializedClass; <-- Not working, I know } public class TVShow { public string backdrop\_path { get; set; } = default!; public List created\_by { get; set; } = default!; public string name { get; set; } = default!; } public class CreatedBy { public int id { get; set; } = default!; public string credit\_id { get; set; } = default!; public string name { get; set; } = default!; public int gender { get; set; } = default!; public string profile\_path { get; set; } = default!; } }
}
DataClasses\Common\DataAccess.cs (psedocode, just for showing I want it in seperate class)
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
So, I want to press a button, use the class GetTMDB, get the json from TMDB, Serialize it (with NewtonSoft.json), return the data back to my forms1.cs (I guess? Or should I call DataAccess.cs?). Is it possible to let another class access the properties? Let's say I want multiple classes to access the Properties to do different kind of stuff with my properties.
First, you're return type is a Task, not what you're really returning, so you have to fix that. Next, the return statement just needs the "object" part removed. Lastly, the other answer is correct. You're jumping in the deep end without knowing how to swim. This is NOT tested, so don't go expecting this to work immediately:
public async TVShow GetTvShowData()
{
HttpClient client = new HttpClient();var content = await client.GetStringAsync("https://api.themoviedb.org/3/tv/136311?api\_key=XXXXXXX&language=en-US"); TVShow myDeserializedClass = JsonConvert.DeserializeObject(content); return myDeserializedClass;
}
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles. Dave Kreskowiak