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. C# OOP Question: Best way to setup these classes?

C# OOP Question: Best way to setup these classes?

Scheduled Pinned Locked Moved C#
tutorialquestioncsharpxmlhelp
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.
  • G Offline
    G Offline
    Goalie35
    wrote on last edited by
    #1

    I'm somewhat new to OOP & having a little trouble setting up some classes. Here's a quick little background on my project: -------------------------------------------- Basically, I need to create some objects for working with excel similar to that of the excel objects within the "Microsoft Office Object Library" (I'm using the ms open xml sdk kit, which seems to require a little more work than the ms object library). In particular, I'm looking to create 3 objects: -Workbook -Worksheet -Range -My Workbook class would contain methods such as "OpenWorkbook" and "Save". -My Worksheet class would contain methods such as "OpenWorksheet", "InsertWorksheet", and "CopyWorksheet". -My Range class would take a range of cells and contain methods for formatting that range such as "FontColor", "FontSize", etc. ----------------------------------------------------- My issue: I need to pass around properties from object to object but unsure on how to do so in the cleanest fashion. For example, within my Workbook class, I declare the following property: public WorkbookPart WbPart { get; set; } I will then need to use this "WbPart" property within my Worksheet class. For example, to insert a new worksheet, I need to use the following: WbPart.AddNewPart(); My question What's the best way to pass around these properties from class to class? Should I create a base class for this or something, and if so, how would I create this base class (I'm unfamiliar with base classes)? Thanks.

    B 1 Reply Last reply
    0
    • G Goalie35

      I'm somewhat new to OOP & having a little trouble setting up some classes. Here's a quick little background on my project: -------------------------------------------- Basically, I need to create some objects for working with excel similar to that of the excel objects within the "Microsoft Office Object Library" (I'm using the ms open xml sdk kit, which seems to require a little more work than the ms object library). In particular, I'm looking to create 3 objects: -Workbook -Worksheet -Range -My Workbook class would contain methods such as "OpenWorkbook" and "Save". -My Worksheet class would contain methods such as "OpenWorksheet", "InsertWorksheet", and "CopyWorksheet". -My Range class would take a range of cells and contain methods for formatting that range such as "FontColor", "FontSize", etc. ----------------------------------------------------- My issue: I need to pass around properties from object to object but unsure on how to do so in the cleanest fashion. For example, within my Workbook class, I declare the following property: public WorkbookPart WbPart { get; set; } I will then need to use this "WbPart" property within my Worksheet class. For example, to insert a new worksheet, I need to use the following: WbPart.AddNewPart(); My question What's the best way to pass around these properties from class to class? Should I create a base class for this or something, and if so, how would I create this base class (I'm unfamiliar with base classes)? Thanks.

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      I don't understand the purpose of the WookbookPart. The simple approach to this is that there are ownership relationships here (a sheet belongs to a book, and a range to a sheet), which implies classes like:

      class Workbook {
      List<Worksheet> Sheets;
      }

      class Worksheet {
      Workbook Owner;
      List<Range> Ranges;

      Cell GetCell(int x, int y);
      }

      class Range {
      Worksheet Owner;
      List<Cell> Cells;
      // formatting properties etc
      }

      class Cell {
      Worksheet Sheet;
      object Value;
      }

      I'm not sure that the list of ranges actually makes sense as ranges are dynamically created (correct?) so there isn't a fixed list to add to and read back from. I've given each class a reference to its owner, which can be null if you have 'orphaned' references (creating a sheet dynamically and not yet added to the book, etc). You'd then want to make sure that when adding things to the list in the parent, they had their Owner property updated accordingly.

      L 1 Reply Last reply
      0
      • B BobJanova

        I don't understand the purpose of the WookbookPart. The simple approach to this is that there are ownership relationships here (a sheet belongs to a book, and a range to a sheet), which implies classes like:

        class Workbook {
        List<Worksheet> Sheets;
        }

        class Worksheet {
        Workbook Owner;
        List<Range> Ranges;

        Cell GetCell(int x, int y);
        }

        class Range {
        Worksheet Owner;
        List<Cell> Cells;
        // formatting properties etc
        }

        class Cell {
        Worksheet Sheet;
        object Value;
        }

        I'm not sure that the list of ranges actually makes sense as ranges are dynamically created (correct?) so there isn't a fixed list to add to and read back from. I've given each class a reference to its owner, which can be null if you have 'orphaned' references (creating a sheet dynamically and not yet added to the book, etc). You'd then want to make sure that when adding things to the list in the parent, they had their Owner property updated accordingly.

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

        :thumbsup:

        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