0

I'm trying to get a parameter with this function...

public static int subsumer(string id,int acc,SqlConnection connection) 
{
    acc++;
    SqlCommand cercas = new SqlCommand("select * from common_relation where id_source ='" + id + "' AND type='@' ", connection);
    SqlDataReader leggsyn = null;
    leggsyn = cercas.ExecuteReader();

    int f = 0;
    while (leggsyn.Read()) 
    {
        f=  subsumer(leggsyn["id_target"].ToString(),acc,connection);
        if (acc <= f) 
        { 
            acc = f; 
        }  
    }

     //siamo arrivati alla fine
    return acc-1;
}

each cycle the parameter acc will increment and debugging i see that in my case it reach value 3, but in the final recursion i get always 0...i can't get it...thank you all

4
  • What do you mean by parameter? Also why are you using string concatenation instead of using a proper parameterized query? A single weird character in your inputs will cause problems. More interesting input may be 1;DROP TABLE COMMON_RELATION;-- Commented Apr 1, 2015 at 14:29
  • 4
    How are you calling the function? Have you stepped through it in the debugger to make sure your logic is correct? Commented Apr 1, 2015 at 14:31
  • what is your function suppose to do ? sum or max ? because i see only assignation in your loop no incrementation Commented Apr 1, 2015 at 14:43
  • i'm calling this function this way int fuffa = DistanzaParole.subsumer("n#00004123", 0, connection); it is supposed to give me the max acc it can reach Commented Apr 1, 2015 at 14:48

3 Answers 3

2

You need to pass acc by reference. I.e use: public static int subsumer(string id,ref int acc,SqlConnection connection) {

Sign up to request clarification or add additional context in comments.

2 Comments

The function is returning acc-1 so using ref should not be necessary (unless the OP is calling it incorrectly).
i'm calling this way int fuffa = DistanzaParole.subsumer("n#00004123", 0, connection);
1

By returning acc - 1, you are decrementing f returned by your recursive call and I don't think that's what you expect.

public static int subsumer(string id,int acc,SqlConnection connection) 
{
    SqlCommand cercas = new SqlCommand("select * from common_relation where id_source ='" + id + "' AND type='@' ", connection);
    SqlDataReader leggsyn = null;
    leggsyn = cercas.ExecuteReader();

    int f = 0;
    while (leggsyn.Read()) 
    {
        f=  subsumer(leggsyn["id_target"].ToString(),acc + 1,connection);
        if (acc <= f) 
        { 
            acc = f; 
        }  
    }

     //siamo arrivati alla fine
    return acc;
}

On a side note, querying recursively isn't a good design choice. Querying recursively with readers open for each call on the stack is even worse. Not using query parmeters is also a bad thing.

2 Comments

ok now i finally get it! i know query recursively isn't a good choise, i'm not an expert developer and i'm facing sentence similarity with wordnet and some other stuff that it isn't properly an "hello world". How can i use query parameters?
@Lorenzo, search existing questions or post a new one containing your query and asking how you can parameterize it.
0

The method increments at the beginning, and decrements at the end, therefore it seems you will always end up at the initial call value of acc (in your case, 0).

2 Comments

it is not supposed to increment in: f= subsumer(leggsyn["id_target"].ToString(),acc,connection); if (acc <= f) { acc = f; } ?
It will increment when acc++, then when there are no records, it will return acc--, which means it returns the same value it was given.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.