Initializing DataSets in VB.NET - database

Is it necessary to use "Nothing" keyword to initialize a DataSet in VB.NET?

Well if you set the variable to "Nothing" then you're not initialising an actual DataSet object at all - you're just setting the value of the variable to a null reference.
For an instance or static variable, Nothing will be the default value. For a local variable, in C# there's effectively no default as the compiler doesn't let you use a variable without it being definitely assigned - whether the VB compiler lets you get away with this or not depends on compiler settings. However, if it does let you use an unassigned variable, the value will be Nothing by default.

Dim ds as New DataSet
(or, if it's already defined, ds = New DataSet) ... is all you need to initialise an actual DataSet object. It's the use of the 'New' keyword that creates the object. Later on, after it has some DataTables in it, you can clear them out with
ds.Tables.Clear
As Jon says,
Dim ds as Dataset = nothing
... does not create an instance of a DataSet, initialised or otherwise. All it does is define 'ds' as a variable that must refer to an instance of the DataSet class, and explicitly sets it to point to nothing.
Dim ds as DataSet
.. would achieve the same thing. Again, later on, after you've used your DataSet,
ds = Nothing
... does not reinitialise the DataSet, all it does is set your variable 'ds' to no longer refer to anything.

Related

Array variable is used before assigned a value

When I rebuild the solution I get the warning:
"Variable 'aMyArray' is used before it has been assigned a value."
A function in VB.NET uses an array which is dynamically populated.
Example:
Function MyArray()
Try
Dim aMyArray()
For i = 0 to 100
ReDim Preserve aMyArray(i)
Next
Catch ex As Exception
End Try
End Function
How should I declare a dynamically populated array to eliminate this warning?
I like to use the CreateInstance to avoid possible errors:
Dim aMyArray() as string = Array.CreateInstance(GetType(String),0)
A. Your function returns nothing, so you should also have a warning about that
B. You seriously need to turn on Option Strict and let the compiler point out other errors like aMyArray has no Type.
C. Never use and empty Try/Catch; if there is an exception, you want to know when something goes wrong (and where) so you can fix it.
D. Avoid arrays like the plague. ReDim Preserve aMyArray(i) creates a new array and then copies all the data to the new structure. This can be costly in terms of performance if it is a large array of something like strings. Net has several very nice collections such as List(of T) which do not need to be explicitly resized:
Private myList As New List(of String) ' or Integer, Decimal or even MyClassObject
...
myList.Add("Hello")
myList.Add("Lists...")
myList.Add("Goodbye")
myList.Add("Arrays")
D(1). The result of using a List would mean that entire procedure can be done away with. Just add new things to the list as needed.
E. The code posted cant actually result in the warning because you dont ever add a value to it. Adding: aMyArray(2) = 2 after the loop will result in the warning. This is because you have never fully declared the array (size and type) as in:
Dim aMyArray(100) As String
ReDim simply resizes the array which isnt the same thing.
F. Since there is nothing in your new array, there is no reason to use ReDim Preserve because there is nothing to preserve. I'll assume that is supposed to be some calculation. On the other hand, if you are trying to resize an existing array, it should be a Sub.
We are all left to guess if it really is meant to modify an existing array (based on ReDim Preserve) or return a new array of stuff (considering the array is declared in the procedure).
Again, none of this is needed using a List, Dictionary or other Collection Type.

VBA excel passing an array to sub fired by button

As in title I would like to pass an array to the sub which is executed by a button (That's why I don't know ho to pass it to the sub). Array is calculated in the Sheet before you can exectue sub with the button.
I've tried using:
Option Explicit On
Public My_array(1 to 10)
But I have received an error that I can not do that command with array, const etc.
What is the correct way to do it?
I think there is some terminology we can try to clear up: you're not actually passing the variable to the button's procedure. A publicly scoped variable is going to be available within that procedure and does not need to be "passed".
Note: I'm not sure what you mean by "Array is calculated in the Sheet". Unless you are using some event procedure to calculate an array in memory (probably you are not, because you don't have the array scoped or declared properly to do this...), this statement can't possibly be true.
Your declaration should be like:
Public myArray(1 To 10) As Variant 'or As String, As Double, etc.
You can omit the As ... part and it will be type Variant. if you need more strongly-typed array, then you will need to specify.
Also, On is not a legal keyword after the Option Explict statement. So, remove that. But keep Option Explicit in all of your modules.
The error message you receive hints at the solution: You can't put public declarations in object modules. An object module is a Worksheet, Workbook, UserForm, or Class module in the VBE.
You can only put these declarations in a normal code module.
Solution: Move the declaration of myArray to a normal code module.

