Solved

C# classes

Posted on 2012-12-27
12
332 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ConsoleSql 1 34
How to write a select year(max(datefield) in EntitySQL in  c# entity framework 12 29
Name space syntax error 12 44
C# parent child form 5 19
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

867 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

21 Experts available now in Live!

Get 1:1 Help Now