Solved

calling method

Posted on 2006-07-19
10
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

710 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