Lambda Expression With Ternary Operator In VB?
Feb 26, 2010Lambda expression with ternary operator in VB?
View 15 RepliesLambda expression with ternary operator in VB?
View 15 RepliesIn Perl (and other languages) a conditional ternary operator can be expressed like this:my $foo = $bar = $buz ? $cat : $dog;Is there a similar operator in VB.NET?
View 2 RepliesI saw this in some vb.net source code:Dim sTest As String = "" & drTest("column")
I was told that if drTest("column") is nothing, then sTest will be assigned "", so it is in effect doing:
Dim sTest As String = If("",Nothing,drTest("column"))What is the downside of doing it the first way I showed?
What is the difference between using If and IIf?
I am looking to make this statement work in a VB.net page:
[Code]...
The ternary operator is causing a error: Compiler Error Message: BC36637: The '?' character cannot be used here. I want to use a tenary operator to display text based on the the field Inactive containing a true or false text value.
I'm usually reasonably good at translating C# into VB, but I'm a bit stuck partially due to the ternary operator in this function and also because I don't fully understand what is happening with the "from x in dataSource.OfType<object>() select 1).Sum()"
[code]...
I'm having problems with a Nullable DateTime in VB.NET (VS 2010).
Method 1
If String.IsNullOrEmpty(LastCalibrationDateTextBox.Text) Then
gauge.LastCalibrationDate = Nothing
[code].....
Suppose we have a nullable value in vb.net: Dim i as Integer? We want to assign a value to it, basing on a condition, and using a ternary operator, because it's so neat and stuff: i = If(condition(), Nothing, 42) That is, if a condition is true, employ the null-ability, otherwise the value. At which point the shooting occurs. For no apparent reason VB compiler decides that the common base type for Nothing and Integer is Integer, at which point it silently translates the statement to: [Code]
View 5 Repliesdim val1 As Integer? = If(5 > 2, Nothing, 43)' val1 = 0 dim val1 As Integer? = If(5 > 2, Nothing, Nothing)' val1 = Nothing What gives? Is this a bug, or am I overlooking something?
View 1 RepliesI am having a weird problem. IIf is messing up when I am working with an array. Apparently it is checking my else statement even though it isn't activated. Here is some code that demonstrates the issue:[code]
View 2 RepliesThe following compiles in VB.NET (with Option Strict On) and outputs False:[code]Why does that work?The documentation clearly states that the three-argument version of If requires a Boolean as the first parameter:[code]So, why does this work? Is it a bug (or "hidden feature") in the compiler or is it a bug in the documentation and Boolean? is actually a valid type for the first argument of If(a, b, c)?In C#, b ? x : y does not compile if b is of type bool?.I've reported this issue to Microsoft Connect. Someone from MS has replied and confirmed that the documentation will be updated to include the Boolean? case.
View 4 RepliesIf you assign a value to a nullable integer via a ternary operator, it can't become null..While this question may seem like a duplicate of many, it is actually being asked for a specific reason.Take this code, for example: Dim n As Integer? = If(True, Nothing, 1) In that code, the ternary expression should be returning Nothing, but it's setting n to 0. If this were C#, I could say default(int?) and it would work perfectly. Now it looks like I am going to have to ditch the ternary and use a regular If block, but I really want to use the ternary. If Nothing were truly VB.NET's equivalent to C#'s default, how can you explain this behavior?
View 1 RepliesI got this Error. below is my code.
GenerateTheList is function.Need help
Private Sub buttGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttGenerate.Click
Dim thread1 As New Thread(New ThreadStart(GenerateTheList))
thread1.Start()
End Sub
From this code
[Code]...
If I put New Action(AddressOf PrivateMesage), then I get the following error: Bounds can be specified only for the top-level array when initializing an array of arrays. on the following code
[Code]...
I am dumb founded at this statement....maybe its just too many hours/days of doing C# to VB.Net conversion but i am drawing a blank on this conversion.
List<string> sColors = new List<string>(this.CustomPaletteValues.Split(','));
try {
List<Color> colors = sColors.ConvertAll<Color>(s => (Color)(new ColorConverter().ConvertFromString(s)));
What i have so far:
Dim colors As List(Of Color) = sColors.ConvertAll(Of Color)(....)
As you can see its the content of the lambda that i am hitting a brick wall with.
In looking at Fuzzy Date Time Picker Control in C# .NET? Piotr Czaapla's answer to that question is exactly what I need. unfortunately, I'm a VB.NET guy and I'm not that familiar with lambda expresions, so my attempts to convert the code have resulted in hours of misplaced parenthesis and banging my head with a brick.
Any chance some bi-lingual wizard could convert the C# code to VB.net for me?
[Code]...
Using lambda's in VB.Net results in no intellisense. Is this a bug with VS2010 or expected? Note that it works fine in C#
Return Array.TrueForAll(chequeColl, Function(x) x.Number <> "N") 'No intellisense Number does not appear
Return Array.TrueForAll(chequeColl, Function(x As MyClass) x.Number <> "N") 'Now casted intellisense appears
UPDATE: Here's an example
Public Class Cheque
Public Property Id As String
Public Property Status As Byte
[code]....
How should the following C# code be converted to VB.Net? I have tried several code converters and none produce correct results.
[code]...
I will probably sound like a bad developer here, but I am attempting to convert some code from the following page from C# to VB.NET:The code I am having trouble converting is from Step 4 of "Joining a Multicast Group" on the page, and here is the code with the comment lines removed:
private void Join()
{
_receiveBuffer = new byte[MAX_MESSAGE_SIZE];
[code].....
I want to implement this function:
Public Function GetFunc(Of TSource, TResult) _
selector As Expression(Of Func(Of TSource, TResult)) _
As Func(Of TSource, TResult)
[code]....
UPDATE:I have the following issue, it's a part of a function:
Dim obj As Object = value
For Each exp In expressions
If obj Is Nothing Then Return [default]
[code]....
Exception Details:
ArgumentException:Object of type 'System.Linq.Expressions.Expression`1 [System.Func`3[System.Char,System.Char,System.Char]]'cannot be converted to type 'System.Func`3[System.Char,System.Char,System.Char]'.
If been try to convert some code I have from VB.net to C# but I just cannot get it working!first is the vb code I'm only interested in the line that has the Lambda Expression for the C#[code]......
View 2 RepliesCurrently, I have code similar to this:
xxx, _
If(dr.Table.Columns.Contains("EnterpriseID"), dr.Field(Of Integer)("EnterpriseID"), -1), _
xxx
I would like to create a Lamdba expression something like the following (this doesn't work):
Dim xCol As Func(Of DataRow, String, Type, Object, Object) = _
Function(dr, x, t, dflt) _
If(dr.Table.Columns.Contains(x), dr.field(Of t)(x), dflt)
So that I can say something like:
xxx, _
xCol(dr, "EnterpriseID", GetType(System.Integer), -1), _
xxx
I know in VB Lambda expressions must return a value and can only have one statement. But in my case I just need to execute a Sub that does not return a value. Is there a way to trick it by somehow returning a dummy value yet still call my Sub? If possible what would the syntax be like?
View 3 RepliesI am having an issue using the vb equivilant of the MAX lambda expression. at foos.Max(function(x) x.id) when I try to intellisense the property ID VS will not show it. But when i run the example it works. Is there something I am doing which is wrong, an i am just lucky it runs? [Code]
View 1 RepliesThere is the code:
Private Sub InsertItemInCache(Of T)(ByVal item As CachedItem(Of T), ByVal dependency As AggregateCacheDependency, _
ByVal key As String, ByVal updateCallBack As CacheItemUpdateCallback)
[code].....
[URL]..Module Module1
[Code]...
It says the result will always be 5 namely the final value of i. How come?
They don't put the iteration variable in the "closure"?
I am Creating a Lambda Expression, but it' not getting me the write Out put :
Public Class Form1
Delegate Function GetLastChar(ByVal S As String) As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
[code]....
I want Send a Name like "Arindam" and its return me "m", the last character.but in messagbox return value shows "fasle" I can't under stand why it's showing "False" instead of "m", the last character.
I am having a generic ListOf-collection holding the self-created User-object, I can filter for users by using lambda-expression like this:
Dim reviewers As List(Of User) = users.Where(Function(u) u.DvRoleID = 2).ToList
Instead of using the lambda expression I can also write the function and call it as argument like this:
Public Function GetReviewer1(ByVal u As User) As Boolean
If u.DvRoleID = 2 Then
Return True
[Code] .....
Now the question: why can't I create a delegate having the same signature as the GetReviewer1 function and use the delegate as argument inside the where-clause like this:
Delegate Function Criteria(ByVal u As User) As Boolean
Public Function GetReviewer1(ByVal u As User) As Boolean
If u.DvRoleID = 2 Then
[Code] .....
The compiler does not accept the crit delegate as argument.
I am trying to see if the controller class name is the name of the controller name passed in. I think my lambda expression is wrong and that is why it isn't working properly. I am passing in the correct controller names, but the export variable is always null.
Public Function CreateController(requestContext As System.Web.Routing.RequestContext,
controllerName As String)
[Code].....
In MSDN documentation defination about lambda expression is "A lambda expression is a function or subroutine without a name that can be used wherever a delegate is valid ". Here validity of delegate is necessery only when we are passing lambda expression as an argument to a method.
[Code]...
Where can I find complex LINQ examples made using VB.NET Lambda Expression syntax?During my searches I always found 101 LINQ Samples but they use the other notation and for me is not always clear how to transform that code into a lambda expression.
View 1 Replies