Declaring Constant Arrays?

Status
Not open for further replies.

HedgePig

Board Regular
Joined
Jun 27, 2002
Messages
146
Hello

Can I declare a "constant array" in VBA?
What I would like to do is set up a constant which contains the number of days in each month (ignoring leap years), e.g.

Const DaysInMonth(12) as Integer = Array(0,31,28,31,30,31,30,31,31,30,31,30,31)

However, I'm unable to find the correct syntax. Is this possible in VBA? (I'm using Excel 97)

Regards
Hedges
 
{snip}

I hate to keep on, but could you give me one piece of unsound reasoning in any of my comments regarding an array of constants in VBA?

I don't understand why you would need to do this.

What is the difference between defining this array as a constant, and just defining it by hardcoding the values in to be compiled at run time?

It makes no difference as far as I can see...
For all those so enthusiastic about criticizing the OP#2 (Kelly), what was he supposed to do?

Teach you about the difference between a constant and a variable? Do the job that whatever university you went to failed to do? By claiming there is no need for a constant array (and by implication for a constant) you are revealing an alarming ignorance of one of the basic building blocks of good software (at least by contemporary standards).

Short of posting a discourse on the subject, I fail to see how it was rude of Kelly to ask you to read up on the subject.

VB(A) doesn't support a lot of critical capabilities that should exist in a modern software language. That, however, is not a license to criticize someone who asks about them. Nor is it a license to claim that there's no need for those capabilities.
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
{snip}

The original post is approaching five years old now. Can an array of constants be declared?
No, unfortunately, not. I might be nice to have that capability but not only is it not available in VB(A) but from the little I've used .Net, neither VB.Net nor C# support the concept.

You can simulate the effect with a function but that's the best you can do. For example, to get the days in a month, a function with the signature DaysInMonth(Mth, Year) might do the job of simulating a constant array.
 
Upvote 0
I see the post I had believed to be consigned into the past has reared its ugly head again.

I feel I must defend myself here to quite an extent.

In my time on this board, approximately 90% of the questions I get involved in are from posters who want to achieve fairly rudimentary things in Excel using VBA. Naively I suppose, I assumed this post was coming from an angle I am all to familiar with, and with which I try to come up to solutions with my own programming problems. I assumed that an a point in the development of his software, the OP2 had perhaps come across a barrier to which there was only one solution. So I suggested a workaround, that I thought would befit his relatively unsophisticated needs. As it turns out, his needs were much more sophisticated than I assumed, and therefore I failed to see the need for constants when perhaps they would be useful.

So you see, my post was not ridicule at his situation, or his techniques, it was more a sense that he had missed the kinda obvious workaround, as it appeared to me.

So it comes down to previous experience on this board, with other posters, and the typical situations encountered. It also comes down to the choice of platform (VBA), within which I would never have imagined such a intensive process to be managed, and it also comes down not to ignorance of programming concepts, but more a lack of information about the context of the post.

And tusharm, I don't know how you can't see the rude and aggressive nature of the posts, but many others have, so I thank them at least.

Again I have to defend my professional integrity, and to that I say this. We're not, as you say, programming in an environment that supports constant arrays. I have, and do, program in environments that support constant arrays. This is a forum for Excel, and at best VBA. 5 years of forum silence answered that. My comments on the motivation for such a data structure have been misread out of context, in so far as the lack of information about the reasoning for the OPs needs in the first place. More information about that motivation is needed than 'There is a need for a constant array'. I have no clue as to his background, his project, his skillset, and I offered a solution that was jumped upon as displaying a lack of competence when all I really tried to do was help.

So yeah, thats my piece. Sorry for taking the time to even look at this and offer my help in the first place. Thanks for digging this up again Tushar, what a great guy you really are.
 
Upvote 0
Very well put Patrick.

I too cannot understand how Tushar could not have picked up on this guy's rudeness.
 
Upvote 0
I'd like to add that I thought Bryan made a quite magnanimous apology in his last post above - to me it's clear he did not set out to offend and has apologised for any offence he did inadvertantly cause. It's time to let the matter drop.

This is becoming tedious for the rest of us who like to use this forum for it's intended purposes. Can a moderator please either lock or move to lounge?

Thanks
Jon :roll:
 
Upvote 0
Jon,

I say leave the thread in this forum ... I don't see why rudeness shouldn't be tackled, and this thread shows that many members are not prepared to sit back and let it happen. You may think differently.
 
Upvote 0
Jon,

I say leave the thread in this forum ... I don't see why rudeness shouldn't be tackled, and this thread shows that many members are not prepared to sit back and let it happen. You may think differently.

It's not so much that it [rudeness] shouldn't be tackled as you put it, but that it has already been covered and laid to rest. The problem is that the "rudeness" topic keeps creeping back up when people are simply ready to (and in some cases already trying to) get back to discussing the subject at hand: arrays.
 
Upvote 0
Von Pookie, it had been laid to rest until Tushar resurrected it. He did not just discuss the subject at hand: arrays, but couldn't understand peoples reactions to OP2, and said so quite strongly.
 
Upvote 0
You're completely correct, again, Glenn.

If you read many of the posts on this forum, as I have, you will notice a trend with posts. That trend is that people often ask questions, under the presumption that the question they pose will help them fix their solution. All to often, however, it emerges that if more detail is given on the actual problem (as opposed to the solution they believe will fix it), other caveats to the problem can be unearthed that lead to the conclusion that the OP's question can actually be reposed in another form, leading to an answer that satisfies what is the orginal issue.

For an MVP to read the above quoted section of my post (or at least I hope he read it, i'm not so sure...)

and then respond with such scathing criticism as:

Teach you about the difference between a constant and a variable? Do the job that whatever university you went to failed to do?

when I have made it clear in other postings that you cannot declare a constant array (for clarity, the you is singular, not plural - referring to his situation, not the fundamentals of programming), moved swiftly on, and suggested just using a variable array and leaving it alone in code.

Makes me a) despair at the lack of insight provided by an MVP, and b) wonder what the P actually stands for in that particular TLA.

I'm done with this anyway. I've moved on, let's all do the same.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,223,630
Messages
6,173,457
Members
452,516
Latest member
archcalx

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top