call an external function on button click
Page 1 of 1
call an external function on button click
#1
Posted 30 April 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
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
#3
Posted 30 April 2009 - 12:31 PM
rqmurad, on 30 Apr, 2009 - 01:21 AM, said:
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
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.
#4
Posted 30 April 2009 - 01:16 PM
#5
Posted 30 April 2009 - 01:56 PM
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)
Then in every class you want to access these inherit from baseclass
Hope that makes sense
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
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
#6
Posted 30 April 2009 - 09:19 PM
rqmurad, on 30 Apr, 2009 - 01:21 AM, said:
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
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
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
Page 1 of 1

Start a new topic
Add Reply




MultiQuote



| 


