Solved

C# classes

Posted on 2012-12-27
12
331 Views
Last Modified: 2012-12-27
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
Comment
Question by:classnet
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 38723594
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
 

Author Comment

by:classnet
ID: 38723622
static didn't work... I get an "inaccessible due to its protection level".

I'll now try to instantiate it.
0
 
LVL 22

Accepted Solution

by:
p_davis earned 500 total points
ID: 38723631
sorry

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

}
0
 
LVL 22

Expert Comment

by:p_davis
ID: 38723633
not stating public implies private.
0
 

Author Comment

by:classnet
ID: 38723636
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
 
LVL 22

Expert Comment

by:p_davis
ID: 38723637
show me the code you have
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Closing Comment

by:classnet
ID: 38723638
Thank you!
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38723640
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
 

Author Comment

by:classnet
ID: 38723651
Thanks AndyAinscow...

I'm an old VB6 and .asp programmer... I find it hard to get a book and go cover to cover...
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38723664
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
 

Expert Comment

by:mmsi
ID: 38723668
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
 

Author Comment

by:classnet
ID: 38723687
Thanks everyone.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now