Link to home
Start Free TrialLog in
Avatar of dkloeck
dkloeckFlag for Spain

asked on

calling method

hi there!,

I need to call a function in a class SF on a variable which i can not get to.

i can call the class but not his instance (variable).

Is there a way to call the method through static fields, events or something?

you can add as much code as you want on the class.

thanks in advance!
dkloeck
Avatar of Fahad Mukhtar
Fahad Mukhtar
Flag of Pakistan image

you can define that method as static
Avatar of MyersA
MyersA

I'm not exactly sure what you mean, but this are some possible scenarios:
You can create private member (variable) and access it through the Get/Set accessors. I can't access _myVariable unless I go through MyVariable property.
You can also create a private method and only access it through a public method. If you want to make the class static (can't instantiate), then anything else inside would also need to be static.  Just add keyword static to the Class_test, MyVariable, _myVariable, and the method declarations.

class Class_test
{
    private string _myVariable;
    public string MyVariable
    {
        get    //Get accessor
        {
            return MyVariable;
        }
        set  //Set accessor
        {
            _myVariable = value;
            System.Windows.Forms.MessageBox.Show(_myVariable);

        }
    }
    private void PrivateMethod(string sTest)
    {
        _myVariable = sTest;
    }

    public void callPrivateMethod(string sTest)
    {
        PrivateMethod(sTest);
    }
}
Avatar of dkloeck

ASKER

i forgot to say 2 things:

1. the method I want to call is not and can not be static

2. i can not get the variable, so i have to call it through some static stuff. I mean i can not do something like myVaiable.callMethod() because i can not get to the variable, i only can use the class like myClass.myStaticMethod() and that static method have to call my non static method or something .. I really don't know how it must be done.

There is another solution to what MyersA posted. (MyersA, I used your code to clearly show the differences between our ways)

class Class_test
{
   public Delegate void FunctionCall(string s);
    private string _myVariable;
    public string MyVariable
    {
        get    //Get accessor
        {
            return MyVariable;
        }
        set  //Set accessor
        {
            _myVariable = value;
            System.Windows.Forms.MessageBox.Show(_myVariable);

        }
    }
    private void PrivateMethod(string sTest)
    {
        _myVariable = sTest;
    }

    public void callPrivateMethod(FunctionCall function)
    {
        function(sTest);
    }
}

This is a solution with a delegate and it allows you to perform any action on the string without the class knowing which method.
Avatar of dkloeck

ASKER

the class knows which method, but its the method of an instand i can not access to
Avatar of dkloeck

ASKER

instance*
Avatar of dkloeck

ASKER

i will do an example for you to know what i really need.

lets say I have a class like this:

class MyClass
{
string uniqueInstanceString;

public void invertString()
{
  uniqueInstanceString.Invert();
}
}


somewhere very very far away in another dimension and project a lifeform calls:

MyClass veryPowerfullInstance = "987654321";

and I need to do somethig from a place where veryPowerfullInstance does not exist to call the invert method in that instance
I of course know that veryPowerfullInstance is the only instance of this class that exists.

fell free to create a new instance to change it, or create static functions or whatever, just leave the method like it is and if the lifeform asks for the string it should say "123456789".

have fun with that problem (its like a riddle :D)

i hope you liked my explanaition =)
Avatar of dkloeck

ASKER

somewhere very very far away in another dimension and project a lifeform calls:

MyClass veryPowerfullInstance = "987654321"; <<< ehehehe.. sorry, I meant:

MyClass veryPowerfullInstance;
veryPowerfullInstance.uniqueInstanceString="987654321";
ASKER CERTIFIED SOLUTION
Avatar of existenz2
existenz2
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So you will need to dosomething like:

ExtendedString ex = new ExtendedString("test");
ex.InvertString();