Connecting Tech Pros Worldwide Forums | Help | Site Map

I can't use an Array thats in un other Function in an other class??

BadOmen
Guest
 
Posts: n/a
#1: Jul 17 '05
I have made an array in a function in a Class now I need to get to that
Array from an other function in an other Class, How do I do that?

The Array has no fixed size as it is dependent on how much a user has put in
a txt file.

The Class with the Array is an object, objInternetClass and I want to reach
the Array by doing something like
This is the MSAgentClass:

General:
Dim objInternet As New InternetClass

Private Sub InitAgentCommands()

'I will later use this For loop in another function to get both the Name and
the URL, that's way it looks like this...

For X = 0 To UBound(objInternet.MyArray) Step 2
Debug.Print "objInternet.URLAddress ==> " & objInternet.MyArray(X)
Debug.Print "objInternet.NameAddress ==> " & objInternet.MyArray(X + 1)
Debug.Print "------------------------------------"

Merlin.Commands.Add objInternet.MyArray(X + 1) , objInternet.MyArray(X + 1)
, objInternet.MyArray(X + 1)

Next

End Sub

Yours, Jonas



BadOmen
Guest
 
Posts: n/a
#2: Jul 17 '05

re: I can't use an Array thats in un other Function in an other class??


I can't get the Array to be Public...

From the Help text:
" To create a public array, use the Public statement in the Declarations
section of a module to declare the array. "

In my InternetClass where I am making the Array from a text file I am
declaring the myArray in the general Declarations sector like this:

Public MyArray() As String
but I get this Compile Error message:

" Constants, fixed-Length Strings, Arrays, User-defined types and declare
statements not allowed as Public members of object modules "

How can I get it Public than??


"BadOmen" <badomen02@hotmail.com> skrev i meddelandet
news:tL9Pb.79170$dP1.197640@newsc.telia.net...[color=blue]
> I have made an array in a function in a Class now I need to get to that
> Array from an other function in an other Class, How do I do that?
>
> The Array has no fixed size as it is dependent on how much a user has put[/color]
in[color=blue]
> a txt file.
>
> The Class with the Array is an object, objInternetClass and I want to[/color]
reach[color=blue]
> the Array by doing something like
> This is the MSAgentClass:
>
> General:
> Dim objInternet As New InternetClass
>
> Private Sub InitAgentCommands()
>
> 'I will later use this For loop in another function to get both the Name[/color]
and[color=blue]
> the URL, that's way it looks like this...
>
> For X = 0 To UBound(objInternet.MyArray) Step 2
> Debug.Print "objInternet.URLAddress ==> " & objInternet.MyArray(X)
> Debug.Print "objInternet.NameAddress ==> " & objInternet.MyArray(X + 1)
> Debug.Print "------------------------------------"
>
> Merlin.Commands.Add objInternet.MyArray(X + 1) , objInternet.MyArray(X +[/color]
1)[color=blue]
> , objInternet.MyArray(X + 1)
>
> Next
>
> End Sub
>
> Yours, Jonas
>
>[/color]


Larry Serflaten
Guest
 
Posts: n/a
#3: Jul 17 '05

re: I can't use an Array thats in un other Function in an other class??


"BadOmen" <badomen02@hotmail.com> wrote[color=blue]
> I have made an array in a function in a Class now I need to get to that
> Array from an other function in an other Class, How do I do that?
>
> The Array has no fixed size as it is dependent on how much a user has put in
> a txt file.[/color]

You should redesign your class so that you do not need to expose the array.
You might even make the array a collection which you would be able to
expose, if you really need to expose it.

What if you had these in your internet class:

Public Sub Addresses(ByVal Index As Long, Name as String, URL as String)
If Index >= LBound(MyArray) And Index <= UBound(MyArray) \ 2 Then
URL = MyArray(Index * 2)
Name = MyArray(Index * 2 + 1)
End if
End Sub

Public Function TotalCommands() As Long
TotalCommands = UBound(MyArray) \ 2
End Function


You could then loop through them elsewhere:

Dim x&, nam$, url$

For X = 0 to objIE.TotalCommands
ObjIE.Addresses x, nam, url
Merlin.Commands.Add nam, nam, nam
Next

