-
[RESOLVED] Access a list of attributes
Hi,
I need to access a list of attributes as follows.
I'm drawing a curve named "CurveName". With Curvename I need to access the curve color, the data symbol etc.
I tried this:
Code:
Dim cllCurveAtt as new cllCurveAtt
sub SetAtt
cllCurveAtt.Add(CurveName, "Color.Red")
......
end sub
If I then want to refer to the curve color through cllCurveAtt(CurveName) I get an error because "Color.Red" is not a system color, it should be Color.Red without the quotes. But if I remove the quotes in the assignment THAT causes an error because since CurveName is a string, that color thing should also be convertable to a string. Catch 22.
How can I do this in another way?
Thanks,
Jan Didden
-
Re: Access a list of attributes
Hey,
What is cllCurveAtt, is that a Class that you have created?
Gary
-
Re: Access a list of attributes
No, sorry, my bad.
I did:
Dim cllCurveAtt as new Collection()
at the top of the module, immediately after the class declaration.
Jan Didden
-
Re: Access a list of attributes
Hey,
Can you provide details of what exactly you are trying to achieve?
It sounds like what you actually want is a Generic Dictionary that holds both a Key and a Value.
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx
Gary
-
Re: Access a list of attributes
Ok. I need to draw a bunch of curves. Each curve is identified by a string CurveName which I read from a file.
Each different CurveName has a number of associated attributes like the CurveColor, the CurveSymbol etc.
The curve drawing routine (which works ok) needs these attributes passed to it, and until now I just passed the parameters verbose everytime I call the sub. But I want to be able to have a central place where I have all the curve attributes in a kind of table and access them from there, so that I can call the curve drawing sub like:
call CurveDraw(CurveName, CurveName.Color, CurveName.Symbol)
Or just call it with CurveDraw(CurveName) and figure the attributes out locally.
Hope I'm halfway clear.
Jan Didden
-
Re: Access a list of attributes
After some more reading, I'll try this:
Code:
Public Class Curve
Public Structure stcCurve
Dim CurveName As String
Dim CurveColor As Color
Dim CurveSymbol As SymbolType
Dim CurvePoints As PointPairList
End Structure
End Class
Does that make sense?
Jan Didden
-
Re: Access a list of attributes
Hey,
Sorry, was having lunch :)
Yes, I would say in your case, a structure, or even it's own class with properties would make sense.
Gary
-
Re: Access a list of attributes
Hi Gary, hope it was a good lunch ;)
So now I have this structure:
Public Structure stcCurve
Dim CurveName As String
Dim CurveColor As Color
Dim CurveSymbol As SymbolType
Dim CurvePoints As PointPairList
End Structure
When setting up the curve I say:
Dim ThisCurveInstance as new stcCurve , right.
Now how do I access that particular instance of stcCurve with the CurveName string I have?
And since each curve has it's own set of attributes in its own set of stcCurve, how do I set up these sets?
I'm sure it sound pretty silly but I just don't see through it.
Jan Didden
-
Re: Access a list of attributes
Hey,
Yes, it was very tasty thanks :)
Thinking about it, I would be inclined to do something like this:
Code:
Public Class stcCurve
Private _curveName As String
Public Property CurveName() As String
Get
Return _curveName
End Get
Set(ByVal value As String)
_curveName = value
End Set
End Property
Private _curveColor As Color
Public Property CurveColor() As Color
Get
Return _curveColor
End Get
Set(ByVal value As Color)
_curveColor = value
End Set
End Property
Private _curveSymbol As SymbolType
Public Property CurveSymbol() As SymbolType
Get
Return _curveSymbol
End Get
Set(ByVal value As SymbolType)
_curveSymbol = value
End Set
End Property
Private _curvePoints As PointPairList
Public Property CurvePoints() As PointPairList
Get
Return _curvePoints
End Get
Set(ByVal value As PointPairList)
_curvePoints = value
End Set
End Property
End Class
And then, you would use it like this:
Code:
Dim curves As New List(Of stcCurve)
Dim curve As New stcCurve
curve.CurveColor = Color.AliceBlue
curve.CurveName = "MyCurve"
curve.SymbolType = blah
curve.PointPairList = blah
curves.Add(curve)
Here a Generic List of Type stcCurve is being used, which you can then add new instances of stcCurve to, and then you can enumerate through them when you need to using:
Code:
For Each myCurve As stcCurve In curves
MessageBox.Show(myCurve.CurveName)
Next
Hope that makes sense!!
Gary
-
Re: Access a list of attributes
Hmmm. I *think* I get it.
If I then would want to pass the CurveColor to a sub based on the CurveName string, I would do like:
call DrawSub (Curvename, myCurve.CurveName.CurveColor) right?
Or just pass DrawSub(CurveName) and in the sub do:
TheColor = myCurve.CurveName.CurveColor OK?
I still am confused by the term 'instance'. If i create an instance of an object, is that then a completely independent one from any other same name object, or is it just to set a pointer to the earlier declaration? IOW, do instances of an object refer to one and the same object in memory?
Jan Didden
-
Re: Access a list of attributes
Hey,
Think of a class as a template, which all instances derive from. So in creating an instance of a class, you are creating a completely separate entity, that has all it's own properties, so we could do something like this:
Code:
Dim curves As New List(Of stcCurve)
Dim curve1 As New stcCurve
curve1.CurveColor = Color.AliceBlue
curve1.CurveName = "MyCurve2"
curve1.SymbolType = blah1
curve1.PointPairList = blah1
Dim curve2 As New stcCurve
curve2.CurveColor = Color.Red
curve2.CurveName = "MyCurve2"
curve2.SymbolType = blah2
curve2.PointPairList = blah2
curves.Add(curve1)
curves.Add(curve2)
Here, curve1 and curve2 are two separate instances of the stcCurve class.
In terms of passing information about the instance of the stcCurve to a method, it depends on what you are doing in that method. If you need all of the information, you would simply do this:
Code:
DrawSub(curve1)
DrawSub(curve2)
You can't do what you are suggesting, as this would not be passing an instance of the class, but rather trying to pass the Type of the Object, which does not make sense.
Similarly, if you only needed to pass the CurveName of the stcCurve instance, you would do this:
Code:
DrawSub(curve1.CurveName)
DrawSub(curve2.CurveName)
Hope that makes sense!!
Gary
-
Re: Access a list of attributes
Thanks! That makes it much more clearer.
One remaining problem: when I try to access (in your example above) for example curve2.curvename from another module, it says "cannot access curve2 in this context because it is private", which is strange because I Dim'd curve2 directly under the class declaration as public, and I imported the namespaces in each module...
Jan Didden
-
Re: Access a list of attributes
Hey,
Can you post the code that you are using?
Gary
-
Re: Access a list of attributes
It's a lot of code, but this is the gist of it.
In Form1 I have:
Code:
Imports WindowsApplication1.FileStuff
Public Class bTest
Dim Curves As New List(Of stcCurve)
Dim Bias As New stcCurve
Dim Temp As New stcCurve
Dim Power As New stcCurve
Dim BiasPoints = New PointPairList()
Dim PowerPoints = New PointPairList()
.......
Public Sub SetUpCurves()
Bias.CurveColor = Color.AliceBlue
Bias.CurveName = "Bias Current"
Bias.CurveSymbol = SymbolType.Circle
Bias.CurvePoints = BiasPoints
Curves.Add(Bias)
Power.CurveColor = Color.AliceBlue
Power.CurveName = "Power Dissipation"
Power.CurveSymbol = SymbolType.Circle
Power.CurvePoints = PowerPoints
Curves.Add(Power)
Temp.CurveColor = Color.AliceBlue
Temp.CurveName = "Power Dissipation"
Temp.CurveSymbol = SymbolType.Circle
Temp.CurvePoints = PowerPoints
Curves.Add(Temp)
End Sub
.....
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'set up the graph
'set curve attributes
Call SetUpCurves()
For Each myCurve As stcCurve In Curves
MessageBox.Show(myCurve.CurveName)
Next
End Sub
End Class
... and that's fine.
Then in another module I have:
Code:
Imports WindowsApplication1.bTest
Public Class FileCSV
......
Public Sub EnableCheckBox(ByVal CurveName As String)
Select Case CurveName
Case bTest.Bias.CurveName 'this gives the error message
bTest.cbxRefIb.Enabled = True
bTest.cbxRefIb.Checked = False
Case bTest.Power.CurveName 'this gives the error message
bTest.cbxRefPc.Enabled = True
bTest.cbxRefPc.Checked = False
Case bTest.Temp.CurveName 'this gives the error message
bTest.cbxRefTc.Enabled = True
bTest.cbxRefTc.Checked = False
End Select
End Sub
End Class
The error message is:
Error 2 'WindowsApplication1.bTest.Bias' is not accessible in this context because it is 'Private'.
-
Re: Access a list of attributes
Dim = private. Change 'Dim' to 'Public'
-
Re: Access a list of attributes
Well yes I had tried that:
Code:
Public Class bTest
Dim Curves As New List(Of stcCurve)
Public Bias As New stcCurve
Dim Temp As New stcCurve
... but that gives me:
Error 1 'Bias' cannot expose type 'CurveStuff.mdCurveStuff.stcCurve' outside the project through class 'bTest'.
I tried 'Shared' but that gives the same error as Dim.
Jan Didden
-
Re: Access a list of attributes
Ok, and what are the modifiers of CurveStuff.mdCurveStuff.stcCurve?
-
Re: Access a list of attributes
Hey,
I think it would help as well if you told us exactly what you are trying to achieve. It may be the case that you don't need to use the approach you are currently using, but rather another solution may be available.
Gary
-
Re: Access a list of attributes
Quote:
Originally Posted by
ForumAccount
Ok, and what are the modifiers of CurveStuff.mdCurveStuff.stcCurve?
You mean this:
Code:
Namespace CurveStuff
Module mdCurveStuff
Public Class stcCurve
Jan Didden
-
Re: Access a list of attributes
Hi Gary,
The stuff you posted in #14 does exactly what I need: different curves that have different attributes, which I can pass to a sub that draws a curve on a Zedgraph pane.
I have all that stuff working but then I extended the nice single curve to the possibility to have a family of curves with different colors, shapes etc. As noted, your suggestion is very good.
The only remaining problem is with the access and visibility, and I guess I'll solve that as well with the pointers from you guys...
Jan Didden
-
Re: Access a list of attributes
Hey,
This might be a bit of an ask depending on the sensitivity of your project, but if you could upload a sample of the application as is, we might be able to find the problem sooner. Just a suggestion.
Gary
-
Re: Access a list of attributes
Gary,
I appreciate the offer to look at it, realizing you are giving freely of your time and expertise. I am a bit uncomfortable though with uploading the whole shebang, as I'm sure you can imagine.
Let me give it one more shot, after all this *should* be benign, no? ;-)
Then again, I might come back like the proverbial dog...
Jan Didden
-
Re: Access a list of attributes
Hey,
Nope, I totally understand.
Another suggestion would be to create another application, that uses the majority of functionality and that has the same issue, i.e. a streamlined version, that we could take a look at and make suggestions.
Gary
-
Re: Access a list of attributes
Quote:
Originally Posted by
janneman
You mean this:
Code:
Namespace CurveStuff
Module mdCurveStuff
Public Class stcCurve
Jan Didden
See, VS is telling you exactly what is wrong. You can't expose class stcCurve through a non-publicly declared Module.
Change your line:
Code:
Module mdCurveStuff
To:
Code:
Public Module mdCurveStuff
================================
I duplicated your error making assumptions on your code structure:
Code:
Namespace nmTest
Public Module mdTest
Public Class bTest
Public Bias As New CurveStuff.mdCurveStuff.stcCurve 'This will be underlined with that same error.
End Class
End Module
End Namespace
Namespace CurveStuff
Module mdCurveStuff 'I need to have the public modifier
Public Class stcCurve
Private _CurveName As String
Public Property CurveName() As String
Get
Return _CurveName
End Get
Set(ByVal value As String)
_CurveName = value
End Set
End Property
End Class
End Module
End Namespace
Namespace FileStuff
Public Class FileCSV
Dim bTest As New nmTest.mdTest.bTest
Public Sub ECB()
MsgBox(bTest.Bias.CurveName)
End Sub
End Class
End Namespace
-
Re: Access a list of attributes
Yes!!
Declaring Bias public in the main module and then make the using module also public did it. I tried that, but only seperately.
You guys are awesome.
I programmed in assembly way down in 1974, a mil weapons system computer with (hold on) 8k 24 bit core mem. Did I tell you we kept a running score of free memory locations? Coming up with a changed multiply routine that saved another two locations was an achievement.
We had a major project in 1978 to quadruple mem to 32k. Man, we could program the world!
It was all so much simpler then. (Not really).
Jan Didden
-
Re: Access a list of attributes
Hey,
Glad to hear that you got it all working.
I missed all those fun times, didn't get into computers until about '99, still lots to learn!!
Remember to follow the links in my signature to close off your thread if all your questions have been answered.
Gary
-
Re: Access a list of attributes
Errr... Hmm... I'm not there yet.
So now that I have this collection of curves:
Code:
Public Class bTest
Dim Curves As New List(Of stcCurve)
Public Bias As New stcCurve
Public Temp As New stcCurve
Public Power As New stcCurve
Public Sub SetUpCurves()
Bias.CurveColor = Color.AliceBlue
Bias.CurveName = "Bias Current"
Bias.CurveSymbol = SymbolType.Circle
Bias.CurvePoints = BiasPoints
Curves.Add(Bias)
Power.CurveColor = Color.Red
Power.CurveName = "Power Dissipation"
Power.CurveSymbol = SymbolType.Square
Power.CurvePoints = PowerPoints
Curves.Add(Power)
Temp.CurveColor = Color.Green
Temp.CurveName = "Heatsink Temp"
Temp.CurveSymbol = SymbolType.Diamond
Temp.CurvePoints = TempPoints
Curves.Add(Temp)
End Sub
I have a string strDataType that holds the name of the curve whose attributes I want to access like "Bias" or "Power". So how do I access, say, the color of the Bias curve? I thought 'Curves.Bias.CurveColor' would be the way, and that I could somehow replace the 'Bias' in the pointer with the variable strDataType and use 'Curves.(strDataType).CurveColor' but that doesn't work.
What is the right way to do this (assuming there is a way...).
Jan Didden
-
Re: Access a list of attributes
Hey,
At the minute, you have a Generic Collection of stcCurve, which means you have the ability to index into that collection using something like this:
Code:
curves(1).CurveName
If however you want to identify the curve by a string literal, then you might want to think about using a Dictionary, something like this:
Code:
Dim newCurves As New Dictionary(Of String, stcCurve)()
newCurves.Add("test1", curve1)
newCurves("test1").CurveName
You can use the Key, in this case test1, to grab the reference to the Curve in question.
Gary
-
Re: Access a list of attributes
I'll try that. I've the feeling I'm building a mini-database, but I'm learning, and that's great.
Jan
-
Re: Access a list of attributes
That does it. I have now for each curve the following:
Code:
Power.CurveColor = Color.Red
Power.CurveName = "Power Dissipation"
Power.CurveSymbol = SymbolType.Square
Power.CurvePoints = PowerPoints
Curves.Add(Power)
dicCurvesList.Add("Power", Curves(1))
And with the string strDataType that holds the name of the curve, I can indeed access for instance the color with
Code:
ThisColor = dicCurvesList(strDataType).CurveColor
Thanks! Exactly what I wanted. Everything in one place and easily extentable if necessary.
On to the next hurdle ;-)
Jan Didden
-
Re: Access a list of attributes
Good stuff!!! Glad to hear you got it to work the way that you wanted it to!!