• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

C# Class creation to manage code in seperate class files

I am looking to understand how to manage multiple class files that will work within one application / program in C#, .net.

I have created a seperate class outside of my normal program.cs class. However, I can't seem to access the properties or methods within the seperate class inside the program.cs class.

I don't want to start creating many classes within the same main class of the program.

Thanks
Greg.
0
gcastong
Asked:
gcastong
2 Solutions
 
käµfm³d 👽Commented:
I have created a seperate class outside of my normal program.cs class. However, I can't seem to access the properties or methods within the seperate class inside the program.cs class.
Is the class marked as static? If not, then you will need to work with an instance of the class (e.g. MyClass obj = new MyClass() ). Also make sure that you haven't inadvertently changed the namespace of the classes; or if you intentionally changed them, make sure you are importing the correct namespace.
0
 
strickddCommented:
Make sure that the properties are marked as public and not private. Also make sure that you instantiate the object of the class type.

public class MyClass
{
    public string MyString { get; set; }

    public MyClass() { }
}

In program.cs:

MyClass myObject = new MyClass();
myOjbect.MyString = "new value";
0
 
SriVaddadiCommented:
make the class public and instantiate the class. Also, the methods and properties have to be public
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now