.net - Can The Select Extension Method Project To A List Of Instantiated Objects
Mar 24, 2011
I have two lists declared as follows:
Dim lstDBItems As New List(Of DBItem)
Dim lstAppItems As New List(Of AppItem)
I am trying to do something like this:
I've a function which returns List(Of AppItem):
Function GetAppItems() As List(Of AppItem)
'...
End Function
In the above function I populate lstDBItems and then write the return statement like follows:
Return lstDBItems.Select(Function(x)
dim oItem As New AppItem()
oItem.Property1 = x.DbProperty1
[Code]....
The weird thing is the code compiles, but on rumtime I get a type case error.
View 3 Replies
ADVERTISEMENT
Sep 10, 2010
I recently wrote a section of code wherein upon finishing, I received a warning from the compiler telling me that one of my variables is being used before it is assigned a value. In practice, the method call on this object will never be made without the object being instantiated beforehand. Here is the snippet of code
Try
fs = New FileStream(fileName, FileMode.Open)
Dim PolarMatrix As PolarMatrix
PolarMatrix = DirectCast(bf.Deserialize(fs), PolarMatrix)
[code]....
I assume I'm receiving this warning because the first line in the Try section may be the line to throw the error, and the Object will never be instantiated. FileName though, is a variable being passed to this method which has already been checked for errors, so it is guaranteed to be correct. The error I'm expecting to perhaps be thrown comes during the deserialization.
So my question: When warnings are given on objects that the compiler thinks may not have been instantiated, does this overrule the user knowing that a problem will never arise on that line? Is it sometimes necessary to add code simply to appease the compiler?
View 1 Replies
Apr 6, 2010
I want to define a function similar to:[code]My current definition is having an issue with the following statement:GetList = (From x In oDS.Tables(0) Select New T(x)).ToList.I can't figure out how to get to create a New object of type T, any suggestions?
View 2 Replies
May 23, 2012
The following LINQ query resp. call of the extension method Select in Visual Basic 2010 is working fine:
Dim qAvSalary = qJobData.Select(Function(e) e.AvSalary) But doing so I am not able to specify the name of property I want the query (e.g. AvSalary) using a string variable. This should be possible if I use a LINQ expression tree. Searching and trying a long time on how to translate the query to a corresponding expression tree was not successful. My final approach is:
[Code]...
View 1 Replies
Nov 13, 2009
I'm working on a larger VB application (framework v3.5) where the compile time continues to get slower and slower the larger it grows. It currently takes about 7 minutes to compile just the extensions project. We have other similar projects in C# that don't experience the slow compile time.
View 1 Replies
Feb 2, 2011
I'm using .NET 3.5 In my DataLayer class I have references of System.Core,System.Data.Linq, System.Data.DataSetExtensions. But I cantnot use this feature in Linq query if I have Option Strict ON:
[Code]...
View 1 Replies
Dec 16, 2010
I have a custom list that inherits Generic.List and it has a method for deselecting all of the members named DeselectAll It has looked like this for a few weeks and has worked fine.
Public Sub DeselectAll()
MyBase.ForEach(Function(p As Publipostable) p.ModeEnvoiChoisi = Nothing)
End Sub
Today, it stopped working ?!? I reverted to an earlier version using delegates and that works fine...
Public Sub DeselectAll()
MyBase.ForEach(AddressOf DeselectModeEnvoi)
End Sub
[code]....
Edit: Stopped Working meaning it no longer sets the property p.ModeEnvoiChoisi to Nothing on each item in the list. The DeselectAll method gets called, but all the items retain their previous values...
View 1 Replies
Feb 17, 2010
This is a question I have asked myself many times in the past as I nested using statements 5 deep. Reading the docs and finding no mention either way regarding other disposables instantiated within the block I decided it was a good Q for SO archives.
[Code]...
View 6 Replies
Jul 7, 2011
I have
Dim objectsList as List(Of Object) = GetAllObjects()
' Filter from Objects just Persons '
Dim peopleList as List(Of Person) = ???
What is the most efficient and effective LINQ expression to do it?
EDIT
1 Dim selectedObjects As List(Of Object) = GetAllObjects()
2 Dim selectedPeople As IEnumerable(Of Person)= selectedObjects.OfType(Of Person)
3 Dim people As List(Of Person) = selectedPeople.ToList()
Error on 3:
Value of type
'System.Collections.Generic.List(Of
System.Collections.Generic.IEnumerable(Of Person))' cannot be
converted to
'System.Collections.Generic.List(Of Person)'.
View 3 Replies
Nov 16, 2011
I have a combo box that is populated with a list of objects (corresponding to all employee rows from a database that meet the criteria IsTech). These are not entity objects though, I have created my own class that stores the member values of each row from the DB. I have set the DisplayMemberPath to EmployeeId so the Combo box display the EmployeeId ( an integer ) member of my object. Ie. right now there are only 2 IsTech employees in my DB so the combobox displays the numbers 3 and 8 ( their EmployeeIDs).
[Code]...
View 3 Replies
Dec 3, 2009
Why would I use an extension method instead of just creating non-extension sub or function?
For ex, I could have an extension function called IsNullOrEmptyOrAllSpaces on String, which does a check as its name implies. Or I can write a stand alone function that does the same thing. Other than having the extension show up in Intellisense, is there any advantage? Is a call to the extension quicker/more efficient than a call to a regular function?
View 8 Replies
Mar 16, 2012
I saw this post and I want to know if this is possible in VB. So like extension method, do extension properties exists in VB.Net? Here I've read they do, but cannot find any examples.
View 3 Replies
Sep 16, 2010
Write an overload for every numeric type or if possible constrain a generic extension method to just numeric types.
View 2 Replies
Jul 16, 2010
Extension methods are useful for types that you don't own and can't/don't want to derive from and extend (e.g. reference types and interfaces). Obviously, interfaces should be kept as short and to-the-point as possible, so extension methods for interfaces are particularly useful (e.g. LINQ).For classes, especially classes that you own, they're still useful - but I'm wondering how you determine what should be an extension method or what should be a method in the class itself.Personally, every time I think about it, I keep going round in circles with the following thoughts:If it's useful enough, it should be in the class.It's not part of the core responsibility of the class, it should be an extension method - but if it's useful enough, surely it should be the responsibility of the class...
View 3 Replies
Aug 13, 2011
I'm trying to code a class of RandomNumber. One of the class methods needs to populate a "List (Of RandomNumber)" ... which was passed as a parameter to the method ... with 10 random numbers between 1 and 50. DEAD SIMPLE :)
View 6 Replies
Apr 10, 2010
I know how to add extension method Like (Date.Now.MyExtensionFunction()),But I can NOT find how to add such methods and functions in class (not in module) with vb.net
View 3 Replies
Mar 26, 2012
I am trying to write extension methods in VB.NET
Imports System.Runtime.CompilerServices
Module ExtensionMethods
<Extension()> _
[code]....
But I am getting this error.Class 'System.Web.UI.WebControls.ListItem' cannot be indexed because it has no default property
What could be wrong?I am calling the code like this.
ddlSalesmanager.Items.FindByText(survey, StringComparison.CurrentCultureIgnoreCase)
P.S: I ported this wonderful code from C# to VB
View 1 Replies
Feb 23, 2012
I converted some code from c# for a VB.net project I have an object declared as this :
<Serializable()> _
Public Class oUserApplication
Public ApplicationID As Integer = 0
Public ApplicationRoleID As Integer = 0
[code]....
View 3 Replies
May 18, 2009
I ran into a strange issue over the weekend while I was working on an asp.net mvc project in vb.net. I created an extension method to convert an integer to the corresponding month it is associated with. I tested the extension method in a console application so I know it is working.
In my asp.net mvc project I have a view and want to call the extension method but I get an error that the extension method is not recognized. I imported the namespace it was contained in and still couldn't shake the error.
Extension Method:
Imports System.Runtime.CompilerServices
Module SiteExtensions
<Extension()> _
[Code].....
View 3 Replies
Nov 10, 2010
I want to implement an extension method in VB.NET that will clone an object of type T.Say, I want
Dim cust1 as New Customer() //...
Dim cust2 as New Customer() //...
cust2 = cust1.Clone()
[code]....
View 2 Replies
Jun 7, 2012
I created a module to add some Extension Methods to a Class. This Class inherits from another parent Class. In the code of my Extension Methods, I would like to refer to the base Class using MyBase. This doesn't seems to be allowed (Error message : "'MyBase' is not valid within a Module.")
[Code]...
View 1 Replies
Sep 13, 2010
I am starting to play with extension methods and i came across with this problem:In the next scenario i get a: "extension method has a type constraint that can never be satisfied"
Public Interface IKeyedObject(Of TKey As IEquatable(Of TKey))
ReadOnly Property InstanceKey() As TKey
End Interface
[Code]...
View 1 Replies
Jul 13, 2011
I'm trying to create an extension method that returns an IEqualityComparer based on a lambda function. Heres the extension method:
<Extension()>
Public Function Comparer(Of T)(Func As Func(Of T, T, Boolean)) As IEqualityComparer(Of T)
Return New GenericComparer(Of T)(Func)
End Function
[Code]...
View 1 Replies
Oct 27, 2010
I have a Direction Enum:
Public Enum Direction
Left
Right
Top
Bottom
End Enum
And Sometimes I need to get the inverse, so it seems nice to write:
SomeDirection.Inverse()
But I can't put a method on an enum! However, I can add an Extension Method (VS2008+) to it.
In VB, Extension Methods must be inside Modules. I really don't like modules that much, and I'm trying to write a (moderately) simple class that I can share in a single file to be plugged into other projects.
Modules can only reside in the file/namespace level so I have one in the bottom of the file now:
Public Class MyClass
'...'
End Class
[Code]....
It works, and "if it ain't broke, don't fix it", but I'd love to know if I'm doing it wrong and there's a better way with less boilerplate. Maybe in .NET 4?
Finally, I know I could write a structure that behaves like an enum, but that seems even more backwards.
View 2 Replies
Nov 16, 2011
I've created a MyFunc extension (in Module Extensions) for all the base types as well as generic arrays, ICollection(Of T) and Dictionary(Of String,T):
Function MyFunc(Of T)(a() As T) As String
Function MyFunc(Of T)(collection As ICollection(Of T)) As String
Function MyFunc(Of T)(dict As Dictionary(Of String,T)) As String
[code].....
View 1 Replies
Jan 1, 2010
how do we access MyBase from an extension method?
i'm trying to extend a Sub called PassDown that would call the form's Base's OnPaint method
<Runtime.CompilerServices.Extension()> Public Sub PassDown(ByVal form_instance As Windows.Forms.Form, ByVal e As EventArgs)
MyBase.OnPaint(e)
but of course, mybase couldn't be accessed this way so is there an alternative?
View 5 Replies
May 31, 2012
Can I access values in application settings of web.config in extension method?I have the below code but it is not working:
<Extension()> _
Public Function DocCountExt(ByVal value As Collection) As Integer
Dim maxOffSet As Integer = Integer.Parse(ConfigurationManager.AppSettings(APP_SETTINGS_OFFSET))
return maxOffSet
End Function
The above code does not work.
View 3 Replies
Aug 7, 2011
i want to extend the integer class to a method that increments current integer value by 2. I create a console application and a second module which contains the extension method.
Imports System.Runtime.CompilerServices
Module StringExtensions
<Extension()>
[code]....
and everything works fine. The extension method module can be imported to any other project (Project-->add existing item --> select the vb file that contains the extension method module) and works fine too. But the code is accessible to other users. Is there a method to convert the extension method module to a dll and adding it to other projects as a reference. I tried to do it with creating a class project and pasting the code of the extension method module and compile the dll and then added it as reference to another project and imported the class with the keyword imports, but it didn't work.
View 5 Replies
Jul 5, 2010
I wanna create extension method for IEnumerable(Of FileSystemInfo). However, compiler says "'Sort' is not a member of'System.Collections. Generic. IEnumerable(Of System.IO.FileSystemInfo)". What is strange is that IntelliSense shows this method. Where's error? [code]
View 1 Replies
Feb 11, 2011
In our .aspx pages, we've got lots of this code:<%= CType(GetLocalResourceObject("key"), String)) %>I'd like to add an extension method that I can use in our .aspx views that allows me to do this:<%= GetLocalResourceString("key") %>The code isn't working, though:Imports System.Runtime.CompilerServices
View 1 Replies