Make variable name using loop and string in PowerBuilder

I am interested if it is possible to make variable name in PowerBuilder using a loop and a string. For example:
long ll_go
string lst_new
for ll_go = 1 to 8
lst_new = "text" + ll_go
lst_new.tag = 5500
next
So, it should give me variables text1, text2..,.,text8 and I would be able to assign values for them. Let me know if anybody succeeded, thanks in advance
Your description is lacking some term precision.
If you actually want to dynamically create new variables as "variable in a powerscript subroutine or function" this is simply not possible.
If instead you want to create dynamically some new controls statictext or textedit objects in a window or visual userobject this is possible:
use a local variable of the type of the new object you need to create, e.g. static text
make it a live object (instantiate) with create
set the object properties to whatever you need
"attach" the new object to its parent (either a window or a visual userobject - though any graphicobject is possible with using the win32api SetParent function) with the OpenUserObject() method. Note that you cannot simply add it directly to the parent's Control[] array.
you can also keep the object in your own array for later convenience access to the created objects instead of looping on the Control[] array
once the object is attached it its parent, you can reuse the local variable to create another one
Here is an example:
//put this in a button clicked() event on a window
//i_myedits is declared in instances variables as
//SingleLineEdit i_myedits[]
SingleLineEdit sle
int i
for i = 1 to 8
sle = create singlelineedit
sle.text = string(i)
sle.tag = "text_" + string(i)
sle.height = pixelstounits(20, ypixelstounits!)
sle.width = pixelstounits(100, xpixelstounits!)
parent.openuserobject(sle, pixelstounits(10, xpixelstounits!), pixelstounits(22 * i, ypixelstounits!))
i_myedits[i] = sle //keep our own reference
next
An exemple of values access:
//put that in another button clicked() event
SingleLineEdit sle
int i
string s_msg
for i = 1 to upperbound(i_myedits[])
sle = i_myedits[i]
if i > 1 then s_msg += "~r~n"
s_msg += "edit #" + string(i) + " (" + sle.tag + ") says '" + sle.text + "'"
next
messagebox("Edits values", s_msg)
As you can see, one practicality problem is that you cannot refer to these controls by constructing the control's name like "text"+2, instead you must access the my edits[] array or loop through the controls and test their .tag property if you set it to something specific.
I do not think that it is possible. Workaround could be an array maybe.
Br. Gábor
I'd see two ways to do this, but they aren't as easy as it seems that you were hoping:
1. Control Array
First method would be to go through the control arrays (on windows, tabs and user objects). I'd create a function that took the control name as a string, then another that overloaded the same function and took control name and an array of windowobject. The string-only method would just call the string/array method, passing the string through and adding the window.Control as the second parameter. The string/array method would go through the array, and for each element, get the ClassDefinition. Pull the name off of it, and parse it apart the way you want it to match the string parameter (e.g. for w_test`tab_first`tabpage_first`cb_here, do you want cb_here to match, or tab_first`tabpage_first`cb_here?). Deal with matches as appropriate. When you find a control of type tab or user object, call the string/array function again with the Control array from that object; deal with success/fail returns as appropriate.
2. DataWindow
What you're describing works extremely well with DataWindows, and their Describe() and Modify() functions. Since you pass these functions only a string, you can build not only the control names, but the values they're set to as you would build any string. In fact, you can build multiple Modify() strings together (delimited by a space) and make a single call to Modify(); this is not only faster, but reduces window flicker and visible activity.
Don't fall into the trap of thinking that, since your data isn't from a database, you can't use a DataWindow. Create an external DataWindow, and simply use it with one row inserted during the Constructor event.
As you might guess, I'd strongly favour the DataWindow approach. Not only is it going to perform better, but it's going to provide a lot more flexibility when you want to move on and tag more control types than just static text. (You'll have to do some type casting even with one control type, but if you want to get into multiples, you'll need to start a CHOOSE CASE to handle all your types.)
Good luck,
Terry
You can't create a variable name in a script because the variables have to be declared before you can use them. With PBNI it's possible to generate a name the way you describe and then get a reference to a variable of that name that already exists but I don't think that's what you want. If you want to keep track of additional properties for your controls, just inherit a new user object from whatever it is (sle, mle, etc.) and add the properties you want. Then you can place your user object on a window and use the properties. Another approach is to use the control's Tag property. It holds a string that you can put whatever you want in. PFC uses this technique. Terry's DataWindow solution is a good approach for storing arbitrary data.
Yes, and there are more than one way to skin a cat.
Sounds like you have several properties so I'd use an array of custom non visual user objects, or an array of structures. Otherwise you could probably use something from the .NET framework like a dictionary object or something like that, or a datawidnow using an external datasource, where you can refer to column names as col + ll_index.ToString().
SIMPLE Example:
Make custom NVO with following instance variables, plus getter/setter functions for each, name it n_single_field
// add the properties and recommend getter and setter functions
public string myTag
public string myText
public int myTabOrder
...
// To USE the NVO define an unbounded array
n_single_field fields[]
// to process the populated fields
integer li_x, li_max_fields
// loop through field 1 through max using array index for field number
li_max_fields = upperbound(fields)
for li_x = 1 to li_max_fields
fields[li_x].myTag = 'abc'
fields[li_x].myText = 'text for field number ' + li_x.ToString()
fields[li_x].myTabOrder = li_x * 10
next
Maybe I'm oversimplifying if so let me know, if there is a will there is always a way. ;)

