Solved

calling method

Posted on 2006-07-19
10
215 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
Comment Utility
you can define that method as static
0
 
LVL 2

Expert Comment

by:MyersA
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
the class knows which method, but its the method of an instand i can not access to
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Author Comment

by:dkloeck
Comment Utility
instance*
0
 
LVL 10

Author Comment

by:dkloeck
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
So you will need to dosomething like:

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

10 Experts available now in Live!

Get 1:1 Help Now