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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Need help with a Struct or Class (array)

Need help with a Struct or Class (array)

Scheduled Pinned Locked Moved C#
helpcsharpdatabasedata-structures
7 Posts 3 Posters 1 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.
  • F Offline
    F Offline
    flicktom
    wrote on last edited by
    #1

    I have a C# problem that I need a solution for. I've pretty much mastered in in VB6, but keep running into brick walls, even with all the tutorials for C#. Here's the definition of the variables you're about to see... "ClassStudents" is a class I created to manage an array of students read in from... "LogReader" This is the name given to the SQL select statement that returns the information I'm trying to use. "logCount" is simply to keep the array element in check. while (LogReader.Read()) { ClassStudents[] Students = new ClassStudents[logCount+1]; Students[logCount].setKnown(LogReader.GetInt32(0), LogReader.GetString(1)..... logCount++; } Basically, I need to be able to populate an array of what in VB6 would be a simple 'Type' so I can go back to it for data 'after' the fact. Everything's fine until I leave the while loop, at which point I seem to lose the ability to reference the "Students" array I created. I tried using Structs as well. I know it must be a fundamental rule I'm breaking, but I just can't find any reference to what is happening in any documentation or help files. I found a post basically outlining the method I'm using, but they stop short of any reason why I wouldn't be able to access the data outside of where it was populated. Kinda defeats the purpose, if you ask me.

    M G 2 Replies Last reply
    0
    • F flicktom

      I have a C# problem that I need a solution for. I've pretty much mastered in in VB6, but keep running into brick walls, even with all the tutorials for C#. Here's the definition of the variables you're about to see... "ClassStudents" is a class I created to manage an array of students read in from... "LogReader" This is the name given to the SQL select statement that returns the information I'm trying to use. "logCount" is simply to keep the array element in check. while (LogReader.Read()) { ClassStudents[] Students = new ClassStudents[logCount+1]; Students[logCount].setKnown(LogReader.GetInt32(0), LogReader.GetString(1)..... logCount++; } Basically, I need to be able to populate an array of what in VB6 would be a simple 'Type' so I can go back to it for data 'after' the fact. Everything's fine until I leave the while loop, at which point I seem to lose the ability to reference the "Students" array I created. I tried using Structs as well. I know it must be a fundamental rule I'm breaking, but I just can't find any reference to what is happening in any documentation or help files. I found a post basically outlining the method I'm using, but they stop short of any reason why I wouldn't be able to access the data outside of where it was populated. Kinda defeats the purpose, if you ask me.

      M Offline
      M Offline
      Marc 0
      wrote on last edited by
      #2

      Hope i get this straight.... You want to get an array with ClassStudents, and then fill it using LogReader? Ok, let me try:

      // Make an arraylist because you don't know the number of students you have
      // In .Net 2, you can do List<ClassStudent> or something (then you don't have to cast)
      ArrayList students = new ArrayList();
      int logCount = 0;

      while (LogReader.Read()){
      // Make a new student
      ClassStudent student = new ClassStudent(logCount);
      // Fill it with data
      student.SetKnown(LogReader.GetInt32(0), LogReader.GetString(1));
      // Now add the new student to the students arraylist
      students.Add(student);
      logCount++;
      }
      // Now students is filled with the students (duh), and you can still reference it
      // That's because you've created the students arraylist outside the while loop

      // Now you can get a student like this:
      // With ArrayList you have to cast from object to a ClassStudent
      // In .Net 2 you dont have to do that with List<ClassStudent> or something
      ClassStudent firstStudent = (ClassStudent)students(0);

      And structs or classes don't really matter for this, the differences between those are completely different. HTH! :) Pompiedompiedom... ;)


      "..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick

      F 1 Reply Last reply
      0
      • F flicktom

        I have a C# problem that I need a solution for. I've pretty much mastered in in VB6, but keep running into brick walls, even with all the tutorials for C#. Here's the definition of the variables you're about to see... "ClassStudents" is a class I created to manage an array of students read in from... "LogReader" This is the name given to the SQL select statement that returns the information I'm trying to use. "logCount" is simply to keep the array element in check. while (LogReader.Read()) { ClassStudents[] Students = new ClassStudents[logCount+1]; Students[logCount].setKnown(LogReader.GetInt32(0), LogReader.GetString(1)..... logCount++; } Basically, I need to be able to populate an array of what in VB6 would be a simple 'Type' so I can go back to it for data 'after' the fact. Everything's fine until I leave the while loop, at which point I seem to lose the ability to reference the "Students" array I created. I tried using Structs as well. I know it must be a fundamental rule I'm breaking, but I just can't find any reference to what is happening in any documentation or help files. I found a post basically outlining the method I'm using, but they stop short of any reason why I wouldn't be able to access the data outside of where it was populated. Kinda defeats the purpose, if you ask me.

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        You are making three errors in that loop: :: You are creating the array inside the loop. That means that the scope of the array is the loop, so you can't use it outside the loop. :: You are creating a new array for each iteration. That means that the previous array is discarded along with the previos item. After the loop you will have an array where every item is null except the last one. :: You are not creating any ClassStudents objects. That means that you can't call the setKnown method as the reference that you try to use to make the call (the item in the array) is null. --- b { font-weight: normal; }

        M 1 Reply Last reply
        0
        • G Guffa

          You are making three errors in that loop: :: You are creating the array inside the loop. That means that the scope of the array is the loop, so you can't use it outside the loop. :: You are creating a new array for each iteration. That means that the previous array is discarded along with the previos item. After the loop you will have an array where every item is null except the last one. :: You are not creating any ClassStudents objects. That means that you can't call the setKnown method as the reference that you try to use to make the call (the item in the array) is null. --- b { font-weight: normal; }

          M Offline
          M Offline
          Marc 0
          wrote on last edited by
          #4

          Guffa wrote:

          :: You are not creating any ClassStudents objects. That means that you can't call the setKnown method as the reference that you try to use to make the call (the item in the array) is null.

          I think that's why he tried structs instead :). Pompiedompiedom... ;)


          "..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick

          1 Reply Last reply
          0
          • M Marc 0

            Hope i get this straight.... You want to get an array with ClassStudents, and then fill it using LogReader? Ok, let me try:

            // Make an arraylist because you don't know the number of students you have
            // In .Net 2, you can do List<ClassStudent> or something (then you don't have to cast)
            ArrayList students = new ArrayList();
            int logCount = 0;

            while (LogReader.Read()){
            // Make a new student
            ClassStudent student = new ClassStudent(logCount);
            // Fill it with data
            student.SetKnown(LogReader.GetInt32(0), LogReader.GetString(1));
            // Now add the new student to the students arraylist
            students.Add(student);
            logCount++;
            }
            // Now students is filled with the students (duh), and you can still reference it
            // That's because you've created the students arraylist outside the while loop

            // Now you can get a student like this:
            // With ArrayList you have to cast from object to a ClassStudent
            // In .Net 2 you dont have to do that with List<ClassStudent> or something
            ClassStudent firstStudent = (ClassStudent)students(0);

            And structs or classes don't really matter for this, the differences between those are completely different. HTH! :) Pompiedompiedom... ;)


            "..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick

            F Offline
            F Offline
            flicktom
            wrote on last edited by
            #5

            Ok. That 'kind of' works... I was able to declare the ArrayList "students", create a new class object "student", pass the values into the object "student" (as far as compile time goes.. can't really test it because of the following...) and add the object "student" to the ArrayList of "students". Ok so far. Now when I try to use the intellisense for "students." I don't get any of the functions that I need to be able to call from the class object that it is comprised of. Pretty pointless if I can't use the functions the whole class was designed for. Any suggestions? Here's a summary of what I need to accomplish... 1. read multiple records (with multiple fields of varying data types, of course) from a table in a database into an array of some sort. 2. While reading the rows, populate a listbox with a built sting with some of the information (but not all) // This much I can handle all well and dandy 3. I need to be able to pull up various elements from the 'full' array of data for a given line when a user clicks on one of the corresponding "index number" line in the listbox. 4. I had this licked with a handy-dandy "type" variable array in VB6, but C# is just TOTALLY not going to settle for the same approach. I need some advice on the best way to approach this problem. I've wasted over 8 hours already, trying to find a means for something that took me a whopping 20 minutes in VB6, and I feel no closer now than when I started.:sigh:

            G 1 Reply Last reply
            0
            • F flicktom

              Ok. That 'kind of' works... I was able to declare the ArrayList "students", create a new class object "student", pass the values into the object "student" (as far as compile time goes.. can't really test it because of the following...) and add the object "student" to the ArrayList of "students". Ok so far. Now when I try to use the intellisense for "students." I don't get any of the functions that I need to be able to call from the class object that it is comprised of. Pretty pointless if I can't use the functions the whole class was designed for. Any suggestions? Here's a summary of what I need to accomplish... 1. read multiple records (with multiple fields of varying data types, of course) from a table in a database into an array of some sort. 2. While reading the rows, populate a listbox with a built sting with some of the information (but not all) // This much I can handle all well and dandy 3. I need to be able to pull up various elements from the 'full' array of data for a given line when a user clicks on one of the corresponding "index number" line in the listbox. 4. I had this licked with a handy-dandy "type" variable array in VB6, but C# is just TOTALLY not going to settle for the same approach. I need some advice on the best way to approach this problem. I've wasted over 8 hours already, trying to find a means for something that took me a whopping 20 minutes in VB6, and I feel no closer now than when I started.:sigh:

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              As an ArrayList contains reference to the type Object, you have to type cast the item to use it: ((student)students[42]).MyMethod(); --- b { font-weight: normal; }

              F 1 Reply Last reply
              0
              • G Guffa

                As an ArrayList contains reference to the type Object, you have to type cast the item to use it: ((student)students[42]).MyMethod(); --- b { font-weight: normal; }

                F Offline
                F Offline
                flicktom
                wrote on last edited by
                #7

                Great. That worked (with a minor edit... needed to replace student with ClassStudent). A recap for any that browse for a solution of this nature... // This, I actually placed after the "System Generated Object Declarations" for increased scope. ArrayList students = new ArrayList(); // The while loop... while (LogReader.Read()) { // Make a new student ClassStudent student = new ClassStudent(); // Fill it with data (Added some nullchecking since original post.. not shown) student.SetKnown(LogReader.GetInt32(0), LogReader.GetString(1)); // Now add the new student to the students arraylist students.Add(student); // Now students is filled with each student (duh), and you can still reference it // Now you can get a student like this: // With ArrayList you have to cast from object to a ClassStudent (my apologies for not catching this... thanks for pointing it out in more detail, Guffa :) ) // Can use this anywhere you like.. ((ClassStudent)students[indexNum]).MyMethod(); Thanks a "metric buttload" to both of you.

                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