(All untested code but you might get the idea)
LFS





-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
BadOmen
Guest
 
Posts: n/a
#4: Jul 17 '05

re: I can't use an Array thats in un other Function in an other class??


I don't know how to use that...I'm a real newbe...

This is what I have done and whant to do(it does not realy work...
*** marks questions and remarks

This is what I need to do:


This is in the InternetClass:

'**** I am making MyArray here

Public Function FromFile()

Dim X As Long
Dim FileNum As Long
Dim TotalFile As String
Dim MyArray() As String
FileNum = FreeFile
' Reads the entire file into memory all at once
Open "c:\testfile.txt" For Binary As #FileNum
TotalFile = Space(LOF(FileNum))
Get #FileNum, , TotalFile
Close #FileNum
' Replace all newline character sequences with commas
' This will create a long file with every item separated
' by a comma in the same order as presently in file
TotalFile = Replace$(TotalFile, vbNewLine, ",")

' Split the enormous single string at the commas and puts it in an Array
MyArray = Split(TotalFile, ",")


End Function



***** In the MS Agent Class I have this function:

Dim objInternet As New InternetClass

Private Sub InitAgentCommands()
' Purpose: Initialize the Commands menu
On Error Resume Next

Merlin.Commands.RemoveAll
Merlin.Commands.Caption = "My Menu Name"
Merlin.Commands.Add "ACO", "Advanced Character Options", "Advanced
Character Options"
Merlin.Commands.Add "SPEECHCP", "Speech Control Panel", "[Open] Speech
Control Panel"
Merlin.Commands.Add "SEP1", "______________________________", ""
Merlin.Commands.Add "Jonas Musik", "Jonas Musik", "Jonas Musik"
Merlin.Commands.Add "Read copy", "read copy", "read copy"
Merlin.Commands.Add "SEP2", "______________________________", ""


'****This must take the array made in the InternetClass under the Function
FromFile()
'****and use it here, How do I do that??? (I can't get the data from MyArray
I cant even use MyArray here...)

For X = 0 To UBound(objInternet.MyArray) Step 2

'URLAddress ís MyArray(X) I will use this in an other place.
'NameAddress Is MyArray(X + 1)

Merlin.Commands.Add objInternet.MyArray(X + 1), objInternet.MyArray(X + 1),
objInternet.MyArray(X + 1)


Next


Merlin.Commands.Add "Back", "Back", "Back"
Merlin.Commands.Add "Forward", "Forward", "Forward"
Merlin.Commands.Add "Close", "Close", "Close"

End Sub


"Larry Serflaten" <Abuse@SpamBusters.com> skrev i meddelandet
news:400d834a_2@corp.newsgroups.com...[color=blue]
> "BadOmen" <badomen02@hotmail.com> wrote[color=green]
> > I have made an array in a function in a Class now I need to get to that
> > Array from an other function in an other Class, How do I do that?
> >
> > The Array has no fixed size as it is dependent on how much a user has[/color][/color]
put in[color=blue][color=green]
> > a txt file.[/color]
>
> You should redesign your class so that you do not need to expose the[/color]
array.[color=blue]
> You might even make the array a collection which you would be able to
> expose, if you really need to expose it.
>
> What if you had these in your internet class:
>
> Public Sub Addresses(ByVal Index As Long, Name as String, URL as String)
> If Index >= LBound(MyArray) And Index <= UBound(MyArray) \ 2 Then
> URL = MyArray(Index * 2)
> Name = MyArray(Index * 2 + 1)
> End if
> End Sub
>
> Public Function TotalCommands() As Long
> TotalCommands = UBound(MyArray) \ 2
> End Function
>
>
> You could then loop through them elsewhere:
>
> Dim x&, nam$, url$
>
> For X = 0 to objIE.TotalCommands
> ObjIE.Addresses x, nam, url
> Merlin.Commands.Add nam, nam, nam
> Next
>
> (All untested code but you might get the idea)
> LFS
>
>
>
>
>
> -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
> http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
> -----== Over 100,000 Newsgroups - 19 Different Servers! =-----[/color]


Larry Serflaten
Guest
 
Posts: n/a
#5: Jul 17 '05

re: I can't use an Array thats in un other Function in an other class??


