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. Cannot Create and Return an Instance of a Class

Cannot Create and Return an Instance of a Class

Scheduled Pinned Locked Moved C#
help
4 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.
  • 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 from ProjectA, set the values of its members, and return the instance object of this class 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 going to the Product class in ProjectA, adding the static keyword to the ID and Dept properties, and creating a static constructor. 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;

    The only problem I have now is I'm still getting an error stating that I cannot use an instance object to access members of the Product class on the line Product prod = new Models.Product();. Please point out what I'm doing wrong.

    Richard DeemingR 1 Reply Last reply
    0
    • L Liagapi

      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 from ProjectA, set the values of its members, and return the instance object of this class 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 going to the Product class in ProjectA, adding the static keyword to the ID and Dept properties, and creating a static constructor. 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;

      The only problem I have now is I'm still getting an error stating that I cannot use an instance object to access members of the Product class on the line Product prod = new Models.Product();. Please point out what I'm doing wrong.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      using static takes a fully-qualified type name, and makes the static members and nested types of that type available without prefixing them with the class name. For example:

      using static System.Console;
      ...
      WriteLine("Foo"); // Calls System.Console.WriteLine("Foo");

      using static directive - C# Reference | Microsoft Docs[^] Based on your description, ProjectA is a namespace; in which case, you just need using, not using static.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      L 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        using static takes a fully-qualified type name, and makes the static members and nested types of that type available without prefixing them with the class name. For example:

        using static System.Console;
        ...
        WriteLine("Foo"); // Calls System.Console.WriteLine("Foo");

        using static directive - C# Reference | Microsoft Docs[^] Based on your description, ProjectA is a namespace; in which case, you just need using, not using static.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

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

        Richard Deeming wrote:

        of that type available with prefixing them

        I think you meant "without".

        Richard DeemingR 1 Reply Last reply
        0
        • L Lost User

          Richard Deeming wrote:

          of that type available with prefixing them

          I think you meant "without".

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          :doh: Coffee deficiency strikes again!


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          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