M
M
Mykola Franchuk2016-07-29 13:59:25
C++ / C#
Mykola Franchuk, 2016-07-29 13:59:25

How to pass a property to another class?

There is a class fMain with its combo box on the form. It is necessary to transfer the data that is inside the combobox to another class. It was decided to use the property:

public string combRangesAccess
        {
            get
            {
                return combRanges.Text;
            }
            set
            {
                combRanges.Text = value;
            }
        }

This code is working, but another class, of course, does not see this property. property is not static. But if you make it static, the studio throws an error: "A non-static field, method, or property dicto.fMain.combRanges requires an object reference." Please tell me how to pass the contents of a combobox to another class using a property. Or maybe there are some other alternatives?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Michael Kim, 2016-07-29
@kuzia_bRatok

Your combobox is a field of class fMain. Static methods do not belong to a specific object => they do not have access to the object's data. But you can pass a reference to an object as an argument to a static method.

// вот сам метод
class fMain{
//...
public static combRangesAccess(fmain ob)
{
    return ob.combRanges.Text;
}
// вот Вы создаете экземпляр класса fMain
fMain obj = new fMain();
// вот вызываете метод
string text = fMain.combRangesAccess(obj);

D
Dmitry Kovalsky, 2016-07-29
@dmitryKovalskiy

You cannot pass anything to another class. Maybe to another object? And what prevents you from doing so - Non-
crutch options depend on your algorithm. What do you implement, how and why in this particular way. You want that at you one class (namely a class) depended on a state of a combobox? it is a bad idea. The class object is still okay - at the constructor level, you can simply pass the state of the property you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question