"BadOmen" <badomen02@hotmail.com> wrote[color=blue]
> I don't know how to use that...I'm a real newbe...[/color]

Then you need to get experienced. By that I mean you need
to practise, practise, practise. If I explained all the theories of
aerodynamics, and told you how to build an airplane, would
you be able to fly? No, you would probably still need some
one to build the airplane, and someone else to explain how
to fly it. Why is that?

You are looking for answers to some very basic programming
questions. The trouble is that you do not have enough experience
to understand the answers. You basically need someone to do
it for you. You even posted code pasted in from someone
elses answer....

Copy and Paste is not a good method to program by. You
should understand every aspect of the code you add, in case
you need to make alterations. I don't want to discourage you
from learning to program, but you really have to learn to crawl
before you can learn to run! To do that, you should be taking
on very simple tasks, finding code examples, and seeing how
they work, and so on.

I know we all started out knowing nothing, but it is also your
own responsibility to read the Programmer's Guide, and Help
files to learn about using VB.

I already explained what you needed to do to expose your array
to the users of your class. You also need to make that array
available to all the routines in your class: Instead of declaring
MyArray inside the FromFile procedure, it needs to be declared
at the top of the module, ahead of any of the procedures. Then
add the two routines I posted last time and try it like I shown....

LFS











-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
BadOmen
Guest
 
Posts: n/a
#6: Jul 17 '05

re: I can't use an Array thats in un other Function in an other class??


Ok....
Well I made some changes to your code to make it work with mine... So now it
works fine...

It is just in one place it does not work. It is in this Select:

Select Case UserInput.Name

Case "SEP2"
'Emty line


Dim x&, nam$, url$

For x = 0 To objReadFile.TotalCommands
objReadFile.Addresses x, nam, url
Case nam
Address = url
objInternet.Navigate Address
Next

Case "Back"
objInternet.Back
End Select

If I do the for loop here, i will get the error message telling me that Case
is without Select Case
Is it not possible to make it work in a Select Case???
Or is it just me having to go back to the "Hello World"??


"Larry Serflaten" <Abuse@SpamBusters.com> skrev i meddelandet
news:400da3c3_2@corp.newsgroups.com...[color=blue]
> "BadOmen" <badomen02@hotmail.com> wrote[color=green]
> > I don't know how to use that...I'm a real newbe...[/color]
>
> Then you need to get experienced. By that I mean you need
> to practise, practise, practise. If I explained all the theories of
> aerodynamics, and told you how to build an airplane, would
> you be able to fly? No, you would probably still need some
> one to build the airplane, and someone else to explain how
> to fly it. Why is that?
>
> You are looking for answers to some very basic programming
> questions. The trouble is that you do not have enough experience
> to understand the answers. You basically need someone to do
> it for you. You even posted code pasted in from someone
> elses answer....
>
> Copy and Paste is not a good method to program by. You
> should understand every aspect of the code you add, in case
> you need to make alterations. I don't want to discourage you
> from learning to program, but you really have to learn to crawl
> before you can learn to run! To do that, you should be taking
> on very simple tasks, finding code examples, and seeing how
> they work, and so on.
>
> I know we all started out knowing nothing, but it is also your
> own responsibility to read the Programmer's Guide, and Help
> files to learn about using VB.
>
> I already explained what you needed to do to expose your array
> to the users of your class. You also need to make that array
> available to all the routines in your class: Instead of declaring
> MyArray inside the FromFile procedure, it needs to be declared
> at the top of the module, ahead of any of the procedures. Then
> add the two routines I posted last time and try it like I shown....
>
> LFS
>
>
>
>
>
>
>
>
>
>
>
> -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
> http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
> -----== Over 100,000 Newsgroups - 19 Different Servers! =-----[/color]


Larry Serflaten
Guest
 
Posts: n/a
#7: Jul 17 '05

re: I can't use an Array thats in un other Function in an other class??


"BadOmen" <badomen02@hotmail.com> wrote[color=blue]
> Ok....
> Well I made some changes to your code to make it work with mine... So now it
> works fine...
>
> It is just in one place it does not work. It is in this Select:[/color]

You can't break up the Select/Case structure like that. It is
a bit more optimized but it is little more than the If/Then/Else
type.

