Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,547 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,847 people online right now. Registration is fast and FREE... Join Now!




Access control from separate class

 
Reply to this topicStart new topic

Access control from separate class

maffelu
21 Aug, 2008 - 07:53 AM
Post #1

New D.I.C Head
*

Joined: 21 Aug, 2008
Posts: 37


My Contributions
Allright. I have a form with a combobox, call it cmb1. I created a method in a separate classfile, Class1.cs, and now when I want to add items to cmb1, located in Form1.cs I can't reach it, get the following error:

Error 1 The name 'cmb1' does not exist in the current context

What do I have to do to access controlls in form1.cs from Class1.cs.

I'm very new to C#. tongue.gif

User is offlineProfile CardPM
+Quote Post

SpiderSpartan
RE: Access Control From Separate Class
21 Aug, 2008 - 08:01 AM
Post #2

D.I.C Head
Group Icon

Joined: 6 Feb, 2008
Posts: 66



Thanked: 3 times
Dream Kudos: 25
My Contributions
I think you have to create an instance of the form in the class. Something like tempForm = new Form1. And then do something like tempForm.cmb1 to access it. I'm not exactly a C# expert.
User is offlineProfile CardPM
+Quote Post

maffelu
RE: Access Control From Separate Class
21 Aug, 2008 - 08:19 AM
Post #3

New D.I.C Head
*

Joined: 21 Aug, 2008
Posts: 37


My Contributions
Would you like to explain that more in detail?

Form1 TempForm = new Form1(); <--that would work if I could do TempForm.cmb1, but It won't work right now.
User is offlineProfile CardPM
+Quote Post

maffelu
RE: Access Control From Separate Class
21 Aug, 2008 - 08:52 AM
Post #4

New D.I.C Head
*

Joined: 21 Aug, 2008
Posts: 37


My Contributions
I'm gonna explain a bit better:

I have following:

Form1.cs
Class1.cs

On Form1.cs I have a combobox called "cmb1". On startup, I want to fil it with some values. So in the Form load I have the following code:

private void Form1_Load(object sender, EventArgs e)
{

Loader.ReaderMess();

}

Now, Loader.ReaderMess(); exists in Class1.cs and looks like this:

public static void ReaderMess()
{
cmb1.items.add("Test1");
}

This won't work, I get the following error:

Error 1
The name 'cmb1' does not exist in the current context

In VB.NET if I'm not misstaken, you only need to do 'Form1.cmb1.additem "test1"', so I thought maybe I could do the same here, but I can't, that's the problem.

It's really annoying, can't seem to find a good way to solve it.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Access Control From Separate Class
21 Aug, 2008 - 08:56 AM
Post #5

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
I would never reference a Form or a specific control in a class file, that defeats the purpose of OO programming. I would pass a reference to the ComboBox to the method, like this


csharp

public static void ReaderMess(ref ComboBox cmb)
{
cmb.items.add("Test1");
}




Then you can call it


csharp

Loader.ReaderMess(cmb1);

User is online!Profile CardPM
+Quote Post

maffelu
RE: Access Control From Separate Class
21 Aug, 2008 - 09:05 AM
Post #6

New D.I.C Head
*

Joined: 21 Aug, 2008
Posts: 37


My Contributions
That we pretty much what I was looking for.

BUT, it doesn't work all the way, I get an overload problem when I call it:

CODE
Loader.ReaderMess(cmb1);


Gives this error:

Error 1 The best overloaded method match for 'CascaLork.Loader.ReaderMess(ref System.Windows.Forms.ComboBox)' has some invalid arguments

Error 2 Argument '1' must be passed with the 'ref' keyword

Error 3 No overload for method 'ReaderMess' takes '0' arguments


User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Access Control From Separate Class
21 Aug, 2008 - 09:19 AM
Post #7

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
You're passing it by reference, so it needs to be

csharp

Loader.ReaderMess(ref cmb1);


My bad, forgot the ref in my original post
User is online!Profile CardPM
+Quote Post

maffelu
RE: Access Control From Separate Class
21 Aug, 2008 - 09:25 AM
Post #8

New D.I.C Head
*

Joined: 21 Aug, 2008
Posts: 37


My Contributions
Thank you PsychoCoder!!

You've saved my day!

I'm giving you huge kudos for this! I know it's basic stuff, but I'm quite new to this.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Access Control From Separate Class
21 Aug, 2008 - 09:27 AM
Post #9

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Glad I could help. In the future (not saying this because I helped) you should use the "This Post Was Helpful" link to let the person know that you appreciated their help smile.gif

Now, go forth and become a C# God smile.gif

EDIT: Oops, spoke too soon lol smile.gif
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 10:59PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month