Those of us who've worked in VB/VB.NET have seen code similar to this abomination:
Dim name As String = IIf(obj Is Nothing, "", obj.Name)
I say "abomination" for three simple reasons:IIf is a function, all of whose parameters are evaluated; hence if obj is nothing in the above call then a NullReferenceException will be thrown. This is unexpected behavior for someone who's accustomed to short-circuited ternary operators in languages like C#.Because IIf is a function, it thus incurs the overhead of a function call. Again, though this isn't a big deal, it just doesn't feel right to someone who expects for it to behave as a ternary operation intrinsic to the language.IIf is non-generic and therefore accepts parameters of type Object, which means the following call boxes (I believe) a total of three integers:
' boxes 2nd and 3rd arguments as well as return value 'Dim value As Integer = IIf(condition, 1, -1)
Now, in some more recent version of VB.NET (I'm not sure what the number is), the If operator was introduced, which works exactly the same way as the IIf function but (as I understand it) without the same shortcomings. That is to say, it does provide short-circuiting and it is an intrinstic VB operation. However, I'm not sure about the last part. The MSDN documentation doesn't seem to indicate whether If boxes its arguments or not.
I need a list of booleans (size anywhere from 200 to 200k). If i use System.Collections.Generic.List<bool> I'm going to suffer from serious boxing issues (correct me if I'm wrong). What are the alternatives? (of course i know i can use a boolean array but i need to be able to add and remove things easily from the array)
Any variable of type Nullable<T> can be assigned to null. For instance:
int? i = null;
At first I couldn't see how this would be possible without somehow defining an implicit conversion from object to Nullable<T>:
public static implicit operator Nullable<T>(object box);
But the above operator clearly does not exist, as if it did then the following would also have to be legal, at least at compile-time (which it isn't):
int? i = new object();
Then I realized that perhaps the Nullable<T> type could define an implicit conversion to some arbitrary reference type that can never be instantiated, like this:
public abstract class DummyBox { private DummyBox() { }
[code]....
But this seems to imply that there is a custom implicit conversion from Nullable<T> to object:
public static implicit operator object(Nullable<T> value);
This is clearly not the case as object is a base class for all types, and user-defined implicit conversions to/from base types are illegal (as well they should be).It seems that object x = i; should box i like any other value type, so that x.GetType() would yield the same result as typeof(int?) (rather than throw a NullReferenceException).
So I dug around a bit and, sure enough, it turns out this behavior is specific to the Nullable<T> type, specially defined in both the C# and VB.NET specifications, and not reproducible in any user-defined struct (C#) or Structure (VB.NET).This particular boxing and unboxing behavior appears to be impossible to implement by hand. It only works because both C# and VB.NET give special treatment to the Nullable<T> type.
Isn't it theoretically possible that a different CLI-based language could exist where Nullable<T> weren't given this special treatment? And wouldn't the Nullable<T> type therefore exhibit different behavior in different languages?How do C# and VB.NET achieve this behavior? Is it supported by the CLR? (That is, does the CLR allow a type to somehow "override" the manner in which it is boxed, even though C# and VB.NET themselves prohibit it?)Is it even possible (in C# or VB.NET) to box a Nullable<T> as object?
I am creating an application that parses an XML and retrieves some data. Each xml node specifies the data (const), a recordset's column-name to get the data from (var), a subset of possible data values depending on some condition (enum) and others. It may also specify, alongside the data, the format in which the data must be shown to the user.
The thing is that for each node type I need to process the values differently and perform some actinons so, for each node, I need to store the return value in a temp variable in order to later format it... I know I could format it right there and return it but that would mean to repeat myself and I hate doing so. So, the question: How can I store the value to return, in a temp variable, while avoiding boxing/unboxing when the type is unknown and I can't use generics? P.S.: I'm designing the parser, the XML Schema and the view that will fill the recordset so changes to all are plausible.
I am tightening up my coding with the Option Strict set to ON. It has now produced alot of errors. An example of this is:
If AllocatedDGV.Rows(i).Cells("RoomNumber").Value = RoomsAvailableDGV.Rows(j).Cells("RoomName").Value Then
It gives me the following error: Option Strict On disallows operands of type Object for operator '='. Use the 'Is' operator to test for object identity.
I need to write an interface to get data to/from our data files.
We have a low level class that holds field values for each record read from the files.
This just holds two values, the value read from the file (DBValue) and the updated value that may need to be written back to the file (CurrentValue).
These values may be any of the standard value types (integer, date etc) or a string.
Either value (DBValue or CurrentValue) may be null if not defined.
I have written the class to manage this data which works fine while option strict is NOT on.
But we have an office policy of having option strict on all the time.
When I put option strict on, my object value comparisons fail with the error: "Option Strict On disallows operands of type Object for operator '='. Use the 'Is' operator to test for object identity."
Question, how should I change the following code to handle option strict on ...
What does the ! mean in the following code snippet?Availability is a table in an access database Family and Model are column headers in the table cmbFamily and cmbModel are combo boxes.
If Availability!family = cmbFamily.Text Then cmbModel.Items.Add(Availability!model) End If
add the models of as aircraft family to a combobox(cmbModel) using a selection from another combobox(cmbFamily).
came across this issue while converting a for loop in C# to VB.net I realized that the increment operators are not available in vb.net (++ and --)whereas i was able it do something like cnt +=1 .In VB, a STATEMENT cannot be just an EXPRESSION. why this doesn't work in the same way as it does in C#.
I am trying to get data from my Access database using like operator. For example my ProducID contains NoteBook, Notes etc. Using like operator, I want to get them all shown if user types No etc.Like operator did not give any errors but it only shows record when I put full NoteBook (does not show for Notes offcourse)
dv.RowFilter = "Convert([OIMainID], 'System.String') Like '" & OIMainIDTextBox.Text & "%'""
i'm almost getting the result i want. What i want is an exact match in the filter so if the box only contain the number 1 to will only show rows with the number 1 and not return say 12 13 14 etc etc, and vice versa for other numbers.
Is there any way to pass in an operator in VB.NET? I'm looking to reduce my lines of code and for two functions there is literally only an operator that is different.For example, I have two functions, Darken and Lighten. I'd like to get to a single function with as little code as possible. The only difference are Greater Than and Less Than operators.
Function Darken(ByVal clr1 As Color, ByVal clr2 As Color) As Color Dim newR = If(clr2.R < clr1.R, clr2.R, clr1.R) Dim newG = If(clr2.G < clr1.G, clr2.G, clr1.G)
how do i randomize the operator?? i mean i wan the operator occur to be in random .i have to rush out my miniproject on next wednesday so i do ask the problem i meet here
i want to check if datasets have changes and after that use msgbox (vbYesNoCancel) when program closes ,when i wrote this code it had error "Operator 'Or' is not defined for types 'System.Data.dataset' and Boolean"..[code]
In the project�s btnReadNumber click event handler, replace the code in the try section of the try/catch block � the two if statements and the unconditionally executed code � with one if-else statement that performs the same tasks. Use the And operator to write the if-statement�s condition.What I don't understand is how am I supposed to use the 'And' operator so that when I insert a number between 32-16384 in the text box it'll allow it to go through and at the same time not allow any numbers lower than 32(lower_bound) and higher than 16384(upper_bound) to go through.I've use the 'Or' operator and that was simple, but with 'And' I'm not understanding it.Remember it has to be with the 'AND' operator. I bold the area where my problem is at. It's obviously not the right answer, but that's where I was stumped.
Dim sum As Integer = 0 Const upper_bound As Integer = 16384 Const lower_bound As Integer = 32
So Google's not a good choice for looking up examples for overloaded operators like "And" or "Or", because it tries to parse them as operators to the search query itself. MSDN also provides no examples of how to implement an overloaded And operator, so I'm not certain how to properly overload it for my project.Does anyone have an example of "And" at minimum? "Or" or "Xor" (or any others) would be a bonus. I'm not certain if I need to overload these operators in my objects just yet, as I'm still building them out and haven't planned beyond just yet. But having examples around that might get indexed by Google will probably help save the sanity of a lot of people...