If A Then
DoX
ElseIf B Then
DoY
Else
DoZ
End If

Select case is similar:

Select Case Input
Case A
DoX
Case B
DoY
Else
DoZ
End Select

From what you've posted, it looks like you'll need to test all
those you know about, and if they don't match then try the
object's commands

Select Case Input
Case "SEP2"
' Nothing?
Case "Back"
' Go back
Case "Quit"
' Quit
Case (etc... Whatever else you have)
Case Else
For x = 0 To objReadFile.TotalCommands
objReadFile.Addresses x, nam, url
If Input = nam then
' Do nam command
Exit Sub
End If
Next
End Select

LFS






-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
BadOmen
Guest
 
Posts: n/a
#8: Jul 17 '05

re: I can't use an Array thats in un other Function in an other class??



"Larry Serflaten" <Abuse@SpamBusters.com> skrev i meddelandet
news:400dfc35$1_5@corp.newsgroups.com...[color=blue]
> "BadOmen" <badomen02@hotmail.com> wrote[color=green]
> > Ok....
> > Well I made some changes to your code to make it work with mine... So[/color][/color]
now it[color=blue][color=green]
> > works fine...
> >
> > It is just in one place it does not work. It is in this Select:[/color]
>
> You can't break up the Select/Case structure like that. It is
> a bit more optimized but it is little more than the If/Then/Else
> type.
>
> If A Then
> DoX
> ElseIf B Then
> DoY
> Else
> DoZ
> End If
>[/color]

I was thinking of doing a bunch If instead so I will use that. I was just
curious of if it was possible with she Select Case :) It was in the original
code...

The problem with The if, end if, in a loop is that I must have if in the
loop I can't make an if statement outside the loop and just a bunch of
elseif in the loop because It then say that I don't have a If. That mean
that I cant have an Else at the end. If I have that it is just fore the last
if and elseif states that I have put under the loop. Example might be
needed...:


<Snip>

ElseIf UserInput.Name = "SEP2" Then
'Emty line
End If

Dim x&, nam$, url$

For x = 0 To objReadFile.ReadFile
objReadFile.Addresses x, nam, url

If UserInput.Name = nam Then
Adress = url
objInternet.Navigate Adress
End If
Next


If UserInput.Name = "Back" Then
objInternet.Back
ElseIf UserInput.Name = "Forward" Then
objInternet.Forward

ElseIf UserInput.Name = "Close" Then
objInternet.Quit

Else

'This Else wold just be for the last three if,elseif NOT for all
above, but I just have to be without the confused Merlin that don't
understand the voice command :)

End If


Thanx for all your help :-)

[color=blue]
> Select case is similar:
>
> Select Case Input
> Case A
> DoX
> Case B
> DoY
> Else
> DoZ
> End Select
>
> From what you've posted, it looks like you'll need to test all
> those you know about, and if they don't match then try the
> object's commands
>
> Select Case Input
> Case "SEP2"
> ' Nothing?[/color]

The Nothing line is emty becouse it is just an Line ------- In the menue. It
will not hapen a thing if pressed. ( It is used with MS Agent voice
Commands)
[color=blue]
> Case "Back"
> ' Go back
> Case "Quit"
> ' Quit
> Case (etc... Whatever else you have)
> Case Else
> For x = 0 To objReadFile.TotalCommands
> objReadFile.Addresses x, nam, url
> If Input = nam then
> ' Do nam command
> Exit Sub
> End If
> Next
> End Select
>[/color]

I am using the Else here all ready making the Merlin in MS Agent look
confused. When he don't understand the voice command he is given.[color=blue]
> LFS
>
>
>
>
>
>
> -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
> http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
> -----== Over 100,000 Newsgroups - 19 Different Servers! =-----[/color]


BadOmen
Guest
 
Posts: n/a
#9: Jul 17 '05

re: I can't use an Array thats in un other Function in an other class??


I have changed your code and maybe you can tell if it's bad or if it's ok?
It works fine, but there maybe problems that I haven't in countered...
This is what I have chained your code in to(you can se your original code
further down):



this is in my internet class:

Public Sub Addresses( Index As Long, Name as String, URL as String)
URL = MyArray(Index)
Name = MyArray(Index + 1)
End Sub

