TimTheEnchanter TimTheEnchanter - 1 year ago 59
Android Question

Generating variables when a program is running? C#

Hello everyone and thanks for checking this post!

I will give you a run down of what my program is trying to achieve first.

So I want to try try and make a program that can get a youtube playlist load all the videos on it and for each one create a variable called view count (name of video)! The amount of variables that are created has to be dynamic acording to the amount of videos. Once this is done the values from each variable are then saved to a local database so that the program can keep track even when closed.

What I am stuck on!

I can not find a way to generate multiple variables without actually coding each one in! There must be a way of seeing the amount of videos and then getting each ones name and creating a new int for each video with that information. Then each time the link for that video is accessed it adds one to the int.

Some helpfull information!

The Xpath for the title of each youtube video is the same being...


The Xpath for the text that displays how many videos are on a playlist is...


Thanks so much for helping me with this problem!

Answer Source

As SLaks mentions, arrays and collections are basic programming concepts that are used to deal with collections of information. If you have 1,000 values, you don't create a single variable for each one, you use a collection or array to manage them instead. Here is an overview of this concept in C#

In C#, you could do this with a Dictionary<string,int>

// initialize the collection
Dictionary<string,int> videos = new Dictionary<string,int>();

// add a video
// where key is a string with the unique name of the video, and value is an
// int representing the viewcount

// get the number of videos in your collection
int count = videos.Count();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download