Setting based 1 Arrays

M

Michael C

Michael Williams said:
The documentation is extremely clear. It's not my fault you didn't read it
;-)

Ok, let me see mike. I start a copy of vb6 and type in this code

Private Sub Form_Load()
Dim x(10)
x(0) = 5
End Sub

hmmmm, seems to me that that array is zero based. If the documentation says
otherwise then it is wrong. Generally I don't have documentation from last
century on my PC so no I have no read it. However, some other areas of vb6
are most definately 1 based. Which areas are 1 based and which are zero
based appears to be random. You're not being very clear mike, perhaps you
can cut and paste this mysterious piece of documentation you refer to. Does
it say VB6 should be 1 based or zero based?

Michael
 
M

Michael C

Mike said:
"You might want to also look into .NET assemblies. Since MS
has kinda forgotten COM in favor of .NET assemblies, you
might want to look at creating the SDK into a .NET assembly
as well. These .NET assemblies can be called by normal
Visual Studio 6 as well as any .NET version that it was
compiled against."

So that is something else I have to get a handle to see what he means wrt
.NET Assembly.

This is the approach I would suggest as well. It's the easiest method for a
dot net developer to use your component because they just add a reference to
your dll and then everything appears in intellisense like other classes and
methods available to the developer. Depending on your business you might
gain more sales. It's very similar to what you did with ATL except that the
target is .Net instead of COM. You have the choice of doing it in C++ or C#
or vb.net. Writing it in C++ will be very easy (even I managed to do it).
Writing it in C# or vb.net will also be very easy and both will be able to
interact with your wcsrv2.dll. The advantage would be that it's probably
easier to work in the higher level language and you could release the source
code if desired. The advantage with C++ is that any include files you have
will work, you keep all your code in 1 language etc.

Michael
 
T

Tom Shelton

Ok, let me see mike. I start a copy of vb6 and type in this code

Private Sub Form_Load()
Dim x(10)
x(0) = 5
End Sub

hmmmm, seems to me that that array is zero based. If the documentation says
otherwise then it is wrong. Generally I don't have documentation from last
century on my PC so no I have no read it. However, some other areas of vb6
are most definately 1 based. Which areas are 1 based and which are zero
based appears to be random. You're not being very clear mike, perhaps you
can cut and paste this mysterious piece of documentation you refer to. Does
it say VB6 should be 1 based or zero based?

Michael

It depends on Option Base. Add:

Option Base 1

to the top of your module.
 
M

Mike

Michael said:
This is the approach I would suggest as well. It's the easiest method for a
dot net developer to use your component because they just add a reference to
your dll and then everything appears in intellisense like other classes and
methods available to the developer. Depending on your business you might
gain more sales. It's very similar to what you did with ATL except that the
target is .Net instead of COM. You have the choice of doing it in C++ or C#
or vb.net. Writing it in C++ will be very easy (even I managed to do it).
Writing it in C# or vb.net will also be very easy and both will be able to
interact with your wcsrv2.dll. The advantage would be that it's probably
easier to work in the higher level language and you could release the source
code if desired. The advantage with C++ is that any include files you have
will work, you keep all your code in 1 language etc.

Michael

Thanks for the design tips. Yes I prefer to do this in C++.

So what you are saying, I can create a C++ class library project and
repeat what Mark (the 3rd party developer) in creating the C# and/or
VB.NET marshalling modules, but this time under C++?

What NEW | PROJECT template to I choose? For C++, do I choose CLR
Class Library?

If interested, the C# and VB.NET modules Mark created can be
seen/downloaded from here:

http://www.winserver.com/public/wcsdk

This is the approach I want to explore. What I tried to do yesterday
was to create a DLL out of the wcserverapi.vb marshalling module.

1) I created new VB.NET Class Library Project

2) Added existing file wcserverapi.vb to the project

I think I changed it (don't fully remember)

Module wcServerAPI

to

Public Class wcServerAPI

Otherwise the types and functions were not exposed.

3) Compiled it to create a DLL

I created a console vb.net test project and referenced the DLL and it
aborted with the first reference to WildcatServerConnectSpecific() -
something related to the managed/unmanaged signature not correct.

So I think I have to wrap these unmanaged types/functions with a
managed class references? Correct?

