Small VB.NET questions

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Small VB.NET questions

Post by agibsonsw »

Hello.

Slightly puzzled by the following:

Dim strWot() As String
strWot = New String(10) {}

I thought that VB.Net doesn't allow/use fixed length strings? And I know curly brackets are used to initialize arrays {}, but why the empty brackets :scratch: ?

And how do I indent or outdent code? In VBA I could just press Tab or Shift-Tab?

Thanks, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78512
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Small VB.NET questions

Post by HansV »

String(10) is not a string of length 10, it is an 11-element array of type String (index 0...10). The { } initializes it to an empty array. You could have used something like

strWot = New String(3) {"this", "that", "other", "what"}
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Small VB.NET questions

Post by agibsonsw »

@Hans Thank you very much for coming back to me :cheers:

I was leaning that way but the brackets occurring after 'String(10)' threw me.

I couldn't see the purpose of { } but I now realise that they are necessary. If I try:
strWot = New String(10) 'no curlies
I receive the error message:
Value of type 'Integer' cannot be converted to '1-dimensional array of Char'

So I believe that 'String(10)' is the older VBA syntax for a fixed-length string and, although VB.Net doesn't support it, it is stil there.

(I seem to have lost the ability to indent code in VB.Net?)

Regards, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78512
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Small VB.NET questions

Post by HansV »

Take a look at Tools > Options... > Text Editor > Basic > VB Specific.
Or at Tools > Options... > Text Editor > Basic > Tabs.

Experiment with the settings.
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Small VB.NET questions

Post by agibsonsw »

@Hans Thanks, I'll play :cheers: (might have to turn off smart indenting).
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.