Condivisione Dictionary

venerdì 06 gennaio 2012 - 23.36
Tag Elenco Tags  VB.NET  |  Windows 7

Sergio83 Profilo | Newbie

Salute a Voi ,

Vi chiedo come condividere un dizionario tra due sub... Ho provato a rendere la sub dove creo il dizionario come Public ma non sembre
funzionare
In questo codice ci sono tre moduli di classe sub
SplashScreen()
loadObj()
Start()
che vorrei tenere volutamente divisi
L'intento è di verificare se presente nel dizionario Classes la stringa Inp
Se possibile vorrei evitare di mettere variabili global ( lo dico solo perchè vorrei imparare ad non usarle..)

Module Module1 Sub Main() SplashScreen() loadObj() Start() End Sub Sub Start() CreateChar() End Sub Sub CreateChar() Dim id As New Character() Dim Chara As New Dictionary(Of String, Character) Dim Inp As String = "" Console.WriteLine("Human") Console.WriteLine("Elf") Do Until Inp = "Exit" Inp = Console.ReadLine() If Classes.ContainsKey("Human") Then Console.WriteLine("OK") --------------Nella riga sopra non riconosce Classes Loop Chara.Add(Inp, New Character()) End Sub Sub SplashScreen() Dim WelcomeMessage As Object WelcomeMessage = "Welcome 0.00.00" Console.WriteLine(WelcomeMessage) End Sub Public Sub loadObj() Dim Classes As New Dictionary(Of Byte, String) Classes.Add("0", "Human") Classes.Add("1", "Elf") End Sub End Module

Jeremy Profilo | Guru

Ciao Sergio

>Vi chiedo come condividere un dizionario tra due sub...
Per condivide l'oggetto Dictionary(come qualsiasi altra variabile) all'interno del modulo è sufficiente dichiararla a livello di modulo(appunto) con visibilità Private
Module Module1 Private Classes As New Dictionary(Of Byte, String) Sub Main() SplashScreen() loadObj() Start() End Sub Sub Start() CreateChar() End Sub Sub CreateChar() Dim id As New Character() Dim Chara As New Dictionary(Of String, Character) Dim Inp As String = "" Console.WriteLine("Human") Console.WriteLine("Elf") Do Until Inp = "Exit" Inp = Console.ReadLine() If Classes.ContainsKey("Human") Then Console.WriteLine("OK") Loop Chara.Add(Inp, New Character()) End Sub Sub SplashScreen() Dim WelcomeMessage As Object WelcomeMessage = "Welcome 0.00.00" Console.WriteLine(WelcomeMessage) End Sub Public Sub loadObj() Classes.Add("0", "Human") Classes.Add("1", "Elf") End Sub End Module

>Se possibile vorrei evitare di mettere variabili global ( lo
>dico solo perchè vorrei imparare ad non usarle..)
Se per "mettere variabili globali", intendi quello che ho fatto io ..... non è assolutamente sbagliato farlo.
Ovviamente è consigliabile dichiararle a livello di classe, o di modulo che sia, il meno possibile ..... solo per una questione di "ordine" nel codice.

Per quanto riguarda il resto del codice che hai scritto ci sarebbero altre cosette da rivedere .... ma questo è un'altro discorso.
Ad ogni modo .... rimango a disposizone per ulteriori info.

Facci sapere...
Ciao

Sergio83 Profilo | Newbie

Ciao Jeremy,

Mi hai messo la pulce nell'orecchio...dove ho sbagliato ?
Module Module1 Private Classes As New Dictionary(Of String, Byte) Private WelcomeMessage As String Sub Main() SplashScreen() loadObj() Start() End Sub Sub Start() CreateChar() End Sub Sub CreateChar() Dim Chara As New Dictionary(Of String, Character) Dim Cl As String = "" Dim Stat As String = "" Console.WriteLine("Human") Console.WriteLine("Elf") Do Until Classes.ContainsKey(Cl) = True Cl = Console.ReadLine() Loop Chara.Add(Cl, New Character()) Console.WriteLine("OK") Do Until Stat = "Exit" Console.WriteLine("STR 4") Stat = Console.ReadLine() Chara(Cl).setStr(4 + Val(Stat)) Console.WriteLine("DEX 2") Stat = Console.ReadLine() Chara(Cl).setDex(4 + Val(Stat)) Console.WriteLine("INT 2") Stat = Console.ReadLine() Chara(Cl).setInt(4 + Val(Stat)) Console.WriteLine("SPT 4") Stat = Console.ReadLine() Chara(Cl).setSpt(4 + Val(Stat)) Loop End Sub Sub SplashScreen() Console.WriteLine(WelcomeMessage) End Sub Public Sub loadObj() WelcomeMessage = "Welcome 0.00.00" Classes.Add("Human", "0") Classes.Add("Elf", "1") End Sub End Module
Ho rivisto qualche cosa

Jeremy Profilo | Guru

Ciao Sergio
Effettivamente ... qualcosa l'hai sistemata ma, a voler essere pignoli ....
Module Module1 Private Classes As Dictionary(Of String, Byte) Private WelcomeMessage As String Sub Main() SplashScreen() loadObj() Start() End Sub Sub Start() CreateChar() End Sub Sub CreateChar() Dim Chara As New Dictionary(Of String, Character) Dim Cl As String = String.Empty Dim Stat As String = String.Empty For Each item As KeyValuePair(Of String, Byte) In Classes Console.WriteLine(item.Key) Next Do Until Classes.ContainsKey(Cl) = True Cl = Console.ReadLine() Loop Chara.Add(Cl, New Character()) Console.WriteLine("OK") Do Until Stat.ToUpper = "EXIT" Console.WriteLine("STR 4") Stat = Console.ReadLine() Chara(Cl).setStr(4 + Val(Stat)) Console.WriteLine("DEX 2") Stat = Console.ReadLine() Chara(Cl).setDex(4 + Val(Stat)) Console.WriteLine("INT 2") Stat = Console.ReadLine() Chara(Cl).setInt(4 + Val(Stat)) Console.WriteLine("SPT 4") Stat = Console.ReadLine() Chara(Cl).setSpt(4 + Val(Stat)) Loop Classes = Nothing Chara = Nothing End Sub Sub SplashScreen() Console.WriteLine(WelcomeMessage) End Sub Public Sub loadObj() Classes = New Dictionary(Of String, Byte) WelcomeMessage = "Welcome 0.00.00" Classes.Add("Human", 0) Classes.Add("Elf", 1) End Sub End Module

Ciao.

Sergio83 Profilo | Newbie

Ottimo chi ti fa notare i tuoi errori , grazie mille !

christinetpineda Profilo | Newbie

it's too much




________
http://www.superdvdoutlet.ca/
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5