473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Variable declaration - 3 Ways - What are the differences?

Those 3 ways will work equally the same:

Dim CultFr As CultureInfo = New CultureInfo("fr-FR")
Dim Cult_frFR = New CultureInfo("fr-FR")
Dim Cult_fr1 As New CultureInfo("fr-FR")

This example illustrate the CultureInfo object but it could be any other objects.

Can someone please specify in which situation it is better to use one over the other ones?

Thanks
Oct 1 '16 #1

✓ answered by IronRazer

The answer to your question is, the 1st and 3rd way are basically the same thing, just written differently. They both would create a strongly typed instance of the CultureInfo class. I would strongly recommend using either of these over the 2nd way.

Dim CultFr As CultureInfo = New CultureInfo("fr-FR")
Dim Cult_fr1 As New CultureInfo("fr-FR")


The 2nd way would create a new instance of a CultureInfo class but, it is not strongly typed. Code which has strongly typed variables has a few advantages such as your code will execute faster.

This is part of why i always recommend turning Option Strict on and always use it myself too. If you look at the msdn link below and read through the Remarks, you will see other reasons why too.

https://msdn.microsoft.com/en-us/library/zcd4xwzs.aspx

4 1153
Oralloy
988 Expert 512MB
Karim Saikali,

My apologies if this sounds rude - I'm presuming that you have academic curiosity, and that this is not an on-line test type question...

This link might help: Dim Statement (Visual Basic)

From what I can see in the Microsoft documentation, none of those forms should work. Did you try testing anything?

Of course, it is possible that the VBA compiler syntax has been extended, so - I'm guessing a little bit, but here are my thoughts:
  1. CultFr is type CultureInfo, and initialised.
  2. Cult_frFR is a variant (it can be any type), and is initialised with a CultureInfo.
  3. Cult_fr1 is type CultureInfo, and initialised. May be that Cult_fr1 cannot be assigned, I do not have the ability to test this right now.

Testing this should be trivially easy, assuming that you have any of the uSloth office tools. Just open the code browser and run a few tests.

This is a test script that I would try:
Expand|Select|Wrap|Line Numbers
  1. ''--first test is if this even compiles...
  2.  
  3. ''--variable construction
  4. Dim CultFr As CultureInfo = New CultureInfo("fr-FR")
  5. Dim Cult_frFR = New CultureInfo("fr-FR")
  6. Dim Cult_fr1 As New CultureInfo("fr-FR")
  7.  
  8. ''--test for assignability
  9. Set CultFr = New CultureInfo("fr-FR")
  10. Set Cult_frFR = New CultureInfo("fr-FR")
  11. Set Cult_fr1 = New CultureInfo("fr-FR")
  12.  
  13. ''--test for Null'ability
  14. ''--May need to use Nothing in place of Null; please update appropriately. 
  15. ''--  Choice of Null versus Nothing has everything to do with primitive values versus objekts. 
  16. Set CultFr = Null
  17. Set Cult_frFR = Null
  18. Set Cult_fr1 = Null
  19.  
  20. ''--test for type restriction by assigning a date 
  21. CultFr = #1/13/2004#
  22. Cult_frFR = #1/13/2004#
  23. Cult_fr1 = #1/13/2004#
  24.  
  25. ''--test for sub-class assignment
  26. Set CultFr = New SubclassOfCultureInfo("fr-FR")
  27. Set Cult_frFR = New SubclassOfCultureInfo("fr-FR")
  28. Set Cult_fr1 = New SubclassOfCultureInfo("fr-FR")
  29.  
I am curious as to how the statements do differ.

Will you please run the tests and let us know what you find?

Luck!
Oralloy
Oct 2 '16 #2
ADezii
8,834 Expert 8TB
  1. I am with Oralloy on this one in that I am a little confused. If you are attempting to create an Object based on the CultureInfo Class, then the 2 Methods of Declaration and Instantiation are as follows:
    Expand|Select|Wrap|Line Numbers
    1. Dim MyObj As CultureInfo
    2. Set MyObj = New CultureInfo
    Expand|Select|Wrap|Line Numbers
    1. Dim MyObj As New CultureInfo
  2. To the best of my knowledge, the first Method is the more acceptable one.
  3. To then retrieve a Property of the CultureInfo Class (CurrentCulture), the Syntax would be:
    Expand|Select|Wrap|Line Numbers
    1. Debug.Print "The Current Culture is " & MyObj.CurrentCulture
  4. The Syntax you have posted is also foreign to me.
Oct 2 '16 #3
Dear Oralloy,Adezzi

Thank you for your answers, really appreciated, but it is not relevant to the question asked,

I am using Visual Studio 2015 and writing in Visual Basic language

The 3 ways of declaring a specific culture as below WORKS FINE ( i test it and provide the same variable value):
Dim CultFr As CultureInfo = New CultureInfo("fr-FR")
Dim Cult_frFR = New CultureInfo("fr-FR")
Dim Cult_fr1 As New CultureInfo("fr-FR")

So many ways to have the same result in creating a variable

Therefore my question was what could differ in the background when creating using 3 different way.
Oct 6 '16 #4
IronRazer
83 64KB
The answer to your question is, the 1st and 3rd way are basically the same thing, just written differently. They both would create a strongly typed instance of the CultureInfo class. I would strongly recommend using either of these over the 2nd way.

Dim CultFr As CultureInfo = New CultureInfo("fr-FR")
Dim Cult_fr1 As New CultureInfo("fr-FR")


The 2nd way would create a new instance of a CultureInfo class but, it is not strongly typed. Code which has strongly typed variables has a few advantages such as your code will execute faster.

This is part of why i always recommend turning Option Strict on and always use it myself too. If you look at the msdn link below and read through the Remarks, you will see other reasons why too.

https://msdn.microsoft.com/en-us/library/zcd4xwzs.aspx
Oct 7 '16 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
12
by: Michael B Allen | last post by:
Which style of local variable declaration do you prefer; put everything at the top of a function or only within the block in which it is used? For example; void fn(struct foo *f, int bar) {...
7
by: Method Man | last post by:
Can someone explain the scope/linkage differences between the following 4 global declarations and when one should be used (theoretically) over the rest? sample.h --------- #ifndef SAMPLE_H...
7
by: seamoon | last post by:
Hi, I'm doing a simple compiler with C as a target language. My language uses the possibility to declare variables anywhere in a block with scope to the end of the block. As I remembered it this...
5
by: widmont | last post by:
Hello, I would like to know the difference between two variable declaration ways: int point(0); and int point=0;
21
by: Kannan | last post by:
Its been a while I have done pure C programming (I was coding in C++). Is the following function valid according to standard C? int main(int argc, char *argv) { int x; x = 9; printf("Value...
14
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
15
by: vaib | last post by:
hi to all.i'd like to know the actual difference between variable declaration and definition.it would be very helpful if anyone out there wud help me out with this thing.i'm writing here after here...
1
NeoPa
by: NeoPa | last post by:
Problem Description : In VBA there is an option to enforce declaration of variables in your code. With this set, any reference to a variable that has not been previously declared (Dim; Private;...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.