djbarbas Posted June 7, 2012 at 07:31 PM Report Share #461244 Posted June 7, 2012 at 07:31 PM (edited) Boas estou com o seguinte problema. Precisava de converter o conteudo de uma string em um comando. Segue um pequeno exemplo dim a as string = "listview1" a.items.item(0).tag="um valor" ' Essa linha não trabalha mas o objectivo era esse 'tentei o seguinte mas sem sucesso tambem Ctype(a, listview).items.item(0).tag = "um valor" 'igualmente sem sucesso Alguem tem alguma sugestão? Obrigado Edited June 7, 2012 at 08:43 PM by ribeiro55 Link to comment Share on other sites More sharing options...
petvetbr Posted June 7, 2012 at 07:48 PM Report Share #461249 Posted June 7, 2012 at 07:48 PM Qual o erro que aparece? Fernando Lage Bastos - MCP/MCTS/MCPD Link to comment Share on other sites More sharing options...
ribeiro55 Posted June 7, 2012 at 08:42 PM Report Share #461254 Posted June 7, 2012 at 08:42 PM (edited) djbarbas, VB não é linguagem de script. Não podes fazer referências desse tipo, ou pelo menos da forma como julgas. Quanto muito, encontras o objecto no seu container, através do seu nome, por exemplo: For Each C As Object In Me.Controls If TypeOf C Is ListView And C.name = "ListView1" Then DirectCast(C, ListView).Items.Add("yey") End If Next Edited June 7, 2012 at 08:45 PM by ribeiro55 1 Report Sérgio Ribeiro "Great coders aren't born. They're compiled and released""Expert coders do not need a keyboard. They just throw magnets at the RAM chips" Link to comment Share on other sites More sharing options...
djbarbas Posted June 7, 2012 at 11:15 PM Author Report Share #461270 Posted June 7, 2012 at 11:15 PM (edited) Muito Obrigado Sérgio, após adaptar o código funcionou 5*. Contudo tenho uma duvida no que respeita à diferença de um Directcast e um Ctype. No código abaixo recebo 2 valores referentes à tabpage que vou fechar: e.tabpage.tag --> Devolve-me o nome da listview e.tabpage.name --> Devolve-me o indice do botão da listview Isso tudo porque estou a controlar que cada botão de uma determinada listview só me adiciona uma unica tabpage através de uma verificação se o valor da tag é 0 ou 1. Tive que usar um Ctype para converter o valor 0 (string) em 0 (inteiro) não tendo sucesso com o directcast. Podiam-me explicar a diferença? Private Sub CustomTabControl1_TabClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles CustomTabControl1.TabClosing For Each C As Object In Me.Panel1.Controls If TypeOf C Is ListView And C.name = e.TabPage.Tag Then DirectCast(C, ListView).Items.Item(CType(e.TabPage.Name, Integer)).Tag = "0" End If Next End Sub Muito Obrigado! Edited June 7, 2012 at 11:42 PM by djbarbas Link to comment Share on other sites More sharing options...
ribeiro55 Posted June 8, 2012 at 12:40 PM Report Share #461388 Posted June 8, 2012 at 12:40 PM Em traços largos, deves usar DirectCast quando tens a certeza do tipo, e o CType quando achas que o tipo pode ser resolvido "automáticamente". Por exemplo: DirectCast("1,5", Double) - Rebenta porque estás a fornecer uma string. CType("1,5", Double) - Não rebenta e avalia correctamente 1.5 D, convertido da string Agora, claro que fazer um DirectCast é muito menos custoso do que uma conversão de tipo. 1 Report Sérgio Ribeiro "Great coders aren't born. They're compiled and released""Expert coders do not need a keyboard. They just throw magnets at the RAM chips" Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now