Solved

# How to assing values to each element in a structure in vb.net in a single instruction?

Posted on 2014-01-28
273 Views
I have a strucutre in vb.net whose each element is a numeric variable, for example:
dim strTotals as structure
dim total1 as decimal
dim total2 as decimal
dim total3 as decimal
.....
end Structure

dim vtotals as strTotals

I want to assign 0 to each element in one single instruction. Is any way to do that or Do I have to assign 0 in this way: vtotal.total1=0: vtotal.total2=0: ....?

0
Question by:BemboX

LVL 83

Expert Comment

I can't think of any way other than perhaps using reflection to loop through structure elements which could be more work.
0

LVL 27

Accepted Solution

Ark earned 500 total points
First, your structure declaration looks like VB-Sharp. VB syntax should be
``````    Private Structure strTotals
Dim total1 As Decimal
Dim total2 As Decimal
Dim total3 As Decimal
End Structure
``````
Second, in VB you don't need initialize integers (as well as double, float, decimal and other numerics) with 0 - they are zero by default:
``````  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim vtotals As strTotals
MsgBox(vtotals.total1) 'show 0
End Sub
``````
If you need init structure with values other then 0, you can either use New method:
``````    Private Structure strTotals
Dim total1 As Decimal
Dim total2 As Decimal
Dim total3 As Decimal
Public Sub New(ByVal t1 As Decimal, ByVal t2 As Decimal, ByVal t3 As Decimal)
total1 = t1 : total2 = t2 : total3 = t3
End Sub
End Structure
'.....
Dim vtotals As New strTotals(1, 2, 3)
``````
or with VS2010 use With keyword:
``````Dim vtotals As New strTotals With {.total1 = 1, .total2 = 2, .total3 = 3}
``````
0

Author Closing Comment

Thanks.
0

## Featured Post

### Suggested Solutions

WPF issue with Trigger 2 64
Performance enhancement 39 25
Round a string to two digits 12 23
VB.NET HttpWebRequest 12 30
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…