5 Replies - 511 Views - Last Post: 04 November 2009 - 09:53 PM Rate Topic: -----

#1 Choscura  Icon User is offline

  • D.I.C Lover


Reputation: 466
  • View blog
  • Posts: 2,227
  • Joined: 18-October 08

dealing with files as instances of a class

Post icon  Posted 04 November 2009 - 12:06 PM

I've been doing a bit of homework for a pet project lately, and I'm having some trouble figuring something out... lets see if I can explain this correctly.

What I want to be able to do is list and sort through a lot of instances of one class inside the program, and I want to have those instances refer to actual files from outside the program- the problem is, how do I establish a way to name or otherwise identify new instances of this class? What I'd ideally like to do- and what seems to be impossible- is to name those instances after the files they represent in some way. Since this is presenting a larger problem than I'd like to deal with, I'm curious how I could otherwise go about this in a way that would still be organized and logical. how do jukebox programs handle the files they point to, for example? Does anybody know what iTunes does?

Thanks in advance for any info on this.

Is This A Good Question/Topic? 0
  • +

Replies To: dealing with files as instances of a class

#2 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: dealing with files as instances of a class

Posted 04 November 2009 - 12:22 PM

Quote

What I want to be able to do is list and sort through a lot of instances of one class inside the program, and I want to have those instances refer to actual files from outside the program- the problem is, how do I establish a way to name or otherwise identify new instances of this class? What I'd ideally like to do- and what seems to be impossible- is to name those instances after the files they represent in some way. Since this is presenting a larger problem than I'd like to deal with, I'm curious how I could otherwise go about this in a way that would still be organized and logical. how do jukebox programs handle the files they point to, for example? Does anybody know what iTunes does?


Ok, you're specifying implementation to a problem. What are you really trying to do? (Not how you want to do it)
I ask because perhaps when we identify your core problem, we might have a better approach to the solution, and it might be completely different from how you want to implement it.
Was This Post Helpful? 0
  • +
  • -

#3 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: dealing with files as instances of a class

Posted 04 November 2009 - 12:23 PM

Why not just have a member variable to the class hold the name?

As for new instances, just create a List<> to hold all of them.
Was This Post Helpful? 0
  • +
  • -

#4 Aeternalis  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: dealing with files as instances of a class

Posted 04 November 2009 - 01:39 PM

Agree with Momerath,


This kind of reminds me of the problem of storing pictures in a database, you don't need to actually store the picture in the database, you just need to store the path the picture is at in a field in the database.

So for your problem, Each object being an instance of a physical file instead of a picture, you could just store the file path and name of the file it represents in the object.
Class PhysFile 
{
public string Path
{ get;
   set;
}
public string Name
{get;
  set;
}
.
.
.
}




would this work for what you need?

Aet
Was This Post Helpful? 0
  • +
  • -

#5 Choscura  Icon User is offline

  • D.I.C Lover


Reputation: 466
  • View blog
  • Posts: 2,227
  • Joined: 18-October 08

Re: dealing with files as instances of a class

Posted 04 November 2009 - 07:40 PM

what I'm actually trying to do is create a list (using a listview) of files that can be sorted according to the values of different columns- the name of the file, the category it's in, the name of the creator, etc. this is all offline, no database programming for me (yet)
Was This Post Helpful? 0
  • +
  • -

#6 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: dealing with files as instances of a class

Posted 04 November 2009 - 09:53 PM

Ok, so you can have a file object that contains your properties. And then you can have a class that manages the collection of added files with methods to add/remove/etc. Maybe it could have methods to return in specific sort orders or maybe that will end up being responsibility of another class.

Something like this:

using System;
using System.Collections.Generic;

class SortableFile 
{
	public string FileName 
	{
		get;
		set;
	}

	public string Category 
	{
		get;
		set;
	}

	public string Creator 
	{
		get;
		set;
	}
}

class SortableFiles 
{
	private List<SortableFile> files;

	public SortableFiles() 
	{
		files = new List<SortableFile>();
	}

	public void AddFile(SortableFile file) 
	{
		files.Add(file);
	}

	public void AddFile(string fileName) 
	{
		AddFile(new SortableFile() { FileName = fileName });
	}

	public void PrintSortableFiles() 
	{
		foreach (SortableFile file in files) 
		{
			Console.WriteLine(file.FileName);
		}
	}
}

class Program 
{
	public static void Main() 
	{
		SortableFiles fileListing = new SortableFiles();
		fileListing.AddFile(new SortableFile() { FileName = "myFile.txt", Category = "Documentation", Creator = "MentalFloss" });
		fileListing.AddFile("mySong.mp3");

		fileListing.PrintSortableFiles();
		Console.ReadLine();
	}
}



Here is a sampling that allows adding a SortableFile to your file handler (SortableFiles) and then prints out all files in that collection.

This is by no means a completed concept though. I'm just kinda giving you a demo on how to get started.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1