Results 1 to 16 of 16

Thread: [RESOLVED] Creating an Instance and storing data

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2014
    Location
    Melbourne, Australia
    Posts
    69

    Resolved [RESOLVED] Creating an Instance and storing data

    So I want to have a corpus which has a list of each system (looking at folder structure there will be 4) and then each system will have any amount of versions (each version folder has a csv file that I need to store info from it)

    I am having problems working out if i am making instances of each right and if I will be able to do something along the lines of:
    Code:
    corpus.systems["systemname"]->versions["versionname]->getBytes();
    that line would give me the List<int> bytes; for that version/csv file.

    Code:
    class Corpus {
       List<System> systems;
       public string location;
    
       public Corpus(string locCorpus, string systemName) {
          systems.Add(new System(systemName);
          location = locCorpus;
       }
    }
    Code:
    class System {
       List<Version> versions;
       // store name of this system instance
       public string systemName;
    
       public System(string sysName, string versionName) {
          systemName = sysName;
          versions.Add(new Version(versionName);
       }
    
       public void addVersion(string versionName) {
          versions.Add(new Version(versionName);
       }
    }
    Code:
    // this class isn't fully done not sure how im storing the 
    // csv information yet but I am thinking lists. So each 
    // version has a List<> for each of its column data
    class Version {
       // list data holders
       // get/setters TODO
    
       public string version_name;
    
       public Version(string versionName) {
          version_name = versionName;
       }
    Any help would be great! Look forward to being told everything is wrong
    Last edited by sharpCode#; May 29th, 2014 at 07:58 AM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    May 2014
    Location
    Melbourne, Australia
    Posts
    69

    Re: Creating an Instance and storing data

    Just noticed should I be putting

    Code:
    = new List<XXXX>();
    on all the List's? also I think I might need Dictionaries instead unless I can do it with lists?

  3. #3
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,226

    Re: Creating an Instance and storing data

    I am of the opinion that you should prefer to call your System class by some other name. You can do it with lists. List is a powerful construct in .NET programming languages.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2014
    Location
    Melbourne, Australia
    Posts
    69

    Re: Creating an Instance and storing data

    I initialized the lists and seems to have fixed a few things. But how would I reference a list in the version instance?

    My aim is to be able to do something like

    Code:
    corpus.systems["systemname"]->versions["versionname]->getBytes();

  5. #5
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,226

    Re: Creating an Instance and storing data

    Could you please write your code here? Then hit RUN and then hit SHARE. Copy paste the link over. I am a bit confused by your last bit of code. I do believe that LINQ can help you here, but you need to provide some more code.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2014
    Location
    Melbourne, Australia
    Posts
    69

    Re: Creating an Instance and storing data

    Quote Originally Posted by abhijit View Post
    Could you please write your code here? Then hit RUN and then hit SHARE. Copy paste the link over. I am a bit confused by your last bit of code. I do believe that LINQ can help you here, but you need to provide some more code.
    Not sure how I would do it because that is just a sample in a PHP format. I want to know how to reference a version for a specific system in the systems List. And then eventually the data in the version.

    Also I think I might need to make the lists public?
    Last edited by sharpCode#; May 28th, 2014 at 09:13 PM.

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Creating an Instance and storing data

    w/o knowing what all is in your classes... this probably how I'd outline it:

    (note this is rough code, shooting from the hip and w/o the benefit of being run through the IDE)
    Code:
    Public Class Version
    ... put your properties and method here, like getBytes()
    End Class
    
    Public Class System
    Public Property Versions as Dictionary(Of String, Version)
    ... put the rest of your properties and methods here ...
    End Class
    
    Public Class Corpus
    Public Property Systems as Dictionary(Of String, System)
    ... put the rest of the properties and methods here ...
    End Class
    Then you can access things like this (once it is loaded).
    Code:
    Dim corpus as new Corpus
    
    corpus.systems("systemname").versions("versionname").getBytes()
    Of course, you'll want to actually assign it to something, rather than just calling getBytes ... assuming that getBytes is a function... I suppose it could be a method.


    But in a nutshell, that's the outline of the classes to get the format you want.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2014
    Location
    Melbourne, Australia
    Posts
    69

    Re: Creating an Instance and storing data

    Quote Originally Posted by techgnome View Post
    w/o knowing what all is in your classes... this probably how I'd outline it:

    (note this is rough code, shooting from the hip and w/o the benefit of being run through the IDE)
    Code:
    Public Class Version
    ... put your properties and method here, like getBytes()
    End Class
    
    Public Class System
    Public Property Versions as Dictionary(Of String, Version)
    ... put the rest of your properties and methods here ...
    End Class
    
    Public Class Corpus
    Public Property Systems as Dictionary(Of String, System)
    ... put the rest of the properties and methods here ...
    End Class
    Then you can access things like this (once it is loaded).
    Code:
    Dim corpus as new Corpus
    
    corpus.systems("systemname").versions("versionname").getBytes()
    Of course, you'll want to actually assign it to something, rather than just calling getBytes ... assuming that getBytes is a function... I suppose it could be a method.


    But in a nutshell, that's the outline of the classes to get the format you want.

    -tg
    Dictionaries? thought I might need them instead

    I will have a look at this on lunch break brought my code with me Ill try a few things and see how it goes then get back to you.

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Creating an Instance and storing data

    I went with the Dictionary because it allows you to set a key on the item when you add it, allowing you to use it to then retrieve it (using "systemname" and "versionname")

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Lively Member
    Join Date
    May 2014
    Location
    Melbourne, Australia
    Posts
    69

    Re: Creating an Instance and storing data

    Jumping onto this now I am working on a different part of the application now but if I remember correctly get/setting dictionaries requires a bit more work. Cause I might need to use dictionaries for the version data.

    Is it just the same usual way for getting but a separate setMethod() to push data into the dictionary?

    And a List<> would just be
    Code:
        public List<string> test
        {
            get
            { return _test; }
            set
            {
                _test.Add(value);
            }
        }

  11. #11
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Creating an Instance and storing data

    My bad, I didn't notice you were using C#, otherwise I would have posted a C# version of the code...

    For a list... not quite... typically when custom lists (and this goes for dictionaries too) are used in a class like this, the property is listed as ReadOnly. So only the Getter is implemented. Seems counter intuitive, but there's a reason for it. You don't want someone to create a new instance of your list and replace yours with theirs... you want them to use YOUR instance of it. So by making the property read only, you prevent them from doing something like this:
    Code:
    corpus.Systems = new List<System>;
    as that would destroy your internal list.

    But all that means is that the property can't be directly reset... it doesn't mean the list itself can't be added to or removed or otherwise edited... so you can still do this:

    Code:
    corpus.Systems.Add(new System); // adds a new system object to the list
    If systems is a Dictionary<String, System> then it works like this:
    Code:
    corpus.Systems.Add("Some New System", new System); // adds a new system object to the list
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12

    Thread Starter
    Lively Member
    Join Date
    May 2014
    Location
    Melbourne, Australia
    Posts
    69

    Re: Creating an Instance and storing data

    Should probably make another topic for this but I am sure it is simple.

    Code:
                var dir = corpus.location + "\\Systems\\" + system;
    
                string[] versionFolders = Directory.GetDirectories(dir);
    
                foreach (string version in versionFolders)
                {
                    corpus.systems[system].addVersion(version);
    
                }
                
                var count = versionFolders.Length;
                string[] files;
                for (int i=0; i >= count; i++)
                {
                     files[i] = Directory.GetFiles(dir + "\\" + versionFolders[i].ToString());
                }
    Last loop is reporting an error "Cannot implicitly convert type 'string[]' to 'string'" I just added the ToString()... was no help.

    files and versionFolders are both string[] though... im confused
    Last edited by sharpCode#; May 29th, 2014 at 07:21 AM.

  13. #13
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Creating an Instance and storing data

    Yes you should create a new topic... but the answer is because GetFiles returns a string array, and you're trying to stuff it into a single string.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14

    Thread Starter
    Lively Member
    Join Date
    May 2014
    Location
    Melbourne, Australia
    Posts
    69

    Re: Creating an Instance and storing data

    Quote Originally Posted by techgnome View Post
    Yes you should create a new topic... but the answer is because GetFiles returns a string array, and you're trying to stuff it into a single string.

    -tg
    Code:
    var count = versionFolders.Length;
    string[] files = new string[count];
    
    for (int i=0; i >= count; i++)
    {
      files = Directory.GetFiles(dir + "\\" + versionFolders[i]);
    }
                
    foreach (string file in files)
    {
       // ...
    }
    haven't tested it yet but no red lines yet.

  15. #15
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Creating an Instance and storing data

    Eeeeh... that's what I started to write, but then realized it doesn't ADD to files... it will over write it each time... so the only thing you'll get in files will be what's in the last folder it looked at....


    OOOH... I know what will work:
    Code:
    List<String> files = new List<String>;
    for (int i=0; i >= count; i++)
    {
      files.AddRange(Directory.GetFiles(dir + "\\" + versionFolders[i]));
    }
    NOW you can iterate through the files list using a for each, or a for next, and it will contain all of the files in all of the directories.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  16. #16

    Thread Starter
    Lively Member
    Join Date
    May 2014
    Location
    Melbourne, Australia
    Posts
    69

    Re: Creating an Instance and storing data

    Gonna make a new topic cause I am having a bit of a problem now. I'll leave this one for the lists which I will have to come back to.

    RESOLVED: All settled. I ended up using DirectoryInfo and then the EnumerateDirectories() and EnumerateFiles() methods to get files and directories that worked well.

    As for the lists<> got them working and get/set(ing) perfectly though I really should decouple that section of the application that is the next learning curve for another time
    Last edited by sharpCode#; May 30th, 2014 at 01:54 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width