Solved

calling method

Posted on 2006-07-19
10
216 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:dkloeck
10 Comments
 
LVL 19

Expert Comment

by:Desp
ID: 17138018
you can define that method as static
0
 
LVL 2

Expert Comment

by:MyersA
ID: 17138636
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);
    }
}
0
 
LVL 10

Author Comment

by:dkloeck
ID: 17140946
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.

0
 
LVL 14

Expert Comment

by:existenz2
ID: 17141681
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.
0
 
LVL 10

Author Comment

by:dkloeck
ID: 17142057
the class knows which method, but its the method of an instand i can not access to
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Author Comment

by:dkloeck
ID: 17142060
instance*
0
 
LVL 10

Author Comment

by:dkloeck
ID: 17142091
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 =)
0
 
LVL 10

Author Comment

by:dkloeck
ID: 17142102
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";
0
 
LVL 14

Accepted Solution

by:
existenz2 earned 500 total points
ID: 17142177
That kind of stuff won't be available till c# 3.0. And C# 3.0 is coming out in the end of 2007 if I remember correctly

you can however do something like:

class ExtendedString
{
 string value = "";
 public ExtendedString(string value) { this.value = value; }

  public void InvertString() { <<invert method>> }
}
0
 
LVL 14

Expert Comment

by:existenz2
ID: 17142183
So you will need to dosomething like:

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now