Thanks

--
 
C

Cor Ligthert[MVP]

Tom,

Is it not a good idea to explicit add to the code "For VB6" for VB6 or any
other not Net tool as it not is for dotNet.vb as you write something for VB6
in this newsgroup?

This newsgroup has dotNet in its name, therefore before somebody searching
for solutions and find this in a dotNet newsgroup can make wrong conclusions

Cor
 
M

Mike

Michael said:
Ok, let me see mike. I start a copy of vb6 and type in this code

Private Sub Form_Load()
Dim x(10)
x(0) = 5
End Sub

hmmmm, seems to me that that array is zero based. If the documentation says
otherwise then it is wrong. Generally I don't have documentation from last
century on my PC so no I have no read it. However, some other areas of vb6
are most definately 1 based. Which areas are 1 based and which are zero
based appears to be random. You're not being very clear mike, perhaps you
can cut and paste this mysterious piece of documentation you refer to. Does
it say VB6 should be 1 based or zero based?

Michael, I can't confirm this under VB6 because my development machine
desktop was corrupted early this year and I now notice I need to
restore some lost VB6 dependencies. But in any case, I believe the
issue is related to String Indexes more than arrays.

For example:

dim x as string
x = "123456789"
dim i as integer
for i = 0 to Len(x)
print " x(";i;") = "; x(i)
next i

Expected result:

x(0) = 1
x(1) = 1
x(2) = 2
x(3) = 3
x(4) = 4
x(5) = 5
x(6) = 6
x(7) = 7
x(8) = 8
x(9) = 9

As a side note, I was never aware of Tom's input with

OPTION BASE 1

I probably would not have used it in our WCVB.BAS header interface
file because it has the explicit lower and upper range defined for the
arrays field we have.

Type Tuser
...
security(1...NUM_USER_SECURITY) * String * SIZE_SECURITY
...
End Type

But overall your point is proven - VB.NET is not VB6. The old rules
do no apply anymore. :)

--
 
M

Michael Williams

Tom, Is it not a good idea to explicit add to the code
"For VB6" for VB6 or any other not Net tool as it not
is for dotNet.vb as you write something for VB6 in
this newsgroup?

That's a sudden change of heart, Ligthert! You've spent ages deliberately
annoying the people in the Classic VB newsgroup by posting dotnet responses
there and telling us that it is fine to do so because VB6 and VB.Net both
have "VB" in their names! Why have you suddenly changed your mind . . . and
does this mean that you are now going to stop pestering us with your
evangelistic dotnet diatribes on the Classic VB group?

Mike
 
M

Michael Williams

Ok, let me see mike. I start a copy of vb6 and type in this code
Private Sub Form_Load()
Dim x(10)
x(0) = 5
End Sub
hmmmm, seems to me that that array is zero based.

Not necessarily. If you do not actually specify the desired base in the
declaration then it will default to the value you have assigned using the
Option Base statement, or to the default setting of zero if you have not
used Option Base. It's all clearly described in the documentation.
If the documentation says otherwise then it is wrong.

No, the documentation is fine. It is you who are wrong, Michael. You have
not read it, or if you have done then you have failed to understand it. As I
said, it's all clearly described there.
However, some other areas of vb6 are most definately 1 based.
Which areas are 1 based and which are zero based appears to
be random.

Nope. It's not random at all. It is all clearly described in the
documentation.
You're not being very clear mike . . .

I think it is you who is not being clear, possibly because of your lack of
understanding due to the fact you have not read the documentation. Don't
worry too much though. Programming is always a bit awkward at first. You'll
pick it up if you stick at it ;-)

Mike
 
M

Michael Williams

But overall your point is proven - VB.NET is not VB6.

That's exactly what we've been trying to tell the likes of Michael C and Cor
Ligthert and Bill McCarthy for ages! VB.Net is /NOT/ VB6. It is in fact
something completely different, which is why Micro$oft created a new and
different newsgroup for it. VB.Net questions and answers are OFF TOPIC in
the Classic Visual Basic group and I wish the likes of the people I have
mentioned would stop infesting the Classic VB group and would stop posting
their evangelistic dotnet drivel there. Thank you for pointing it out to
them.

Mike
 
M

Mike

Imitating Marv Albert - YES!

