Cannot Create and Return an Instance of a Class
-
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.
-
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.
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 needusing
, notusing static
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
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 needusing
, notusing static
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Richard Deeming wrote:
of that type available with prefixing them
I think you meant "without".
: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