Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

calling method

Posted on 2006-07-19
10
Medium Priority
?
223 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:Fahad Mukhtar
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
Independent Software Vendors: 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!

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

715 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