For the vb.net class library project, I

Took the wcserverapi.vb file, removed the MODULE statement and made it:

Public Class WcServerAPI

Then I did a global replacement for:

public function --> public shared function

compiled the DLL and I was now able to successful reference the
WCSERVERAPI.DLL and run with a test client application!

So in theory, this should link with a C# and other .NET languages?

Right?
 
M

Michael C

Tom Shelton said:
It depends on Option Base. Add:

Option Base 1

to the top of your module.

That would defeat the point of my code which was to find out the default.

Michael
 
M

Michael C

Michael Williams said:
Not necessarily. If you do not actually specify the desired base in the
declaration then it will default to the value you have assigned using the
Option Base statement, or to the default setting of zero if you have not
used Option Base. It's all clearly described in the documentation.


No, the documentation is fine. It is you who are wrong, Michael.

What is that I'm actually wrong on Mike? You keep saying I'm wrong but going
into vague rants.
Nope. It's not random at all. It is all clearly described in the
documentation.

So documenting something random makes it non-random? Let's see, arrays are
zero based, Mid function is 1 based, some componenents (released with vb6)
are zero based while others are 1. Surely to make all this non-random the
documentation would have to come up with a valid reason that some are 0 and
some are 1.
I think it is you who is not being clear, possibly because of your lack of
understanding due to the fact you have not read the documentation. Don't
worry too much though. Programming is always a bit awkward at first.
You'll pick it up if you stick at it ;-)

I asked you to post the relevant parts of the documentation you are
referring to. You are unable to do that? :)

Michael
 
M

Michael C

Michael Williams said:
That's exactly what we've been trying to tell the likes of Michael C and
Cor Ligthert and Bill McCarthy for ages! VB.Net is /NOT/ VB6.

Of course it's not VB6 Mike, it's VB7! :)

Michael
 
M

Michael C

Mike said:
Thanks for the design tips. Yes I prefer to do this in C++.

So what you are saying, I can create a C++ class library project and
repeat what Mark (the 3rd party developer) in creating the C# and/or
VB.NET marshalling modules, but this time under C++?
Yep.

What NEW | PROJECT template to I choose? For C++, do I choose CLR Class
Library?

I'm not going to be able to be a huge help here because I've only created
some fairly basic dlls like this but in VS2005, I selected Visual C++, CLR,
Class Library. If you click View-Solution Explorer then there are some right
click wizards that will add functions for you.
If interested, the C# and VB.NET modules Mark created can be
seen/downloaded from here:

http://www.winserver.com/public/wcsdk

This is the approach I want to explore. What I tried to do yesterday was
to create a DLL out of the wcserverapi.vb marshalling module.

1) I created new VB.NET Class Library Project

2) Added existing file wcserverapi.vb to the project

I think I changed it (don't fully remember)

Module wcServerAPI

to

Public Class wcServerAPI

Otherwise the types and functions were not exposed.

3) Compiled it to create a DLL

I created a console vb.net test project and referenced the DLL and it
aborted with the first reference to WildcatServerConnectSpecific() -
something related to the managed/unmanaged signature not correct.

I'm not sure about that one but that is possible. Maybe code like this

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]

causes problems when trying to export such a class/struct. I've only ever
used such classes/structs as internal.
So I think I have to wrap these unmanaged types/functions with a managed
class references? Correct?

Possibly but if you're doing this in C++ then you could just use existing
C++ include files (assuming you have them) and make public dot net friendly
wrappers.

Michael
 
M

Michael C

Mike said:
Imitating Marv Albert - YES!

For the vb.net class library project, I

Took the wcserverapi.vb file, removed the MODULE statement and made it:

Public Class WcServerAPI

Then I did a global replacement for:

public function --> public shared function

compiled the DLL and I was now able to successful reference the
WCSERVERAPI.DLL and run with a test client application!

So in theory, this should link with a C# and other .NET languages?

Right?

Absolutely. But are you creating this dll in C++.net or VB.net?

Michael
 
T

Tom Shelton

That would defeat the point of my code which was to find out the default.

Michael

Option Base 0 - that is the default. But, Mikes point is that you can't just
assume a 0 base or even a 1 base in VB6.

In fact, I made it a practice to pretty much always declare both bounds on all
of my array declarations, just to avoid confusion.
 
