SQL private void called from public static ArrayList

paolobozz

New member
Joined
Nov 7, 2019
Messages
2
Hi,

I have a SQL private void called from public static ArrayList like :

C#:
public static ArrayList Parametri_Rotore()
{
    ArrayList arl = new ArrayList();
    par p;
    int id = Mex.PAR_ROTORE + 1;
    int id_par;

    // Descrizione
    p = new par(); id_par = 0;
    p.par_descr = Mex.M[id]; id++;
    p.par_tipo = TEXTBOX_STRINGA;
    p.par_min = 0;
    p.par_max = 0;
    p.par_lenght = 20;
    p.par_enable = Abilita_Parametro(OPERATORE);
    arl.Add(p);

    Insert_SQL_Par_Rotore(id.ToString(), p.par_descr, p.par_um, p.par_tipo.ToString(), p.par_min.ToString(), p.par_max.ToString(), p.par_lenght.ToString(), p.par_enable.ToString());

    // ...
}


Previous code is called from the following code :

C#:
private void Insert_SQL_Par_Rotore(string IDParametroProg_string, string Descrizione_string, string UnitaMisura_string, string Tipo_string, string Min_string, string Max_string, string Perc_string, string Perc_Enable_string)
{
    // ...
}


I receive the following error:

CS0120 È necessario un riferimento all'oggetto per la proprietà, il metodo o il campo non statico 'Form_Parametri.Insert_SQL_Par_Rotore(string, string, string, string, string, string, string, string)'

could you please give me a suggestion how to solve this issue ?

Regards,

Paolo
 
Last edited by a moderator:
A static method cannot call an instance method without create an instance of the class first. Your Parametri_Rotore() is declared to be static. Your Insert_SQL_Par_Rotore () is declared to be an instance method. If the latter method is not dependent on any instance variables, the most expedient (although not always correct) solution is to simply declare the latter method static as well.
 
Hi Skydiver,

Thanks for your reply. Please consider that I am new with C#, however As far as I understand, seems that is not possible to pass all the parameters (string IDParametroProg_string, string Descrizione_string, string UnitaMisura_string, string Tipo_string, string Min_string, string Max_string, string Perc_string, string Perc_Enable_string) in a static method with your suggested solution .

Could you please explain how would you pass those parameter into a static method ?

Regards,
Paolo
 
Last edited:
Hello and welcome to the forums.

Firstly, please don't bate post. I detest it, and I'm doing this long enough to know when someone is looking for someone to do your homework for you. With a little research, you could find out what is being recommended to you.

Also post your code in code tags [CODE=csharp] Your code here [/CODE]

And post your error messages in English, as this is an English speaking forum.

Error translated says : CS0120 A reference to the object is required for the property, method or non-static field 'Form_Parametri.Insert_SQL_Par_Rotore (string, string, string, string, string, string, string, string)'

Two things you can do; they are, create an object reference of the object you are trying to access or else make it static. static modifier - C# Reference

I generally try to avoid static members when working with databases as they are known to leave code-smells.
 
Thanks for your reply. Please consider that I am new with C#, however As far as I understand, seems that is not possible to pass all the parameters (string IDParametroProg_string, string Descrizione_string, string UnitaMisura_string, string Tipo_string, string Min_string, string Max_string, string Perc_string, string Perc_Enable_string) in a static method with your suggested solution .

Could you please explain how would you pass those parameter into a static method ?
It is possible. You would pass them exactly the same way:
Calling a private static method from a static method:
class Foo
{
    public static void DoHomeworkForMe()
    {
        DoHomework("Do", "my", "homework", "for", "me.", "I", "am", "lazy.");
    }
    
    private static void DoHomework(string s1, string s2, string s3, string s4, string s5, string s6, string s7, string s8)
    {
        :
    }
}
 
Back
Top Bottom