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.
Related
Trying to program 'with immutability in mind' in VB.NET can be a challenge.
In many cases, creating classes with a parametrized constructor and properties with no setters is a pretty good way to create objects that are immutable (from outside the object).
Sometimes it would be more practical to just use the VB.NET ReadOnly keyword, but:
If the data type of the variable is a reference type, such as an array
or a class instance, its members can be changed even if the variable
itself is ReadOnly.
See here.
Is there a code analysis tool or simple solution that can give me a compilation warning if for instance a string array that is declared ReadOnly has one of its members changed?
That would make the ReadOnly keyword so much more useful. Or is it possible to define such a rule myself in Visual Studio 2022?
Not striving for (functional programming-esque) purity but such an extra check to make the ReadOnly keyword more useful would be pretty handy, so hopefully there is a straightforward way to get such a code check!
The VB.NET Static declaration:
http://msdn.microsoft.com/en-us/library/z2cty7t8.aspx
The only reference I can find to this question is from 2008:
http://forums.asp.net/t/951620.aspx?what+is+the+equivalent+of+static+from+vb+net+in+c+
Is there an equivalent in recent versions of C#, or still not present? Is there anything particularly wrong about using Static in VB.NET?
C# does not support it and probably won't be because it somehow violates object programming idea of state being part of object, not a method.
Of course one can say that it is only syntactic sugar, and he/she will be event quite right. But still, looking through class code, we expected description of its state variables as a fields of class. Otherwise we should find for it in each and every method.
So this can be simply seen about some high-level decision and your millage may vary here.
Personally, I like the VB procedure-level Static, even if it's not "pure" enough for some folk.
You can set it in declaration and forget it:
Static oClient As HttpClient = New HttpClient()
There's no checking whether a module-level variable needs to be instantiated or not:
If moClient Is Nothing Then moClient = New HttpClient()
And, silly me, I always expect that there is equivalency between C#.NET and VB.NET but I've learned that clinging to that concept is folly, unfortunately.
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.
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.
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.