Nov 8, 2009

CType With Variable In VB.NET

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > BASIC / Visual Basic (.NET)

CType With Variable In VB.NET

tansqrx
I have hit a problem that requires a much greater VB.NET guru than I could ever imagine being. This is a code fragment from a configuration engine that I am building.

My program runs off a XML configuration file. This part of code comes from a DLL that will initialize, read, and write to the XML configuration file. I have found that my configuration values may change (add or remove values). As such I have tried to make a more flexible solution than statically reading and writing the values. The values are held in memory in nested user defined structures (I hope you can make that out).

CODE
    Structure configValues
        Public cfgUser As configGroup
        Public cfgStartup As configGroup
        Public cfgUpdate As configGroup
        Public cfgMainForm As configGroup
    End Structure


CODE
    Structure configUser
        Public cvProfileName As configValue
        Public cvGGDN As configValue
        Public cvBotsListSelect As configValue
        Public cvBotsList As configValue
    End Structure


CODE
    Structure configGroup
        Public iCount As Integer
        Public aKeys() As configValue

        Sub addKey(ByVal key As configValue)
            ReDim Preserve Me.aKeys(iCount)
            Me.aKeys(iCount) = key
            Me.iCount += 1
        End Sub

Sub addkey(ByVal strName As String, ByVal objValue As Object, ByVal tType As Object, ByVal objDefault As Object)
            Dim key As New configValue
            key.strName = strName
            key.objValue = objValue
            key.tType = tType
            key.objDefault = objDefault
            Me.addKey(key)
        End Sub
    End Structure


A simple sample of the XML File.

CODE
<Config>
       <User>
    <Name>JoeBob</ Name >
    < Exists>True</ Exists >
       </User>

</Config>


A function similar to what I will be using to read the XML.

CODE
strName = CType(configXMLDocument.GetElementsByTagName("Exists",Boolean)


Enough with the background, here is my problem. When reading the file I need to know what the type of objValue is, or in this case that Exists is a Boolean. This way I can put everything into a loop and just step through all the values without having to specify the type for each one. I guess I am wondering if there is a way to use a variable in the place of the type object (Boolean). Below is what I think should work but of course the complier completely blows up. If you need more details or a better explainiation let me know.

CODE
Dim t As System.Type = configValues.cfgUser.aKeys(1).objValue.GetType()
        
Dim b As Boolean = CType(configValues.cfgUser.aKeys(1).objValue, t)

 

 

 


Comment/Reply (w/o sign-up)

faulty.lee
Some correction for your code
CODE

strName = CType(configXMLDocument.GetElementsByTagName("Exists",Boolean)

should be
CODE

strName = CType(configXMLDocument.GetElementsByTagName("Exists") ,Boolean)

There's 2 way to go around this.
1. Use a XML stylesheet. (xls) to specified the data type of each column. Only useable if you have a fix table. You can edit the style sheet directly from VS.
2. Store your boolean as 0 or 1, that way CBool will do.
CODE

strName = CBool(configXMLDocument.GetElementsByTagName("Exists"))


Btw, i'm working a something silimiar also. Reading setting from xml. But my lib will automatically load the value into public variable if the name matches the one in the xml. I did it so that i can set any configuration which having to predefine what i need to get from the config file.

You can take a look at this page also, it has a good sample on using XML as config file (ini)
http://gpwiki.org/index.php/VBNET:Class_XMLINI

 

 

 


Comment/Reply (w/o sign-up)

tansqrx
A stable schema is a problem for me, that is part of the allure of reading dynamically. I tool a look at the Wiki site and I found a few good ideas. I think I will have to end up reading the type and running it through a case statement and convert it as needed.

BTW, sorry for the typo in the one line of code, I guess that’s what I get for editing in Word.

Comment/Reply (w/o sign-up)

faulty.lee
QUOTE(tansqrx @ Nov 16 2006, 05:23 AM) *

A stable schema is a problem for me, that is part of the allure of reading dynamically. I tool a look at the Wiki site and I found a few good ideas. I think I will have to end up reading the type and running it through a case statement and convert it as needed.


I'm glad that helps. Let me know if you still need any help.

QUOTE(tansqrx @ Nov 16 2006, 05:23 AM) *

BTW, sorry for the typo in the one line of code, I guess that’s what I get for editing in Word.


I'm just curious, why would you want to edit your code in Word?

Comment/Reply (w/o sign-up)

tansqrx
I always edit all my posts in Word before I send them out to the general public. I’m not exactly the best speller in the world and it is a must not to make me sound like a third grader. In this instance my existing code was from the old way that I was reading the XML file in. I was in a hurry so I just corrected the line of code in Word before posting.

I think I have found a solution to my problem. It’s not exactly what I had in mind but it works. A few more hours of debugging and I will post it.

Comment/Reply (w/o sign-up)

faulty.lee
Hi,

This is a bit off topic. I just been to your site http://www.ycoderscookbook.com/ link from another forum topic. Only realize the owner is the same person that i replied on this topic.

Very nice site indeed. I'm amaze by the amount of work you actually put into doing that.

The design of the site is also very unique.

Just some comment, if you have heard of gaim, maybe you can contribute to their project or combine their's with your's

Regards
Faulty

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Ctype Variable Vbnet


    Looking for ctype, variable, vb, net

See Also,

*SIMILAR VIDEOS*
Searching Video's for ctype, variable, vb, net
advertisement



CType With Variable In VB.NET

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com