Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Beginner: Return properties from another Class

Beginner: Return properties from another Class

Scheduled Pinned Locked Moved C#
csharpjsondatabasewinformshelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_15953027
    wrote on last edited by
    #1

    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.

    L D 2 Replies Last reply
    0
    • M Member_15953027

      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.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • M Member_15953027

        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.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups