.net - Result With IF() Function And A Nullable Integer?
Dec 1, 2009I would expect the following vb.net function to return a value of Nothing, but instead its returning a value of 0.
[Code]...
I would expect the following vb.net function to return a value of Nothing, but instead its returning a value of 0.
[Code]...
I am trying to understand why the two code samples behave differently. I always believed the If() function to mimic the If language feature. Or am I looking at a behavior of Nullable(Of Integer) that is causing this?
Sample #1:
If Not String.IsNullOrWhiteSpace(PC.SelectedValue) Then
Dim pcFilter1 As Integer? = CInt(PC.SelectedValue)
Else
[Code]....
I have a LINQ2Entity problem, I want to get the value, string, from a database, so I select FProducts.Serial_number, and to end the query, I do .ToDictionary. The problem is, it tells me that it doesn't have sufficient parameters, to convert ToDictionary. So I need something like select FProducts.Serial_number, Nothing). ToDictionary. Also FProducts.Serial_number, 0).ToDictionary doesn't work.
Sub GetStatistics(ByVal ProductNumbers As List(Of String), ByRef Passed As Integer, ByRef FailedProducts As List(Of String))
Passed = 0
FailedProducts = Nothing
[Code]...
When using the If operator [URL] to assign a value to a System.Nullable object, if the result is Nothing (null), then 0 is assigned to the object. Example: 'Expected value is null (Nothing). Actual value assigned is 0. Dim x As System.Nullable(Of Integer) = If(1 = 0, 1, Nothing) If x is a nullable type, why is it being assigned the default integer type of 0. Shouldn't it receive a value of null?
View 2 RepliesI wonder if this is not possible dim mystring as string="1" dim myint As Nullable(Of Integer) myint=0 myint=mystring It crashes on the last line why is that? mystring is already integer (1) , i dont have to cast using cint, i thought. option strict off- if this cause the problem
View 7 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've been pulling my hair out over some unexpected behavior from nullable integers.If I set an Integer to Nothing, it becomes Nothing as expected. If I set an Integer? to a String that is Nothing, it becomes 0! Of course I get this whether I explicitly cast the String to Integer? or not.[code] Previously I had my code up here so without the explicit conversion to Integer? and everyone seemed to be fixated on/confused by that. There were a lot of suggestions that Option Strict On would catch this type of thing. However, this is actually a quirk of the string-to-integer conversion rules which predate nullable types, but still impact them.
View 2 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've tried these two blocks but it returns the same value. I do not describe more, I simply just show the code:
Dim f As Nullable(Of Integer)
If f = 1 Then
Console.WriteLine("Equal")
[Code]....
It works in C# correctly...
How can I create a nullable numeric optional parameter in VB.NET?
View 3 RepliesI'm currently working with a client's VB.Net code, which was developed for them by a small development shop a few years ago and which they purchased and have been maintaining and uprgrading since. This client's primary developer is out on indefinite (likely permanent) medical leave and I'm now filling in until they bring in a full timer (as I'm a contractor here). My current task is to add some functionality to a the VB.Net code they purchased. I'm finding practices and techniques in the code that absolutely baffle me and can't make the code do what I want. I'm starting to wonder if it's me and was hoping to get some thoughts on the code I've encountered.
For example: Setting a variable to accept the result of a function by calling the function with many parameters, clearing the parameters in the function, setting them to some value, calling another function with those new values, then never using the values returned by the functions. I'll add a code snippet in the first comment since this is already getting long.
My understanding of the Integer.TryParse() function was that it tried to parse an integer from the passed in string and if the parse failed the result integer would remain as it did before.
I have an integer with a default value of -1 which I would like to remain at -1 if the parse fails. However the Integer.TryParse() function on failing to parse is changing this default value to zero.
Dim defaultValue As Integer = -1
Dim parseSuccess As Boolean = Integer.TryParse("", defaultValue)
Debug.Print("defaultValue {0}", defaultValue)
Debug.Print("parseSuccess {0}", parseSuccess)
[Code]....
The code performs correctly when the input is an integer, but when I enter a character, the result is as though I've entered an integer. Can you tell me what's wrong with my TryParse statement?
Private Sub btnCompare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompare.Click
' Declare Local Variables
[CODE]...
I am having trouble with using sumifs excel function in vb.net. I can use sum function with "cells" but I could not get result from sumifs function with "columns".
It is giving "OleAut reported a type mismatch." error.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim oExcel As Object
[Code].....
I have a problem with Asc() function as a blow picture,I will explain each step as I do.First I convert some values to ASCII code and store in value variable, then retrieve 3rd position of variable (4th line of the picture.)
and use Asc() function to find result which it show 63. (like 5th and 6th line).But I try to manually use Chr() function, the result become 2nd and 3rd line.So for the Asc("ˆ") func. result should be 63 or 136?
I am using the following code in my project. I want to find the number of days by given the last date and now.[code] for example, when BorrowDate is "01/Jul/2011" then the result is 7 days which it should be 10 to now.
View 1 RepliesWhen I was coding a program, I was able to find the result of a function in the Immediate Window by using:
View 3 RepliesI have something that I want to do and what looks very simple, but I cannot get it to work. I want to build a function that gets as input a xdocument and some other strings. It should have a return that contains a filtered part of the xdocument in a way that I can use it as input for a new Linq function. This is what I mean:
Private Function fncGetFilteredDecendantsOfNode(ByVal xdocDoc As XDocument, _
[Code]...
I am working on a project that requires me to hop into to separate DB's. So I have figured that I need to have multiple functions inside of my VB page. The only problem I am having,is I am not to sure how to get this all accomplished. So far I have figured out the overall structure, just need to implement that structure.
Here is my idea:
The main Function would call two other functions. We can Call them Sub Function 1 and Sub Function 2. So, the main Function takes the saved sessions information for the E-mail address and dumps in into Sub Function 1. It needs to open up a new connection to the db/stored procedure and RUN the following procedure and then return the result. Here is the stored procedure and what I think is correct.
CREATE PROCEDURE WEB_User
(@EMAIL_ADDRESS varchar(80) = [EMAIL_ADDRESS])
AS SELECT MEMBER_NUMBER
FROM WEB_LOGIN
WHERE EMAIL_ADDRESS = @EMAIL_ADDRESS
So my question is, what is the function suppose to look like? how do I send the session information to the procedure? and finally, how do I return the stored procedure results and push back into the main function so it can be carried into sub function 2?
This is just an efficiency question really.. I'm interested to know if there is a more efficient or logical way that people use to handle this sort of scenario.
In my asp.net application I am running a script to generate a new project my code at the top level looks like this:
Dim ok As Boolean = True
ok = createFolderStructure()
If ok Then ok = createMDB()
[Code]....
I create a boolean and each function returns a boolean result, the next function in this chain will only run if the previous one was successful. I do this because an asp.net application will continue to run through the page life cycle unless there is an unhandled exception and I don't want my whole application to be screwed up if something in the chain goes wrong (there is a lot of copying and deleting of files etc.. in this example).
I want to use explicit conversion (CType function in VB.NET) to convert the variable d from double type to integer type and store the result as 132.
Imports System.Console
Module Module1
Sub Main()
[CODE]...
This is my code. But Visual Basic tells me an error: Error1Type 'i' is not defined.C:\Users\mihaispr\Desktop\Conversie explicita\Conversie explicita\Module1.vb1922Conversie explicita
I need to hash a number (about 22 digits) and the result length must be less than 12 characters. It can be a number or a mix of characters, and must be unique. (The number entered will be unique too). For example, if the number entered is 000000000000000000001, the result should be something like 2s5As5A62s. I looked at the typical, like MD5, SHA-1, etc., but they give high length results.
View 5 RepliesI am trying to format a Function Code so that it returns a 4 digit result no matter the entry by the user (i.e. they enter 540, stores 0540 in the database). While looking online, I've found the format function, but when I use it, it returns 540, regardless if I enter 540 or 0540. [Code]
View 2 RepliesCan someone on the VB language team explain why a function result type is not part of the parameter signature?If it were then function overloads would also work for functions that only differ by the result type. In some cases, this is useful (aka cleaner code) especially when you have strongly typed data.
Other languages have this and implement it by considering the function result as an OUT parameter either as param 0 or at the end of the param list (stack) so existing type matching verification can be performed by the compiler.
In VB, the workaround is just messy and would be easily fixed by a language change. This example is overly simplified and not meant as a solid justification for the request Say you have a parse requiirement that takes a comma delimited string in the form: "31,32,33,34" or "bob,carol,ted,alice"
In you code,
[Code]...
I am interested in developing a multi-language program that will have a main .NET GUI but will perform calculations by calling procedures from a Fortran95 dynamic link library file. Right now I am just playing around with a very simple project in order to get the interoperability between the .NET framework and a Fortran95 (*.dll) file working correctly. So far, I've been able to pass the Fortran95 (*.dll) file an array from VB, sum all the elements of the array using an intrinsic Fortran function, and pass the scalar result back to the .NET framework with no problem. However, if I try passing Fortran the exact same array, construct the transpose of that array using another intrinsic Fortran function, and try passing the transposed array back to the .NET framework I keep throwing an exception. I am not a programming wiz, but it appears that when Fortran stores an array in memory, the .NET framework doesn't know how to read that. I am looking for some guidance on how to pass an array from the Fortran95 (*.dll) file back to the .NET framework.
View 9 RepliesI have a whole slew of database access functions which assume a particular connection string. Within my application I call
[Code]...
I see some functions within some code that are:
Public Function GetDataObjects(Of Customer)(ByVal dataset as DataSet)
...
End Function
What exactly does the (Of Customer) do in this instance or mean?
I have a function in a module that returns a datatable.I need to call that function asynchronously and get the results in the Main thread (Main Form) in vb.net.
View 2 RepliesI would like to allow my users to decide where they want their data to reside. I have created a data source which is referenced by crystal reports. I would like to replace the |DataDirectory| to point to a path the user selects. or if possible substitute it with vb function.
Below is the line in xml that point to my db. I would like to allow it to call a vb function that will return my db path I am not sure how to replace the connection string by using a vb function in xml.
<Connection ConnectionStringObject="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|MY_DB.mdb;Persist Security Info=True;Jet OLEDB:Database Password=pass" IsAppSettingsProperty="false" Modifier="Assembly" Name="MY_DB.mdb" Provider="System.Data.OleDb" />
I would like to replace |DataDirectory| with the result of a function called MyDBPath(), can anyone assist.
I need top create a function that gets the next integer in the sequence of 1 to 60. Each time the function is called, I need it to increment from what the last value was and return the next number as the result. It also cannot return the same integer in 2 consecutive calls. When the count gets to 60, the sequence needs to reset back to 1. I would like to function to have no parameters.
I have created a function that does this using the cache to store the last value that was returned, but I am worried that with concurrent calls to the function that it might not work as I expect and 2 calls could get the same integer.
Is cache blocking the correct method to use in this instance or is there something other method I am not thinking about?
Also, stuck using .Net 3.5 and this is a web application.
I just wrote this function based on using SyncLock. Any obvious problems with it that I don't see? I am using the Cache to save the last value that was returned.
Private Shared Function GetNextNumber() As Integer
Dim o As Integer
Dim r As Integer
[Code].....