how to access one class property in another class linq [duplicate] - c#

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
how to make an variable accessible to other class(within same csproj)
I have a variable
int principleIndex = Principles.Instance.RowIndexToPrincipleIndex(hti.Row);
I want to use it in another class linq query:
principlesList.Select(p => p.GetInstanceForDatabase()).where(p=>p.principleIndex ).ToList()
but principleIndex is not accessible here.
How do I do it??? I tried to make it static and I also tried to use it inside a property, but does not work.

Is principleIndex defined in the same scope as your second line of code? If so perhaps your Where call should read something like .Where(forDBInstance => forDBInstance.Index == principleIndex)

Related

Is there a way to shorten field declaration in C#? [duplicate]

This question already has answers here:
Is there a way to abbreviate a custom class type declaration?
(4 answers)
Implicit typing; why just local variables?
(6 answers)
Closed 2 years ago.
Why do I have to write:
public VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName> field = new VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName>();
Instead of:
public var field = new VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName>();
Is there any way to shorten this ridiculous declaration?
Why do I have to write two identical types in a single line?
Upd.
I have found that using "dynamic" keyword instead of "var" perfectly solves this problem!
Please feel free to provide any info on the perfomance (or other) issues with this solution!
You can use a type alias. Documentation can be found here. Link to alias for generic class: here
using YourShortName = VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName>;
Usage
public YourShortName field = new YourShortName();
See here for why you can only use var for local variables.
And see here for an in depth look at dynamic and why it's not even remotely close to using var.

Accessing a Lazy-Loaded field... Object reference not set to an instance of an object [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 4 years ago.
I am sure I am just missing something basic so hope I can get my question across clearly.
I have a class named ItemData which implements an interface IITemData. Within the class ItemData I have the following:
However when I am trying to use this ItemData class (via the interface) it looks like this ComponentType is loading okay:
But when I am trying to access it in a lambda expression:
I keep getting the Object Reference error from the above.
I have the feeling I am just missing something straight forward.
Try it with null check maybe, seems like some of the data in list are null
var sortedList = new List<IItemData>(new ItemData[]{originalList.Where(x => x?.ComponentType?.Name == "template").SingleOrDefault()});

Initializing a Class dynamically using a string as class name [duplicate]

This question already has answers here:
C# Reflection: How to get class reference from string?
(6 answers)
Closed 8 years ago.
I am new to C# and writing some automation framework .I want to initialize a class dynamically based on condition .
i get the name of the class as a string based on conditions .
Ex : "Vehicle_"+ typeOfvehicle => Which will on run time may be Vehicle_2Wheeler or Vehicle_3Wheeler or Vehicle_4Wheeler .
I am using if , else statement for now . But if i can initialize the class with the type of Class i want to dynamically it would be better .
I think i need to use the Reflection API but not sure how to achieve this .
Please let me know if some one has an idea of this .
In C# Type.GetType("Truck") will return a Type that you can then instantiate
var type = Type.GetType("MyProject.Truck");
var instance = (Vehicle)Activator.CreateInstance(type);
Though if you don't know the specific type at compile time, leave off the cast, and just use object, dynamic, or a base class.
To pass args:
Activator.CreateInstance(type, arg1, arg2);

C# Do not define variable for not used out parameter [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there a way to omit out parameter?
Instead of:
SomeType param3;
SomeMethodCall(param1, param2, out param3);
I want not to define param3 in the case I don't need its value.
Is there any way to achieve this?
No, there is no way of doing that in C#.
If SomeMethodCall is something you defined yourself then you can overload the method. If it's not then you can't.
No, you'll have to actually create a variable of the correct type if you want to use a function that takes an out parameter.

Default for generic type? [duplicate]

This question already has answers here:
Is there a reasonable approach to "default" type parameters in C# Generics?
(6 answers)
Closed 9 years ago.
Is it possible to do something like
public class PriorityQueue<TValue, TPriority=int> where TPriority : IComparable
(note the =int) ?
Before you suggest it, yes, I know I can just add another line:
public class PriorityQueue<TValue> : PriorityQueue<TValue, int> { }
But I'm wondering if it's possible to do it as a param.
No. There is no option for default types on generic types in C#.
Your second example is often the "best" option available, if you need this behavior.

Categories

Resources