简体   繁体   中英

Assigning Value from Save File in Visual Basic 2010

My project is using Visual Basic 2010. I have a pure text save file with the following format:

num_TEC|1
num_STR|0
num_PER|0
num_MEC|1
num_KNO|1
num_DEX|1

I'm trying to load the data by using the first column as the object name and the second as the value. I've had some success with the following (skipping some basic IO lines):

prop = FileReader.ReadLine().Split("|")
tempvar = prop(0)    

If TypeName(Me.Controls(tempvar)) = "NumericUpDown" Then
  Me.Controls(tempvar).Value = prop(1)

I have a problems with this because I have some objects in panels or group boxes and Me doesn't reach into them.

Is there a way to just strictly reference an object with a variable? I'd love to assign the first column into prop(0) and just say

if TypeName(object(prop(0))) = "NumericUpDown" then
  object(prop(0)).Value = prop(1)

Can anybody help with this?

Thanks!

I suggest add your control id into some kind of list or dictionary that contains all control that you defined and use as sample above. The list can be list of class controls that you defined or simple dictionary

eg: add this as fields. String here means control ID, Object means

private readonly Dictionary<String,Control> dictControl = new Dictionary<String,Control>();

// add all controls that participate in your process. make sure it is unique name eg:

private void yourMethodAllControl()
{
    this.dictControl.Add(yourControl.Name, yourControl);
    ... add all your control and so on....
}

// set your values here

private void SetValues()
{
   .....// get all lines from files....
foreach(String line in lines)
{
   var query = from ctrl in this.dictControl
               where ctrl.Key == line.Split(0)
               select ctrl;
   var singleCtrl = query.First();
   singleCtrl.Value = line.Split(1);
}

}

Basically the idea is make your is easier to operate by using dictionary, rather than boxing and unboxing and check for the control children; It is simply put them in the dictionary by using its name and its instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM