Miten funktiota käytetään? Suorittaako tietokone aina ohjelmaa yhdessä kohtaa vai voiko se tutkia monia funktioita samanaikaisesti ("moniajon tapaan")? Alla ole ohjelma toimii jos poistaa kaiken Sub Main2() lähtien. Jos kyseisen ohjelman haluaa suorittaa kahdella sub Main()(tai miten ne nyt visual basicissa voi nimetä?) niin laitetaanko Sub Main2() funktio kutsu ohjelman alkuun ja jos niin miten?
Module Module1
Sub Main()
Console.Clear()
Console.Write("Write text: ")
Dim Text = Console.ReadLine()
If ContainsSuchword(Text) Then
Console.WriteLine("There is one or more such words")
Else
Console.WriteLine("There is no such words")
End If
Threading.Thread.Sleep(1000)
End Sub
Private Function ContainsSuchword(ByVal text As String) As Boolean
Dim Suchwords = New String() {"word1", "word2", "you were"}
For Each Suchword In Suchwords
If text.ToLower.Contains(Suchword) Then
Return True
End If
Next
Return False
End Function
End Module
Sub Main2()
Console.Clear()
Console.Write("Write text: ")
Dim Text = Console.ReadLine()
If ContainsSuchword(Text) Then
Console.WriteLine("There is one or more such B words")
Else
Console.WriteLine("There is no such words")
End If
Threading.Thread.Sleep(1000)
End Sub
Private Function ContainsSuchword(ByVal text As String) As Boolean
Dim Suchwords = New String() {"word1B", "word2B", "you wereB"}
For Each Suchword In Suchwords
If text.ToLower.Contains(Suchword) Then
Return True
End If
Next
Return False
End Function
End ModuleMod. lisäsi kooditagit.
codemike kirjoitti:
Suorittaako tietokone aina ohjelmaa yhdessä kohtaa vai voiko se tutkia monia funktioita samanaikaisesti ("moniajon tapaan")?
VB.net suorittaa ohjelmaa aina oletuksellisesti yhdestä kohtaa, mutta säikeillä voi korjata tämän ongelman.
codemike kirjoitti:
Alla ole ohjelma toimii jos poistaa kaiken Sub Main2() lähtien.
Johtuu siitä, että siinä on syntaxi virhe. KAIKKIEN funktioiden (ja aliohjelmien) on oltava jonkin luokan/modulen sisällä. Koodissasi kuitenkin rivillä 22 lopetetaan module, mutta ei aloiteta uutta.
codemike kirjoitti:
Jos kyseisen ohjelman haluaa suorittaa kahdella sub Main()(tai miten ne nyt visual basicissa voi nimetä?) niin laitetaanko Sub Main2() funktio kutsu ohjelman alkuun ja jos niin miten?
Ei
En tiedä tarkasti, mitä tuon koodin olisi tarkoitus tehdä, mutta tässä nyt korjattu versio säikeillä:
Module Module1
Sub Main()
'Luodaan säie objektit
Dim t1 As New System.Threading.Thread(AddressOf Main1)
Dim t2 As New System.Threading.Thread(AddressOf Main2)
'Aloitetaan säikeiden suoritus
t1.Start()
t2.Start()
End Sub
Sub Main1()
Console.Clear()
Console.Write("Write text: ")
Dim Text = Console.ReadLine()
If ContainsSuchword(Text) Then
Console.WriteLine("There is one or more such words")
Else
Console.WriteLine("There is no such words")
End If
Threading.Thread.Sleep(1000)
End Sub
Private Function ContainsSuchword(ByVal text As String) As Boolean
Dim Suchwords = New String() {"word1", "word2", "you were"}
For Each Suchword In Suchwords
If text.ToLower.Contains(Suchword) Then
Return True
End If
Next
Return False
End Function
Sub Main2()
Console.Clear()
Console.Write("Write text: ")
Dim Text = Console.ReadLine()
If ContainsSuchwordB(Text) Then
Console.WriteLine("There is one or more such B words")
Else
Console.WriteLine("There is no such words")
End If
Threading.Thread.Sleep(1000)
End Sub
Private Function ContainsSuchwordB(ByVal text As String) As Boolean
Dim Suchwords = New String() {"word1B", "word2B", "you wereB"}
For Each Suchword In Suchwords
If text.ToLower.Contains(Suchword) Then
Return True
End If
Next
Return False
End Function
End Moduleps.Toivottajasti joku Mod korjaa tuojon sinun viestiin kooditagit, olisi nimittäin helpompaa luettavaa.
Kiitos paljon avusta.
Aihe on jo aika vanha, joten et voi enää vastata siihen.