Avatar of Sheldon Livingston
Sheldon Livingston
Flag for United States of America asked on

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?
C#Visual C++.NET

Avatar of undefined
Last Comment
Sheldon Livingston

8/22/2022 - Mon
p_davis

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();
Sheldon Livingston

ASKER
static didn't work... I get an "inaccessible due to its protection level".

I'll now try to instantiate it.
ASKER CERTIFIED SOLUTION
p_davis

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
p_davis

not stating public implies private.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Sheldon Livingston

ASKER
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
p_davis

show me the code you have
Sheldon Livingston

ASKER
Thank you!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
AndyAinscow

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.
Sheldon Livingston

ASKER
Thanks AndyAinscow...

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

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
mmsi

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;
Sheldon Livingston

ASKER
Thanks everyone.