T

Tom Shelton

What is that I'm actually wrong on Mike? You keep saying I'm wrong but going
into vague rants.


So documenting something random makes it non-random? Let's see, arrays are
zero based, Mid function is 1 based, some componenents (released with vb6)
are zero based while others are 1. Surely to make all this non-random the
documentation would have to come up with a valid reason that some are 0 and
some are 1.

It's not random at all.... And it's very clear:

Arrays: If the lower bound is not explicitly delcared, then the lower bound
will be the value of Option Base. If Option Base is not explicitly set, then
it defaults to Option Base 0.

Strings: They start at one. Strings are not arrays in VB6 - and the functions
that work on strings start at one.

I personally never really used arrays that didn't start at 0, BUT - this is a
feature that should NOT have been removed from VB. This is one of the few
items where change was made for changes sake - especially since .NET arrays
clearly support non-zero bounds.
 
T

Tom Shelton

I'm not going to be able to be a huge help here because I've only created
some fairly basic dlls like this but in VS2005, I selected Visual C++, CLR,
Class Library. If you click View-Solution Explorer then there are some right
click wizards that will add functions for you.

If your interested in seeing examples of using .NET code from C++ or creating
..NET libraries that wrap C++ code, I do have a couple articles on my blog that
cover some of the basics of this: www.tom-shelton.net


One article covers creating a C++/CLI wrapper around a lib file so that you can call
the code directly from a .NET app.
 
M

Michael Williams

So documenting something random makes it non-random?

It is NOT random, and it is NOT documented as being random, and you are an
idiot for saying so.
Let's see, arrays are zero based . . .

Wrong! The base of arrays in VB6 is determined by your code. The base can be
zero or 1 or just about any other integer you desire, depending on your
personal choice. It is all very clearly documented and is very definitely
NOT random.
What is that I'm actually wrong on Mike?

You are wrong when you say that arrays are zero based and you are wrong when
you say that the base is random. How many other things would you like to be
wrong on?
Mid function is 1 based.

Yes, Mid and Left and Right and Instr and other string handling functions
count the first character of a string as being character number one. It is
definitely NOT random.
Surely to make all this non-random the documentation
would have to come up with a valid reason that some
are 0 and some are 1.

That statement of yours is ill thought out and illogical and completely
wrong. You obviously do not understand the meaning of the word random.
Perhaps you should look it up in your favourite dictionary. The fact that
the Mid function and other string functions count the first character of a
string as being character number 1 whereas the base of an array is as
determined by the programmer and otherwise defaults to zero does NOT make
either of those things random. Having the facility to specify the base of an
array yourself is sometimes quite useful and often enables you to visualise
things more easily for certain specific tasks, but if you would prefer to
always have all your arrays zero based then you can do so. It is up to the
programmer, and it is very clearly documented. It is NOT random.
I asked you to post the relevant parts of the documentation
you are referring to. You are unable to do that? :)

Of course I am able to do that! How much do you want? Here is an extract
taken verbatim from the help files. If you want any more then you'll have to
drag yourself out of your lazy stupor and find it yourself:

Option Base Statement. Used at module level to declare the default
lower bound for array subscripts.
Syntax: Option Base {0 | 1}
Remarks: Because the default base is 0, the Option Base statement
is never required. If used, the statement must appear in a module
before any procedures. Option Base can appear only once in a
module and must precede array declarations that include dimensions.
Note: The To clause in the Dim, Private, Public, ReDim, and Static
statements provides a more flexible way to control the range of an
array's subscripts. However, if you don't explicitly set the lower
bound with a To clause, you can use Option Base to change the
default lower bound to 1.

Mike
 
H

Herfried K. Wagner [MVP]

Michael C said:
You really shouldn't tell me these things Cor. :) Everytime I get on here
I find out another thing I don't like about vb.net :) That is an absolute
tragedy imo. After years of dealing with inconsistancy in VB6 I was quite
happy to accept zero based in dot net under the assumption it was that way
everywhere. In C# *everything* is zero based.

In C# everything is in a way that does not feel natural to the human ;-).
Personally I prefer the '0 To x' syntax for arrays, and I'd really
appreciate custom array boundaries (not for collections, but for arrays).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top