NameValueCollection struct

I am trying to code a NameValueCollection struct.

When I create an instance of NameValueCollection, call Add() and then call Item, blank strings are returned. Why are they blank please?

struct NameValueItem
{
  String Name;
  String Value;
};

struct NameValueCollection
{
  int Count;
  NameValueItem Items[10];
  
  NameValueCollection()
  {
    Count = 0;
  }

  ~NameValueCollection()
  {
  }
  
  void Add(const String Name, const String Value)
  {
    Items[Count].Name = Name;
    Items[Count].Value = Value;
    Count++;
  }

  String Item(const String Name)
  {
    return Items[0].Value;
  }
};

Beakie:
When I create an instance of NameValueCollection, call Add() and then call Item, blank strings are returned. Why are they blank please?

The thing that I noticed about this code is that you're passing strings around by value the whole time. I'm not sure whether that works on AVR or not. The first thing I'd try is replacing "String" with "String&" in the parameters and return values of your methods.

In case "pass by value" is unclear, I'll include this link to an avrfreaks explanation...

maniacbug:

Beakie:
When I create an instance of NameValueCollection, call Add() and then call Item, blank strings are returned. Why are they blank please?

The thing that I noticed about this code is that you're passing strings around by value the whole time. I'm not sure whether that works on AVR or not. The first thing I'd try is replacing "String" with "String&" in the parameters and return values of your methods.

In case "pass by value" is unclear, I'll include this link to an avrfreaks explanation...

Thanks :slight_smile:

maniacbug:
The first thing I'd try is replacing "String" with "String&" in the parameters and return values of your methods.

Perfect. Worked a treat :slight_smile:

Yay, happy to hear it. Now I am interested to find out the root cause of this--I may try out some tests of my own to understand it. It's definitely good practice to pass large items by reference, but still I am interested in finding out what limits were encountered in this case.

Only worked for the first value. Now it hangs when I add more :frowning:

Can you post your code, otherwise we're all just guessing.

Groove:
Can you post your code, otherwise we're all just guessing.

struct NameValueItem
{
  String Name;
  String Value;
};

struct NameValueCollection
{
  int Count;
  NameValueItem Items[10];
  
  NameValueCollection()
  {
    Count = 0;
  }

  ~NameValueCollection()
  {
  }
  
  void Add(const String& Name, const String& Value)
  {
    Items[Count].Name = Name;
    Items[Count].Value = Value;
    Count++;
  }

  String& Item(const String& Name)
  {
    for (int i; i < Count; i++)
    {
      if(Items[i].Name == Name)
      {
        return Items[i].Value;
        break;
      }
    }
    String sReturn = "";
    return sReturn;
  }
};

All of it?

It uses an ethernet shield.

WebServer.zip (2.47 KB)

Just post your test code or you're just wasting everyone's time.

Groove:
Just post your test code or you're just wasting everyone's time.

I did

Where is the code that just tests the subject of this thread? How are you certain tha t the code you linked to doesn't affect your NameValueCollection?

i attached it a couple of posts ago

Where is your bare-bones test program?

Are you being deiliberately obtuse, or do you really not understand simple debugging?

Groove:
Are you being deiliberately obtuse, or do you really not understand simple debugging?

What do you want? I post the struct with the problem. I post the files. I post a link to the files.

I fail to see what you are still after from me?

OK, I'll use simple language - where is the simplest sketch that shows how your struct works (or doesn't) that doesn't rely on other undebugged code?
I.e., just simple prints.
Clear now?

Just pulled it down to have a look-see... What is this line..?

    String sReturn = "";
    return sReturn;

That's a guaranteed failure right there. It's returning a reference to a temporary string, so as soon as Item() returns, the return value is pointing to nothing.