I am kinda newbe to C#. and I am trying to understand the logic of generic and OOP stuff.
My app consists of 3 layers. UI, BLL DAL. And I am customizing a gridview columns based on my needs. All the customized columns are sharing the event when the user type something in the cell, I do db search for that text. In the customized datagridview class.
I am sending information as mentioned below and upon that I receive different results. I used datatable before .. it works as it should . but I wanna accomplish that with types objects as List. here is the code..
I'll be happy if someone makes my lift easier .. I tryied to have a variable and assige to it different values (class name, list. list of .. didn't work .. is C# or the logic supports am trying to do without going back to datatable ..
am working on this thing .. ( it says Implicitly-typed variables must be initialized ).
var obj;
if (a = 1 ) var obj = Activator.CreateInstance(typeof(CareerFile1));
if (a = 2 ) var obj = Activator.CreateInstance(typeof(CareerFile2));
if (a = 3 ) var obj = Activator.CreateInstance(typeof(CareerFile3));
dbo_Bll aaa = new dbo_Bll();
-- the problem comes from here ..
List Lst = aaa.Get_LookupItem(c1);
Prasad RaveendranRank #320 · 7.8K reputationPosted Nov 18, 2023, 10:06 PM EST
Here's an example of how you might resolve these issues:
object obj = null; // Declare obj as an object type if (a == 1) obj = Activator.CreateInstance(typeof(CareerFile1)); else if (a == 2) obj = Activator.CreateInstance(typeof(CareerFile2)); else if (a == 3) obj = Activator.CreateInstance(typeof(CareerFile3)); dbo_Bll aaa = new dbo_Bll(); // You might need to cast the returned object to a specific type // based on the logic above before adding it to the list. List Lst = new List(); // Create a list of objects Lst.Add(obj); // Add the created object to the list // Then use the list however neededHere,
objis declared as anobjecttype to accommodate the different objects created usingActivator.CreateInstance(). AListis used to store these objects since their types are not directly known at compile time.If you have a specific known interface or base class shared by
CareerFile1,CareerFile2, andCareerFile3, you could use that common type in the list instead ofobject. For example:List Lst = new List(); // Use a common base class if availableReplace
CommonBaseClasswith the actual base class/interface thatCareerFile1,CareerFile2, andCareerFile3inherit/implement.Remember, casting might be necessary to use the objects retrieved from the list, as their specific types might not be known at compile time.
Sam HobbsRank #107 · 20.5K reputationPosted Nov 19, 2023, 12:31 PM EST
First note that you have:
(a = 1)
And that should be:
(a == 1)
See the difference?
Also, instead of:
if (a == 1 ) obj = Activator.CreateInstance(typeof(CareerFile1)); if (a == 2 ) obj = Activator.CreateInstance(typeof(CareerFile2)); if (a == 3 ) obj = Activator.CreateInstance(typeof(CareerFile3));You can do:
Type[] CareerTypes = new Type[] {typeof(CareerFile1), typeof(CareerFile2), typeof(CareerFile3)}; . . . obj = Activator.CreateInstance(CareerTypes[a-1]);As in: https://dotnetfiddle.net/dtghHE
And if CareerFile1, CareerFile2 and CareerFile3 inherit from a base then you can use the base instead of object.
I am not sure what you need to do but Activator.CreateInstance is advanced stuff. There might ber an easier way to do what you need to do.
Chris LoveRank #332 · 7.5K reputationPosted Nov 20, 2023, 3:14 PM EST
It looks like you're trying to use generics to create lists of different types based on a condition. The error message you're seeing, "Implicitly-typed variables must be initialized," is because implicitly-typed variables in C# (those declared with
var) need to have their type determined at compile-time based on the right side of their initialization. Since you're declaringobjwithout assigning any value to it, the compiler can't infer what type it is.Let's correct a few things in your code. Firstly, it's important to note that you can't create a List of a type that is not known at compile time, without using reflection or other advanced techniques. Here's a way to restructure your code to eliminate the immediate error and start on a path to a more dynamic solution:
object obj = null; if (a == 1) obj = Activator.CreateInstance(typeof(CareerFile1)); else if (a == 2) obj = Activator.CreateInstance(typeof(CareerFile2)); el se if (a == 3) obj = Activator.CreateInstance(typeof(CareerFile3)); // Now we have an object, but what we want is a list of that object type // To achieve this we're going to use reflection Type objType = obj?.GetType(); if (objType != null) { var listType = typeof(List<>).MakeGenericType(objType); var listInstance = Activator.CreateInstance(listType); // Now you'll have to cast this to an appropriate type or use reflection to work with listInstance } // Use the listInstance ...In this code:
obj.=) to equality operator (==) in conditions.Activator.CreateInstanceto determine the type of the list we're creating.However, please note that working with a non-generic list means you'll lose some type safety, and you'll have to use reflection or dynamic casting when working with items in the list. If you can create a common interface or base class for the
CareerFileobjects, you could create aListorListinstead, which would be more in line with good OOP practices.