By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,369 Members | 966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,369 IT Pros & Developers. It's quick & easy.

Variable declaration - 3 Ways - What are the differences?

P: 3
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

Share this Question
Share on Google+
4 Replies


Oralloy
Expert 100+
P: 983
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
Expert 5K+
P: 8,628
  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

P: 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
P: 82
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

Post your reply

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