|
Ok, so I should know this but I just do not know how to create a global variable in VB.NET i am more used to programming in VB6 and am still adjusting to .NET, so I need to ask how do I create a global variable?
I understand that it would be placed after the Public Class (project name) line but then how Would the syntax be for creating a global variable??? I also already know that I should try to not use global variables but I just am writing a very simple program so dont worry I do know that Global variables sometimes are not the best to use.
Thanks.
|
|
|
|
|
Well, for starters, global variables are nearly always bad. But, if you need one, it's just a shared variable ( which is the same as static in C# ). The difference is, it needs to exist in a class, you can't just create a variable in space somewhere. Other than that, it's the same thing. The keyword is 'shared', not 'static' in VB, and I don't know enough VB to give it to you beyond that, but if you google VB.NET shared, you should find examples.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Okay so I looked it up on google but didn't find any good examples of the syntax for global variables in VB.NET
i am trying to do this:
Public class Invest '(invest being my project name)
'then create a global variable, as Christian said I tried using the shared function but still dont know the proper syntax
Shared Dim iCount as Integer = txtAge.text
I know the line of code i just posted is incorrect but I dont know the proper syntax for declaring global variables
can someone tell me what I am doing wrong???
|
|
|
|
|
Does VB.NET really magically turn your textbox text into an int ? What happens when it fails, does it throw an exception ? VB is so nasty.
I found this with google:
Public Shared ReadOnly Property DBName() As String
Get
Return "FunnyNumbers_DB_for_the_auditors_only"
End Get
End Property
I presume the readonly is optional. I also assume you can use the same syntax for variables as for properties.
A shared variable is initialised when the program starts, I believe. At a minimum, you can't know when it is initialised, so you can't know what the value of txtAge.text will be when it is initialised. I assume you have only one instance of this textbox, you should handle the lost focus or text changed events to update this value.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
You really shouldn't be creating "global" variables in the first place. If you do, it's a sign that you may have a problem with your class structure and are violating encapsulation rules.
Public Class MyGlobals
Shared Public someVar As Integer
End Class
And to get at it's value:
MyGlobals.someVar = 30
You would also be advised that it's better to wrap the variable in a Property to control access to the variable and do validation on any set attempts.
|
|
|
|
|
|
Can I overload the System.DateTimeOffset class so that I can write either:
dim d = now.date 'existing
or
dim p = now.mynewproperty 'overloaded by me
Thanks
|
|
|
|
|
You can derive a class from any class you like. I believe you can also use extension methods to add a method to a class, but I've never done that.
To add - you'd need to override the Now property to return an instance of your new class, if you wanted to add a property to the Now property.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Please if anyone could offer me help on this, I need to know how to invoke a dopostback in the web browser control in visual basic. My idea is that I want to capture data from a website (authorized) that is using asp.net server. The website has a dropdownlist that postbacks according to the value chosen and shows the value (that i need to capture).
If anyone could Help , I would appreciate it
THanks
|
|
|
|
|
Hi everyone,
I have a datagridview with 3 columns "Name", "Age", "salary".
While displaying in datagridview, I hide the "Age" column and display only the two column using this code:
DataGridsearch.Columns("Age").Visible = False
Now I have written one code for ClickRow event of datagridview, in which when user clicks any row, the content of the cells get displayed into the respective textboxes for "Name" and "Salary". The code is like this:
Dim row_click As Integer
row_click = DataGridView.CurrentRow.Index
name.Text = DataGridView.Item(0, row_click).Value
salary.Text = DataGridView.Item(2, row_click).Value
Now, i am able to display both "Name" and "Salary" in its respective textboxes, from datagridview, but not able to display the "Age" in its textbox "age.Text".
Can anyone tell me how to do this, since I have already hide the "Age" column while displaying in datagridview?
|
|
|
|
|
You need to use VB equivalent of this C# code. DataGridView.Rows[RowIndex].Cells[ColIndex].Value.ToString()
It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
|
|
|
|
|
Hi,
I didn't get how to implement this as I have hidden 1 column. Can i get some help on this more?
|
|
|
|
|
I wrote an application in VB 2003 which used the "SqlClient.SqlConnection". I used standard connection code;
ConnectionString="workstation id=MySys;packet size=4096;data source=MySQL;User ID=RP;PASSWORD=RP;initial catalog=Inventory"
cnData.ConnectionString = ConnectionString
Try
cnData.Open()
Catch
MsgBox("Data Source could not be opened.")
Exit Sub
End Try
and it worked fine. I opened it in vb 2008 and it performed the conversion without any reported errors, but it will no longer connect?
MySys is a separate Development system and MySQL is another system on my LAN running SQL Server 2000.
I have been to www.connectionstrings.com[^] but after trying numerous things, nothing helped. Any ideas?
modified on Saturday, September 26, 2009 1:20 AM
|
|
|
|
|
Found the answer, it appears I had to add the Network Library to get this to work!
ConnectionString=Workstation ID=MySys;packet size=4096;Data Source=MySQL;User ID=RP;Password=rp;Initial Catalog=Inventory;Network Library=dbnmpntw
That must have been assumed in VB 2003?
|
|
|
|
|
Hi,
Can anyone give me a simple code to import a datagrid to excel?
I look over the web, it appears that I cannot find a suitable code for my application.
I am using visual studio 2005..
one problem that I encountered from some of the codes that I found is that I cannot use this import syntax(Imports Excel = Microsoft.Office.Interop.Excel) for excel.
Thanks for your help with this question.
|
|
|
|
|
Since you have a datagrid, you must have it bound to a dataset. Right?
You could instantiate Execl in your VB program like this:
Imports Microsoft.Office.Interop
Dim ExcelApp As New Excel.Application
Here are some additional code snipets ...
Dim ExcelBookTmp, ExcelBookMaster As Excel.Workbook
Dim ExcelSheetTmp, ExcelSheetMaster As Excel.Worksheet
ExcelApp.Visible = True
ExcelApp.Workbooks.Add()
ExcelBookMaster = ExcelApp.Workbooks(1)
ExcelSheetMaster = ExcelBookMaster.Worksheets(1)
ExcelSheetMaster.Cells(1, 1) = "Hello World"
ExcelApp.Workbooks.Open(Me.TextBox1.Text, , True)
You could then loop around your dataset and populate the Excel worksheet.
Cool stuff.
|
|
|
|
|
Hi,
thanks for replying. But when I instantiated (Imports Microsoft.Office.Interop) excel. I got namespace or type in the imports"Imports Microsoft.Office.Interop" doesn't contain any public member or cannot be found.
Guide me on this.
Thanks
|
|
|
|
|
In VS2005, under the Project Menu, click Add Reference.
On the "COM" tab of that window, verify that Microsoft Office 11.0 or 12.0 Object Library is listed.
Question: Is MS-Office installed on the machine you are trying to develop this?
|
|
|
|
|
Hi David,
Thanks but the reference is there and ms office is installed.
Await for more suggestions.
Thanks alot
|
|
|
|
|
The most reliable and flexible solution is to use xml.
First create an excel file in MS Excel. Format it according to what you would like the end result to look like. Save the file as xml.
Open the xml file with visual studio, and see what it looks like. Then use a string or streamwriter or whatever, to create a copy of the header part of your file. Then loop through your datagrid, dataset, or datatable, and add each row to the rows bit of the file.
If necessary finish off with any footer part of the file, and then save the whole thing as whateverfilename.xls
And presto! you can create an xls file without needing ms office installed on the machine that your app is running on, and that will work in any Excel version, not just the version that happens to be installed on the development machine.
My advice is free, and you may get what you paid for.
|
|
|
|
|
Thanks but I know nothing in xml.
|
|
|
|
|
|
Add a comment within your code with TODO: at the start and it will appear in your To Do list in Visual Studio. Very handy.
for example:
'TODO: Review this code
Steve Jowett
-------------------------
Real programmers don't comment their code. If it was hard to write, it should be hard to read.
|
|
|
|
|
Hi everyone i need to create a match fixture in vb.net i need to write a code that enable two teams compete against each other in a match game. can someone tell me how to do this please
thanks
|
|
|
|
|
Define what you mean by "fixture" and what a "match" consists of.
|
|
|
|