C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

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




call an external function on button click

 

call an external function on button click

rqmurad

30 Apr, 2009 - 01:21 AM
Post #1

New D.I.C Head
*

Joined: 26 Apr, 2009
Posts: 2

i am doing widows programming in c#
i wrote all my code in a different codefile named baseclass.cs
the form1 has its own codefile form1.cs
i cannot access any member of baseclass.cs codefile

if there is a button on form1, how i can run a function of baseclass.cs codefile with buttonclik?

how i can view a value on form 1



Attached thumbnail(s)
Attached Image
User is offlineProfile CardPM
+Quote Post


b.ihde

RE: Call An External Function On Button Click

30 Apr, 2009 - 02:42 AM
Post #2

D.I.C for life
Group Icon

Joined: 29 Sep, 2008
Posts: 973



Thanked: 30 times
Dream Kudos: 75
My Contributions
hi and welcome to DreamInCode!

Please show us what you have tried so far?
And please post also the code from your form1 smile.gif

Thanks

Ben
User is offlineProfile CardPM
+Quote Post

InternalStatic

RE: Call An External Function On Button Click

30 Apr, 2009 - 12:31 PM
Post #3

New D.I.C Head
*

Joined: 28 Apr, 2009
Posts: 11



Thanked: 2 times
My Contributions
QUOTE(rqmurad @ 30 Apr, 2009 - 01:21 AM) *

i am doing widows programming in c#
i wrote all my code in a different codefile named baseclass.cs
the form1 has its own codefile form1.cs
i cannot access any member of baseclass.cs codefile

if there is a button on form1, how i can run a function of baseclass.cs codefile with buttonclik?

how i can view a value on form 1


Visual Studio is actually smart enough to "pretend" that all of your code that resides in your project is actually part of the same file. So, to answer your question, imply call the functions you wish to access as if they resided in the current file! Visual Studio compiles all files in the same project as one.

Side note: Normal convention is to split up your classes into different files. Usually, every class has its own file, though you may lump them all together in one (I don't recommend this). Also, when naming your namespaces and classes, use what we call Pascal casing. All that means is that the first letter of EVERY word is capitalized. For example:

namespace PascalCasingNamespaceExample
{
public class PascalCasingClassExample
{ }
}

Hope that helped.
User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: Call An External Function On Button Click

30 Apr, 2009 - 01:16 PM
Post #4

Code Guru
Group Icon

Joined: 18 Oct, 2008
Posts: 2,905



Thanked: 165 times
Dream Kudos: 725
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
This thread might help you:

http://www.dreamincode.net/forums/showtopic99852.htm
User is offlineProfile CardPM
+Quote Post

PsychoCoder

RE: Call An External Function On Button Click

30 Apr, 2009 - 01:56 PM
Post #5

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,701



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

My Contributions
This is a instance of nesting classes. The easiest way to accomplish would be to make the inner classes protected Take a look at this example (using your class names)

CODE

namespace MyNamespace
{
    /// <summary>
    /// Description of Class1.
    /// </summary>
    public class baseclass
    {
        protected component myComponent;
        protected rescoeff res;
        protected coefficients coef;
        protected design_calculate designCalc
            
        //constructor for the outer class
        public baseclass()
        {
            myComponent = new component();
            res = new rescoeff();
            coef = new coefficients();
            designCalc = new design_calculate();
        }
        
        protected class component
        {
            //class code here
        }
        
        protected class rescoeff
        {
            //class code here
        }
        
        protected class coefficients
        {
            //class code here
        }
        
        protected class design_calculate
        {
            //class code here
        }
    }
}


Then in every class you want to access these inherit from baseclass

CODE

public class MyClass : baseclass
{
         //in our constructor we will access another class
        public MyClass()
        {
               //now you can access the nested class like so
               // notice that myComponent is a protected member of component
              myComponent.SomeFunction = "BlahBlah";
         }
}


Hope that makes sense smile.gif
User is offlineProfile CardPM
+Quote Post

rossporter

RE: Call An External Function On Button Click

30 Apr, 2009 - 09:19 PM
Post #6

New D.I.C Head
*

Joined: 30 Apr, 2009
Posts: 9



Thanked: 1 times
My Contributions
QUOTE(rqmurad @ 30 Apr, 2009 - 01:21 AM) *

i am doing widows programming in c#
i wrote all my code in a different codefile named baseclass.cs
the form1 has its own codefile form1.cs
i cannot access any member of baseclass.cs codefile

if there is a button on form1, how i can run a function of baseclass.cs codefile with buttonclik?

how i can view a value on form 1


Simple issue of class visibility. The default access modifier of a C# class is private. Your class "baseclass" is therefore private as it is not declared for public, protected, or internal access.

Since baseclass does not contain any state and appears to be only a container for other public classes, I would suggest you use a namespace to contain the other classes rather than a class. If you have another reason to make this a class, then I would suggest changing the declaration to either
CODE

     public class baseclass
     //OR
     internal class baseclass


public classes are visibile to all code, outside and inside of the assembly (i.e. project).

internal classes are visible to any code within the assembly.

protected classes are visible only to other classes which inherit from their container class and cannot be directly declared in a namespace.

protected internal classes are visible to the current assembly and to any classes which inherit from their container class. They also cannot be directly delcared in a namespace.

Hope it helps
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 03:49PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month