Passing field-symbols into FORM

I need to assign data field (component of another field symbol) to field-symbol in a several places of code. For the sake of reusability I decided to encapsulate this code in procedure, but I cannot understand how to pass field-symbols into this procedure.
LOOP bseg ASSIGNING <bseg>
...
PERFORM assigning USING <bseg>
CHANGING <wrbtr>.
...
ENDLOOP.
FORM assigning USING <bseg> TYPE bseg
CHANGING <wrbtr> TYPE bseg-wrbtr
IF ...
some logic here
ASSIGN <bseg>-wrbtr TO <wrbtr>.
ELSE
ASSIGN <bseg>-skfbt TO <wrbtr>.
ENDIF.
ENDFORM.
This code does not work.
What should I do to change the field symbol reference too?
This is not possible, at least not the way you try to do it. Field symbols cannot be passed as the pointers they really are. If you need something like that, you'll have to use real references.
Not knowing anything about the rest of your code - it looks a bit weird. Why would you want to change data in BSEG fields directly? I can only assume that you're "abusing" fields to transport some custom value throughout the code, and that's usually a bad idea. And if you need to do this, I'd rather do it this way:
LOOP bseg ASSIGNING <bseg>.
IF foo.
l_my_wrbtr = <bseg>-wrbtr.
ELSE.
l_my_wrbtr = <bseg>-skfbt.
ENDIF.
" ... pro'lly thousands of lines I don't even want to see...
IF foo.
<bseg>-wrbtr = l_my_wrbtr.
ELSE.
<bseg>-skfbt = l_my_wrbtr.
ENDIF.
ENDLOOP.

EquivalentC# coding for Dim dataTable As DataTable = CType(sender, GridView).DataSource

i am trying to convert Vb.net To C#.net. could any one please help me to find the Equivalent C# coding for Dim dataTable As DataTable = CType(sender, GridView).DataSource.
and also,any tip for soring datas in data gridview.
thanks
You mean like this?:
DataTable dataTable = ((GridView)sender).DataSource;
To cast types in C#, you put the type in parentheses before the value:
(GridView)sender
Then, to access properties on it, you'll want to wrap the whole thing in parentheses:
((GridView)sender).DataSource
(This is because otherwise you'd be trying to call .DataSource on the un-cast sender which would fail.)
Then to declare a value (the variable that you're assigning), the standard syntax is to specify the type and then the variable name:
DataTable dataTable
(I highly recommend using a better variable name, by the way. C# is case-sensitive, so this is valid. But it's unintuitive at best.)
In C# you can also use the var keyword to infer the type, often resulting in cleaner code:
var dataTable = new DataTable();
This only works if there's an inferrable type from the right side of the assignment. Since the DataSource property isn't specifically of type DataTable then you don't want to use var in this particular case, since it would result in an Object (which isn't what you're looking for). But it can be used in cases like my last example above this paragraph where you don't want to repeat the type name twice in the same line of code.
DataTable dataTable = ((GridView)sender).DataSource
Regarding sorting in a GridView, see Sorting Data in a GridView Web Server Control, then post a question with what you have tried if you're having problems.

Resources