[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

C# classes

Just starting here...

I have a project with a Form1.cs and a newly added myClass.cs.

How can I access a routine in myClass from Form1?
0
classnet
Asked:
classnet
  • 5
  • 4
  • 2
  • +1
1 Solution
 
p_davisCommented:
you either need to instantiate a new instance of your object or you need to make the method static.

1)
myClass  mClass = new myClass();
mClass.YourMethodCall();

2)
//in your class
static void YourMethodCall()
{

}

//in the form
myClass.YourMethodCall();
0
 
classnetAuthor Commented:
static didn't work... I get an "inaccessible due to its protection level".

I'll now try to instantiate it.
0
 
p_davisCommented:
sorry

//in your class
public static void YourMethodCall()
{

}
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
p_davisCommented:
not stating public implies private.
0
 
classnetAuthor Commented:
Instantiation didn't work... any other ideas?

FYI... Changing static to public still doesn't work.

The only dot operators for my class are .Equals and .ReferenceEquals
0
 
p_davisCommented:
show me the code you have
0
 
classnetAuthor Commented:
Thank you!
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
p_davis has answered your question.


Look up (and learn about) public, protected and private in the help files.  I would also strongly advise purchasing a book to help you learn C#.  Just doing things at random is not a good idea when you don't know the very basics of the language.
0
 
classnetAuthor Commented:
Thanks AndyAinscow...

I'm an old VB6 and .asp programmer... I find it hard to get a book and go cover to cover...
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
That I can understand.
The problem is that you need to know the very basics first.  (Ever try building a house starting with the attic?).  Learning by doing is good but you can save yourself a lot of time and effort in the long run by reading about the basics - say the first few chapters.  Then look in later chapters / index as you attempt something.
0
 
mmsiCommented:
What I usually do is.

1.  is make the methods in the class public static.
2.  on the top of form1, where the directives are put - using [project name];

for example if my project is called "TestApp" I would put:
using TestApp;

Then you can access the public static methods by calling your class.

testCalss.TestMethod();


If you have your class in a folder called "Classes" you would put this at the top of your form:

using TestApp.Classes;
0
 
classnetAuthor Commented:
Thanks everyone.
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now