Question Problem with Dim x As Object

nhmouta

New member
Joined
Sep 3, 2011
Messages
1
Programming Experience
3-5
Hello
I want to summarize this code because it is too long:
Dim Ad2 As Object
Dim Ad3 As Object
Dim Ad4 As Object
Dim Ad5 As Object
Dim Ad6 As Object
Dim Ad7 As Object
Dim Ad8 As Object
Dim Ad9 As Object...
To: Dim Ad268 As Object
Thanks!.. :smile:
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,570
Location
Sydney, Australia
Programming Experience
10+
That makes no sense. If you need 8 variables then you declare 8 variables. If you need one variable then you declare one variable. Code is only too long if it does unnecessary things.
 

Flippedbeyond

Well-known member
Joined
Dec 19, 2008
Messages
198
Programming Experience
1-3
i agree with Solitaire, but if for some reason you really do need to declare invidual variables, and they're all of the same type you can do something like this with the syntax, which shortens the code you need to write.

Ex:
Dim Ad2, Ad3, Ad4, Ad5, Ad6 As Object
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,570
Location
Sydney, Australia
Programming Experience
10+
Ah, now that I see Solitaire's post, I realise that I misread post #1. I thought that you meant that you wanted to summarise this:
VB.NET:
Dim Ad2 As Object
Dim Ad3 As Object
Dim Ad4 As Object
Dim Ad5 As Object
Dim Ad6 As Object
Dim Ad7 As Object
Dim Ad8 As Object
Dim Ad9 As Object
to this:
VB.NET:
Dim Ad268 As Object
but now I realise that you mean that you want to summarise this:
VB.NET:
Dim Ad2 As Object
Dim Ad3 As Object
Dim Ad4 As Object
Dim Ad5 As Object
Dim Ad6 As Object
Dim Ad7 As Object
Dim Ad8 As Object
Dim Ad9 As Object
'...
Dim Ad268 As Object
to whatever is appropriate. Sorry about that. In that case an array is most likely the best option, although there are various collection types, e.g. List and Dictionary, that may be more appropriate, depending on the exact circumstances. If you do use an array, just note that indexes start at zero and you need to specify the upper bound, i.e. the highest index, and not the length when you create the array.
 
Top Bottom