option strict question - vb.net

Late binding is not allowed and that's how we want it.
Depending on whether our program is running on a LAN or the Internet, we need to dim an object as one of two types.
We use an if...then statement to ascertain whether or not we are running on a LAN or the Internet.
When we declare our object inside the if...then statement, we have declared in the wrong scope and cannot use the object.
When we declare it as an 'Object' type and use DirectCast inside an if...then statement, we receive a late binding error.
How can we get around this problem without turning off Option Strict?

I haven't used vb.net - so consider the source. But can't you declare each of the types to be a subclass of another type, and have that superclass type as the declared type of your object?

As far as I know that's essentially what I'm doing when I try to use the Object class for variable initialization.
However, when I attempt a DirectCast to narrow it down to the type I need, it still throws the late binding error.

Related

Are ByteBuddy's field setting checks too strict?

I am using MethodCall.setsField() to try to set an instance field on another instance.
My generated class that is doing the field-setting, GC, is trying to set the value of an instance field in an instance of something it has created (CI). So the field's declaring type is CI; my field-setting code resides in GC (which is in the same package as CI but otherwise unrelated to it).
The ByteBuddy checks seem to indicate that although GC and CI are in the same package, GC must be assignable to CI in order to set this field! That greatly surprised me, but I am not a bytecode expert, and I might very well be overlooking something obvious. Could someone kindly explain why this check is necessary?
The method call sets the field implicitly on the this instance on which the method is invoked. For this to be possible, a non-static field must be declared by a super type of the type on which the method is invoked.
If you think this is too strict, please file an issue with an example of the code you are trying to generate, including the code to generate it which is currently failing. Maybe I am not thinking straight about this and if there's a restriction to be lifted, I would surely do it.

Correct terminology for "obj.X" VB.net?

I am reviewing some code and I realized I don't remember the correct terminology for something. I believe if I had the following code
pnlOne.Visible = False
Would the "visible" part be considered a method, function, or what? I am learning VB alongside JavaScript, and in JS it would be a method. Is it the same for vb?
In VB.net, that is a "property". Properties in VB.net and C# as essentially glorified methods for getting and setting a value. (They actually compile down to something like get_Visible and set_Visible methods.)
pnlOne is an instance of a class and Visible is its property
Visible could be either ..
a Property; or
a Field (called "Member Variable" in VB)
.. depending on how it is declared. Both Properties and Fields are specializations of "Members"1. See Differences Between Properties and Variables in Visual Basic.
I suspect Visible is a Property in this case, and it will be for all standard Control types .. however, to verify this either way requires knowledge of the Type of the object named by pnlOne.
1
Methods (or "Sub/Function Procedures") are a different kind of Member and it is not appropriate to call either a Property or Field a "Function" or a "Method". (Note: various references inconsistently make a distinction between a Method and a Procedure; in VB.NET they an be thought of as synonyms.)
Nit: the correct term in JavaScript would be property; properties can evaluate to function-objects and can thus also can be considered methods when they do so - usually when this is used meaningfully. In any case, the code would have to be different (e.g. jsObj.set_Visible(true)) if a method was used.

Information on OOP, creating Objects

I have a problem in understanding OOP...
This is it :
Sometime's you create an object with this syntax:
Object ObjectName = new Object();
But sometimes, we don't need to do that like in Android:
Textview TextviewName;
Or in J2ME:
form formName;
I already searched it and I got some information (but not sure) that this is because of static method... is it true? I think it has a relation with Polymorphism.. is it true?
Thanks all.
PS : Sory if I made some mistakes, English is not my native languange :D
Forget static methods - they're not relevant here. I'd advocate only looking at static methods / elements when you've truly grasped what objects are.
In Java, you can do this:
Object object;
Just as well as you can do this:
Object object = new Object();
In the first example, you're creating a reference but you're not populating that reference with anything, in the second example you're creating a reference and populating it with a new object, on which you can call methods, modify values and so on.
If you try and call methods on the first declaration you won't be able to - there's nothing there. Depending on the language and how you've declared it this might produce an error at runtime or a compile time error (Java does both depending on whether it's a field or a local variable.) The principle though is the same for all OO languages, you can't dereference (call methods, fields, etc.) on a reference that hasn't been populated, because in effect you're trying to call a method on something that isn't there.
you are mixing different languages and it's not the case of static methods nor polymorphism..
i suggest to read a good book of OOP beginning with the basis.. you can find "Thinking in c++" for free on the net..
Your Textview would not be initialized. Any try at using it would result in a NullReference error. In order for an object to actually be created, you have to use the new syntax or a function that returns a valid object.
However, this is a syntax-dependent issue, so first decide what language you want to study. If your Textview had been declared in C++, it would actually create an object, on the stack.

How does VB.NET resolve this to an object property?

Ok, so I'm a C# coder, and I have trouble even reading VB.NET, so please forgive what may turn out to be an incredibly dumb question. But I have some code which looks like this:
Function GetName(sourceObject as Object) as String
return sourceObject.Name
End Function
So, ignoring the fact that the syntax is probably wrong - how does VB.NET get the Name property from sourceObject? Inspecting it at run-time reveals that sourceObject is of a type that supports a property called Name with a getter, but what does VB.NET do in this case? Is there some extra code that is being generated by the compiler to somehow cast this automagically at run-time?
As you may be able to tell, I'm a little confused. Thanks in advance!
This is a case of late binding. So, if the sourceObject contains a property as Name it will return the value otherwise it will just throw an error that property not found.
Late binding in C# requires reflection so it throws compile time error, whereas in vb.net it can be done without reflection.
The Visual Basic compiler performs a process called binding when an
object is assigned to an object variable. An object is early bound
when it is assigned to a variable declared to be of a specific object
type. Early bound objects allow the compiler to allocate memory and
perform other optimizations before an application executes. By
contrast, an object is late bound when it is assigned to a variable
declared to be of type Object. Objects of this type can hold
references to any object.
If you are using c# 4.0 then you can try this.
return ((dynamic)SourceObject).Name;
Because of inherent late binding, all VB variables of type Object are equivalent to c# objects which have been cast to dynamic -- or to put it another way, c# added dynamic in order to gain functional parity with VB.
So, the equivalent c# code would be ((dynamic) sourceObject).Name
I would look into doing this with Generics or an interface if possible, as thats a cleaner design.
Just be aware that VB's Late Binding bypasses type checking with your compiler. If the Object being passed in doesn't have a Name property, an exception could be raised at run time.

Force parentheses even when calling parameterless functions in VB.NET?

in VB.NET it is possible to omit parentheses when you call a parameterless function. However this can be very confusing because developers could think that a statement is accessing a property instead of a method. this could result in a performance drop if you are calling the method again and again instead of storing the result in a temp variable.
is there an option in VS2008 or a compiler option to force parentheses on statements that are calling a method?
and if so, would it be also possible that VS will insert missing parentheses automatically if you "format document" (Menu: Edit - Advanced)?
thanks, toebens
No there is no such option in the VB.Net compiler. Parens are optional and there is no warning or error that exist for using a lack of them.
The other reason is that VB.Net is a language which tries to be flexible and get the syntax out of the way of the user. This type of restriction goes against this general philosophy.
Another issue to consider is that it's not a universally enforceable restriction. VB.Net allows for late binding scenarios whenever option strict is set to off. In these scenarios it is impossible for the VB.Net compiler to determine ahead of time if a particular call is a property, statement or not a valid call at all.