Help with AppSettings file

furiousferret

Member
Joined
Dec 21, 2005
Messages
16
Programming Experience
1-3
Hello

I'm trying to write an application that is extremely customizable. One thing I want to accomplish is for the user to be able to move a function to whatever button he desires by editing the AppSettings Section. I can modify the text of the button this way, but attaching the action has been troublesome.

When I run the code below, the button does nothing.


VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] topTab6_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, _
[/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) _
[/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] topTab6.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] topTab6Act [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = ConfigurationSettings.AppSettings("topTab6Act")
[/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2]topTab6Act
[/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
MsgBox(ex.ToString)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

the entry AppSetting Field is below:
<add key="topTab6Act" value="Disconnect()" />

Any help would be appreciated...
 
In your code topTab6Act is a String object. You cannot just execute a String. If you want to execute a method whose name is contained in a String then you need to use Reflection, e.g.:
VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim methodName As String = "SayHello"
        Dim myType As Type = Me.GetType()
        Dim myMethod As Reflection.MethodInfo = myType.GetMethod(methodName)

        myMethod.Invoke(Me, Nothing)
    End Sub

    Public Sub SayHello()
        MessageBox.Show("Hello")
    End Sub
Note also that the method must be public because you can't call a private method from outside the instance.
 
Works perfect, one more thing, when I compile the code, I get a task error:

The variable 'topTab3Text' is either undeclared or was never assigned.

VB.NET:
[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] set1B15Text [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = ConfigurationSettings.AppSettings("topTab3Text")[/SIZE]
 
[SIZE=2][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].topTab3.Text = topTab3Text[/SIZE]
[SIZE=2]


The application works, I'm just getting 40+ task errors, what am I missing?
[/SIZE]
 
The VB 2005 compiler is much more strict than the VB.NET 2003 compiler was. That is telling you that you are using a variable on the right side of an assignment or as an argument to a method before you have ever used it on the left hand side of an assignment, i.e. you are using its value before ever giving it a value. This type of thing is already not allowed in C# and other C-based languages, but VB has always been less strict. This is its strength, because it's easy for beginners to use, but it's also its weakness, because it makes it easier for errors to creep into your code. Basically, you should assign a value to EVERY variable before you use it, e.g. this is wrong:
VB.NET:
Dim myString As String

myTextBox.Text = myString
while this is correct:
VB.NET:
Dim myString As String = String.Empty

myTextBox.Text = myString
or:
VB.NET:
Dim myString As String

myString = String.Empty
myTextBox.Text = myString
You should ALWAYS assign a value to a variable, either by passing a value to its constructor or using it on the left hand side of and equals sign, before using its value, either by passing it to a method or using it on the right hand side of an equals sign.
 
Whats throwing me off is I'm assigning the variable on right side and on the left side, in order, it should work (and works fine on other appsetting references) but it doesn't.


VB.NET:
[/LEFT]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] testText [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = ConfigurationSettings.AppSettings("topTab3Text")[/SIZE]
 
[SIZE=2][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].topTab3.Text = testText[/SIZE]
[/SIZE]

Could it be the the 'ConfigurationSettings.AppSetings("topTab3Text") isn't translating to an existing value?




 
The only thing you should EVER be editing in that region is the constructor, and then only to change the access modifier, the argument list or to add additional initialisation code AFTER the call to InitializeComponent. You should not EVER be touching anything else in that area.
 
Back
Top