0

I may be dense here, but here is my problem. I am writing an android app that is supposed to manage an inventory of items. There are multiple Spinner and AutoCompleteTextView objects on one of my layouts that I am trying to populate from one function, instead of having one function for each.

Here is what I have... The object _type is supposed to become a reference to the class for the Sqlite query to populate data in. I used Object in this sample code, but I have run it, as var, object, type... I have several issues coming up here, 1) I get '_table' is a variable but is used like a type 2) I am getting a similar problem with _res when I start trying to parse the data returned from the db connection

In essence what I want to do is use a variable as a place holder for a type. I've tried using a number of casts, and functions like .GetType(), but they still throw the same "x is a variable but used like a type". When I use it in the context of the list that receives the data I get the message "list requires one type argument"

At the moment I have this working by just having independent functions for each form item I am working with, but that's chunky, and I'd love to extend this to work on all form items that get data from the DB.

      public ArrayAdapter<string> PopulateAdapter(Context _c, 
                                                MyDatabase.enTables _which,
                                                MyDatabase.Options _o =null)
            {
                string[] _l;
                SQLiteConnection db = new SQLiteConnection(MyDatabase.GetDBFilePath());
                string _sql;

                Object _table;
                List<_table> _res;

                switch (_which)
                {
                    case GMDatabase.enTables.Manufacturers:
                        _sql = "SELECT * FROM Manufacturers";
                        _table = MyDatabase.Manufacturer;
                        break;
                    case GMDatabase.enTables.Styles:
                        _sql = "SELECT * FROM Styles";
                        _table = MyDatabase.Styles;
                        break;

                }

                _res<_table> = new List<_table>;
                _res = db.Query<_table>(_sql);
                if (_res.Count)
                {
                    for (int i = 0; i < size; i++)
                    {
                        _l[i + 1] = _res[i].Name;
                    }
                    return new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, _l);
                }
                return null;
            }

Thank you for taking a look, and any advice or ideas would be most appreciated!

Jesse Knott
  • 335
  • 4
  • 12
  • 2
    _table is a variable of type object, not a type itself. needs to be a Type, not a variable. You may want to take the time to read up on C# Generics – Jason Jun 27 '19 at 13:15
  • Don't use a leading underscore for local or parameter variables. Check out the Microsoft coding standards. It will save you a lot of hassle when you start working on other people's projects. – Neil Jun 27 '19 at 13:16
  • tl;dr **Just write multiple methods. This is not a profitable use of your time.** You're trying to use an object as a type parameter for generics. You can't. Types aren't objects in C#: `int` is a type; `string` is a type; `TextBox` is a type. You can't assign `string` to a variable, because `string` is not any kind of object. `typeof(string)` is an object of type `Type`, but that's a *very different thing*. The type parameter needs to be a type that is known at compile time. You could give this method a type parameter, but then you'd still be doing ugly things that generics aren't meant to do – 15ee8f99-57ff-4f92-890c-b56153 Jun 27 '19 at 13:18
  • Cheers, Thanks for the info, I was pretty sure that would be the case, I just wanted to see if there were a chance I could make something more reusable. I use leading _ for variables in my psudo code so I can always identify where I am experimenting, Definitely wouldn't use in production code, too easy to get mixed up with linker __ or disposable _ stuff... Thanks again! – Jesse Knott Jun 27 '19 at 18:31

0 Answers0