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);
Chris LovePosted Nov 20, 2023, 8:14 PM
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:
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.Sam HobbsPosted Nov 19, 2023, 5:31 PM
First note that you have:
(a = 1)
And that should be:
(a == 1)
See the difference?
Also, instead of:
You can do:
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.
Prasad RaveendranPosted Nov 19, 2023, 3:06 AM
Here's an example of how you might resolve these issues:
Here,
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:Replace
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.