Jump to content
xisto Community
Sign in to follow this  
tansqrx

CType With Variable In VB.NET

Recommended Posts

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).

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

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

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 SubSub 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.

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

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

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.

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

Edited by vizskywalker (see edit history)

Share this post


Link to post
Share on other sites

Some correction for your code

strName = CType(configXMLDocument.GetElementsByTagName("Exists",Boolean)
should be
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.
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://forums.xisto.com/no_longer_exists/

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.