Solved

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

Posted on 2014-01-28
3
281 Views
Last Modified: 2014-01-30
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: ....?

thanks in advance
0
Comment
Question by:BemboX
3 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39816479
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

by:
Ark earned 500 total points
ID: 39820039
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

Open in new window

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

Open in new window

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)

Open in new window

or with VS2010 use With keyword:
Dim vtotals As New strTotals With {.total1 = 1, .total2 = 2, .total3 = 3}

Open in new window

0
 

Author Closing Comment

by:BemboX
ID: 39821168
Thanks.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Regex validation 2 24
VB.Net - MemoryMappedFiles - Confirm receipt 2 17
Sending receiving text messages in vb.net 15 32
VB.NET 2008 - SQL Timeout 9 23
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now