Solved

C# classes

Posted on 2012-12-27
12
337 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio hangs on running project 6 85
C# Linq Select From List 3 141
Need help with another query 10 54
Health check of winows and webservces 5 52
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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