I need to be able to change the value the variable "timeMins" at runtime in the JSON container class below. But, the only way that VB.Net allows me to do this is to declare "timeMins" as a Constant - However, constants cannot be changed at runtime as far as I know in VB.net.
Below is what I have so far...It compiles and runs, but does not do what I need it to do.
Const timeMins As String = "15"
Public Class JSON_Container_Real_Time
<JsonProperty(PropertyName:="Meta Data")>
Private Meta As MetaData
<JsonProperty(PropertyName:="Time Series (" + timeMins + "min)")>
Public Time_Series_Daily As Dictionary(Of String, StockInfo)
End Class
In its current state what you're trying to do is not possible. At namespace level you're only allowed to declare types and constants, so you would need to move the variable declaration inside your class in order to be able to make it a non-constant. However, this means that you cannot use it in the JsonProperty attribute, because attributes require constant values only.
You would have to look for another solution to serialize/deserialize you class.
Related
I'm having trouble inheriting a (public) variable, let's say
Public Var As ClassThatIsIndependent
The declaration above generates no trouble for itself, however, if i inherit the class that holds it
Implements BaseClass
I get the error "object module needs to implement variable for interface". I've tried these options (both inside ChildClass)
Public Var As ClassThatIsIndependent
and
Public BaseClass_Var As ClassThatIsIndependent
But none of them solves the problem. Any alternative? I'm open to possible Set/Get solutions, however, i'd prefer to maintain Var as a public variable.
Per the Visual Basic 6.0 Programmer's Guide, Polymorphism, Implementing Properties section:
Suppose we give the Animal class an Age property, by adding a Public variable to the Declarations section:
Option Explicit
Public Age As Double
The Procedure drop downs in the code modules for the Tyrannosaur and Flea classes now contain property procedures for implementing the Age property,
…
Using a public variable to implement a property is strictly a convenience for the programmer. Behind the scenes, Visual Basic implements the property as a pair of property procedures.
You must implement both procedures. The property procedures are easily implemented by storing the value in a private data member, as shown here:
Private mdblAge As Double
Private Property Get Animal_Age() As Double
Animal_Age = mdblAge
End Property
Private Property Let Animal_Age(ByVal RHS As Double)
mdblAge = RHS
End Property
The private data member is an implementation detail, so you have to add it yourself.
That is, the "public interface" is exactly the same whether you use a Public variable or define them with Property Get/Let. And to implement a property in an interface, you can't use the Public variable approach and need to use the Property Get/Let syntax and handle the data storage for it in your own private variables.
Ok, so this kind of follows after a previous question that I've asked involving structures and classes. So referencing this question (and I am using classes now for the base) I have one member of the class that is an array (and I know that I have to declare it without dimensions) that as part of the constructor I want it to define the dimensions of the array. When I was initially trying to do the ReDim the compiler was unhappy because I was declaring the member as ReadOnly. While what I'm doing with the array has it's own question of feasibility to it that's not what I'm asking about as it raised a different issue that I must answer first.
Is there a way to make members of a class/structure read only outside of the class/structure but modifiable with in the class/structure without having to use properties or internal functions/subs to gain the read access?
Basically like declaring the member private but you can at least read the member outside the class/structure. Just not anything else.
You can do something like this
Private _some As String
Public Property Some As String
Get
Return _some
End Get
Private Set(value As String)
_some = value
End Set
End Property
No. On its own, there is no way to make a class field public for reading, but private for writing. Accessibility modifiers on a field affect both read and write.
The cleanest way to do what you want is to define a private field in your class, and define a public property getter:
Private _dummy As String
Public Property Dummy() As String
Get
Return _dummy
End Get
End Property
Granted, it would be nice to be able to express this more succinctly, as is possible with C# using auto-implemented properties:
public string Dummy {get; private set;}
I want to create a variable that can be used across multiple forms.
It's going to be a temporary storage place for integers.
There are a couple of ways to do this in VB: a VB-specific way and a non-VB specific way (i.e. one that could also be implemented in C#.
The VB-specific way is to create a module and place the variable in the module:
Public Module GlobalVariables
Public MyGlobalString As String
End Module
The non-VB-specific way is to create a class with shared properties:
Public Class GlobalVariables
Public Shared Property MyGlobalString As String
End Class
The primary difference between the two approaches is how you access the global variables.
Assuming you are using the same namespace throughout, the VB-specific way allows you to access the variable without a class qualifier:
MyGlobalString = "Test"
For the non-VB-specific way, you must prefix the global variable with the class:
GlobalVariables.MyGlobalString = "Test"
Although it is more verbose, I strongly recommend the non-VB-specific way because if you ever want to transition your code or skillset to C#, the VB-specific way is not portable.
IN VB6 just declare on top code
public GlobalVariable as string
then you can use GlobalVariable in any form as you like.
like
GlobalVariable = "house"
then you can use /call in other form
text1 = GlobalVariable
will show value "house"
You can just add it as PUBLIC to ANY Module
Example:
Module Module1
'Global variables
Public glbtxtTemplateName As String 'GLOBAL VARIABLE FOR TEMPLATE
VB loads the Modals first as a class and all PUBLIC items therein are shared directly. Think about it this way.
Lets say we have a MODULE called "MY_PROCESSES"
When you declare a SUB or a FUNCTION in "MY_PROCESSES" if you want it to be used OUTSIDE of "MY_PROCESSES" you declare as PUBLIC like this
PUBLIC SUB LOAD_TEMPLATE()
....
To get to LOAD_TEMPLATE you just call it in your code from anywhere:
LOAD_TEMPLATE
So if I need to set or use the global variable that I made public in my module I just refer to it by name:
glbtxtTemplateName="TEMPLATE_NAME"
IF glbtxtTemplateName="" then LoadTemplate
I do like building the class as above because you can reference it faster without remembering the variable but if you only need 1 or 2 global variables you can name them like we used to with Hungarian Notation style name.
This method is really quite simple and elegant. Old is new and New is Old.
I have a (Serializable) class (Accounts), and inside this class some properties, including a Dictionary of another Serializable class (Symbols).
I Serialize the class (Accounts) and save them into a file to be used for next run.
Now, I have a new version of the application, which contain a new properties inside (Symbols) class. When I Deserialize the (Accounts), it loads the Dictionary correctly, but with un-desired values (the strings are nothing and the booleans are false).
Can I set a default values for those new properties inside the Symbols class when I deserialize? Note that I want this without a For loop, since the Dictionary of Accounts and the Symbols are large, which means I will need a huge double for loop to solve this using for loop.
Thanks.
Ahmad
You can just specify a default value for each property:
For backing fields:
Private _someProperty as Boolean = True
For auto-Implemented Properties:
Public Property SomeProperty As Boolean = True
I managed to work-around this case by making all my variables as Strings (to be handled as objects), and within my code, I check if the value is Nothing, then I will assign the default value when needed... I know this is not the best solution, but at least it worked out!
I have a little Problem:
I have a method that parses an incoming string for certain values, if a value is found, a new class is instantiated. The class name is identical to the parsed string. At the moment, my code looks like this:
Public Class Test1
End Class
Public Class Important
End Class
Public Class DoWork
Public Sub DoWork(incoming as String)
Select case incoming
case "Test1"
dim myobj as new Test1
Case "Important"
dim myobj as new Important
End Select
End Sub
End Class
I do not like the string literals like "Test1" - i could store them in a constant, but if the class names change, they have to be changed too. Is there a way to replace the literals with the Name of class?
I know that me.gettype produces the result for instantiated objects, but what about the simple name for a class, which is no object at this moment?
If your string is in correct format you can use Type.GetType(string) method to retrieve type. Then you can use Activator class to create instance if you have default constructor on that type.
Rafal's answer is good if you're stuck with the current situation, with the incoming string parameter. But it's still a bit fragile. What if the incoming parameter changes? What if you want to restructure your code, moving some classes to different namespaces or assemblies? What if those strings change - do you now have to rename your classes and recompile? You don't see the magic strings explicitly now, but they're still there.
So ask yourself - where are those strings coming from? Are they generated internally by your code? If so, you might want to generate, instead of strings, an Enum value that corresponds to the class to be instantiated. If they're external strings that you map to your classes, consider having explicit mapping (in a configuration file, for instance) that map String->Type. It's a bit more cumbersome, but a lot more flexible.