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. How To Return an Instance of a Non-Static Class With Static Members

How To Return an Instance of a Non-Static Class With Static Members

Scheduled Pinned Locked Moved C#
helpquestiontutorial
1 Posts 1 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.
  • L Offline
    L Offline
    Liagapi
    wrote on last edited by
    #1

    Suppose in my project I'm referencing another project as follows:

    using static ProjectA;

    Prior to using the static keyword in the using statement, one of my methods was able to create an instance object of a class called Products, set the values of its members, and return the instance object as shown below:

    Product prod = new Models.Product();
    prod.ID = unit.ID;
    prod.Dept =unit.Department;
    return prod;

    Now I'm getting errors stating that I cannot use an instance object to access members of the Product class. I tried to resolve this issue by removing the line Product prod = new Models.Product(); and going to the Product class in ProjectA and adding the static keyword to the ID and Dept properties. I can now set values of the ID and Dept properties using the class name as shown below:

    Product.ID = unit.ID;
    Product.Dept =unit.Department;

    But the big problem now is I do not have the instance object of the Product class so I cannot return it and I get an error related to that. How do I return the Product class instance in this situation?

    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