Jump to content



Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!

Toggle shoutbox Shoutbox Open the Shoutbox in a popup

@  agyat : (24 May 2013 - 05:15 PM) O Dear, Where Are You? Without Your Words This Sb Is ..
@  agyat : (23 May 2013 - 01:23 AM) Wow! Mr. Sb Back Home.
@  OpaQue : (23 May 2013 - 12:44 AM) Ting
@  OpaQue : (24 April 2013 - 02:44 PM) I guess, Time to run Mycent script.
@  OpaQue : (24 April 2013 - 02:43 PM) wow.. not much spam. except habatt posting lot of links.. :P
@  yordan : (23 April 2013 - 01:04 PM) You're welcome, agyat. Nice to have been helpful. Second lesson: try full words, "you" instead of "EW".
@  agyat : (23 April 2013 - 05:03 AM) @YORDAN: tHANK EW FOR YOUR FIRST LESSON.   :D
@  yordan : (22 April 2013 - 09:43 PM) @agyat : "why don't you help me", or "please help me", or "please teach us"
@  yordan : (22 April 2013 - 09:42 PM) welcome back, velma
@  velma : (22 April 2013 - 07:51 AM) **yawns** Good to be back, wonder what is going on here :)
@  agyat : (22 April 2013 - 03:50 AM) Oh! so, why don't help me learn english..
@  yordan : (21 April 2013 - 08:38 PM) The goal mentioned by shiu : "learning english, learning computer"
@  agyat : (21 April 2013 - 06:31 PM) WHAT GOAL?
@  yordan : (20 April 2013 - 10:39 AM) yes, that's our goal. simultaneouly learning English and teaching/learning computer using.
@  shiyu : (20 April 2013 - 07:30 AM) learning english,learning computer
@  yordan : (19 April 2013 - 01:11 PM) Oh, I see, it's just a trick in order to force people looking at your texte. Somehow smart, maybe.
@  agyat : (19 April 2013 - 02:54 AM) And of course I know it is not SEO friendly.
@  agyat : (19 April 2013 - 02:52 AM) There may be two possible answers for that ....


1) Shout was posted using mobile keypad.

2) To force people read content carefully and/or with more concentration.
@  agyat : (19 April 2013 - 02:49 AM) There may be two possible answers for that ....
@  yordan : (18 April 2013 - 09:35 PM) however, why this mixing of capital letters in the middle of your text?

Photo
- - - - -

CType With Variable In VB.NET


5 replies to this topic

#1 tansqrx

tansqrx

    Super Member

  • [HOSTED]
  • 759 posts

Posted 15 November 2006 - 07:20 AM

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

<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, 21 November 2006 - 03:48 AM.


#2 faulty.lee

faulty.lee

    Super Member

  • [HOSTED]
  • 500 posts
  • Interests:Electronics, Software Programming, Embedded Programming, Movies, Windows Shopping
  • myCENTs:79.88

Posted 15 November 2006 - 08:30 AM

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://gpwiki.org/in...ET:Class_XMLINI

#3 tansqrx

tansqrx

    Super Member

  • [HOSTED]
  • 759 posts

Posted 15 November 2006 - 09:23 PM

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.

#4 faulty.lee

faulty.lee

    Super Member

  • [HOSTED]
  • 500 posts
  • Interests:Electronics, Software Programming, Embedded Programming, Movies, Windows Shopping
  • myCENTs:79.88

Posted 15 November 2006 - 09:31 PM

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?

#5 tansqrx

tansqrx

    Super Member

  • [HOSTED]
  • 759 posts

Posted 20 November 2006 - 07:50 PM

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.

#6 faulty.lee

faulty.lee

    Super Member

  • [HOSTED]
  • 500 posts
  • Interests:Electronics, Software Programming, Embedded Programming, Movies, Windows Shopping
  • myCENTs:79.88

Posted 21 November 2006 - 04:02 AM

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



Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users