Public Function TotalCommands() As Long
TotalCommands = UBound(MyArray) -1
End Function


You could then loop through them elsewhere:

Dim x&, nam$, url$

For X = 0 to objIE.TotalCommands Step 2
ObjIE.Addresses x, nam, url
Merlin.Commands.Add nam, nam, nam
Next


Yours, Jonas


"Larry Serflaten" <Abuse@SpamBusters.com> skrev i meddelandet
news:400d834a_2@corp.newsgroups.com...[color=blue]
> "BadOmen" <badomen02@hotmail.com> wrote[color=green]
> > I have made an array in a function in a Class now I need to get to that
> > Array from an other function in an other Class, How do I do that?
> >
> > The Array has no fixed size as it is dependent on how much a user has[/color][/color]
put in[color=blue][color=green]
> > a txt file.[/color]
>
> You should redesign your class so that you do not need to expose the[/color]
array.[color=blue]
> You might even make the array a collection which you would be able to
> expose, if you really need to expose it.
>
> What if you had these in your internet class:
>
> Public Sub Addresses(ByVal Index As Long, Name as String, URL as String)
> If Index >= LBound(MyArray) And Index <= UBound(MyArray) \ 2 Then
> URL = MyArray(Index * 2)
> Name = MyArray(Index * 2 + 1)
> End if
> End Sub
>
> Public Function TotalCommands() As Long
> TotalCommands = UBound(MyArray) \ 2
> End Function
>
>
> You could then loop through them elsewhere:
>
> Dim x&, nam$, url$
>
> For X = 0 to objIE.TotalCommands
> ObjIE.Addresses x, nam, url
> Merlin.Commands.Add nam, nam, nam
> Next
>
> (All untested code but you might get the idea)
> LFS
>
>
>
>
>
> -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
> http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
> -----== Over 100,000 Newsgroups - 19 Different Servers! =-----[/color]


Larry Serflaten
Guest
 
Posts: n/a
#10: Jul 17 '05

re: I can't use an Array thats in un other Function in an other class??


> this is in my internet class:[color=blue]
>
> Public Sub Addresses( Index As Long, Name as String, URL as String)
> URL = MyArray(Index)
> Name = MyArray(Index + 1)
> End Sub[/color]

That is just another way to do the same thing, but, you left out the Index check.

If you do not _verify_ that the input is valid, then you really should supply
error handling code in case you get bad input. For example what if someone
passes in an Index valuie of -1 or 100000, or other such numbers? Without
verification or error handling, that function could crash your program.

It pays to be defensive in all your code, even if you are the only one that is
going to use it. I prefer to verify the input, or, stop the trouble at the source,
so to speak. You might develop a different strategy....

LFS





-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
BadOmen
Guest
 
Posts: n/a
#11: Jul 17 '05

re: I can't use an Array thats in un other Function in an other class??



"Larry Serflaten" <Abuse@SpamBusters.com> skrev i meddelandet
news:401059d6_6@corp.newsgroups.com...[color=blue][color=green]
> > this is in my internet class:
> >
> > Public Sub Addresses( Index As Long, Name as String, URL as String)
> > URL = MyArray(Index)
> > Name = MyArray(Index + 1)
> > End Sub[/color]
>
> That is just another way to do the same thing, but, you left out the Index[/color]
check.[color=blue]
>
> If you do not _verify_ that the input is valid, then you really should[/color]
supply[color=blue]
> error handling code in case you get bad input. For example what if[/color]
someone[color=blue]
> passes in an Index valuie of -1 or 100000, or other such numbers? Without
> verification or error handling, that function could crash your program.
>
> It pays to be defensive in all your code, even if you are the only one[/color]
that is[color=blue]
> going to use it. I prefer to verify the input, or, stop the trouble at[/color]
the source,[color=blue]
> so to speak. You might develop a different strategy....
>
> LFS
>
>[/color]

I have now added the index verification, I think you have a point there...
:)

Thank you
[color=blue]
>
>
>
> -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
> http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
> -----== Over 100,000 Newsgroups - 19 Different Servers! =-----[/color]


Closed Thread


Similar Visual Basic 4 / 5 / 6 bytes