Solved

C# classes

Posted on 2012-12-27
12
338 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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
 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

623 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