Can one ►variable◄ have/contain more than one value ?

It may be something instead of variable if possible.

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 2872 Views - Last Post: 12 December 2010 - 08:02 AM Rate Topic: ***** 1 Votes

#1 monkeyB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-December 10

Can one ►variable◄ have/contain more than one value ?

Posted 08 December 2010 - 01:04 PM

For exmaple:

Button 1 clicked
Set variable1 = 5 and 10 and 15 and 21
Set variable2 = 5 and 15

button 2 clicked
If variable1 contain 5 and 15
then...
blablabla

OR(i prefer this one if possible.)

button 2 clicked
if variable1 contain at least 2 number that they are in variable1(variable 2 contain 5 and 15 btw. which are in variable 1 too, so its correct.)
then
blabla

Possible ? if so, how ? id like to see an example code.

Is This A Good Question/Topic? 0
  • +

Replies To: Can one ►variable◄ have/contain more than one value ?

#2 SarumanTheWhite  Icon User is offline

  • D.I.C Regular

Reputation: 67
  • Posts: 339
  • Joined: 04-November 08

Re: Can one ►variable◄ have/contain more than one value ?

Posted 08 December 2010 - 01:07 PM

Look into Arrays.
Was This Post Helpful? 1
  • +
  • -

#3 monkeyB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-December 10

Re: Can one ►variable◄ have/contain more than one value ?

Posted 08 December 2010 - 01:13 PM

View PostSarumanTheWhite, on 08 December 2010 - 12:07 PM, said:

Look into Arrays.

If i remember correctly, arrays like another variable, so i cant check if variable1(not any specific array and so) contain at least 2 valuable from variable2
Correct me if im wrong.
Was This Post Helpful? 0
  • +
  • -

#4 SarumanTheWhite  Icon User is offline

  • D.I.C Regular

Reputation: 67
  • Posts: 339
  • Joined: 04-November 08

Re: Can one ►variable◄ have/contain more than one value ?

Posted 08 December 2010 - 01:34 PM

Try an ArrayList. It has a function ( Contains ) that checks if an element is present or not
Was This Post Helpful? 1
  • +
  • -

#5 lordofduct  Icon User is online

  • I'm a cheeseburger
  • member icon


Reputation: 2055
  • View blog
  • Posts: 4,050
  • Joined: 24-September 10

Re: Can one ►variable◄ have/contain more than one value ?

Posted 08 December 2010 - 01:46 PM

Arrays have a contains function... it's static and it's not called 'contains'...

Dim arr() As Integer = New Integer() {5,10,15,21}

If Array.IndexOf(arr,5) >= 0 AndAlso Array.IndexOf(arr, 15) >= 0 Then
    ''-1 is returned if nothing is found... otherwise a 0 or positive index is returned
    ''do stuff
End If




@monkeyB - pick up your textbook (preferrably a .Net text, if you don't have one GET ONE) and open to the chapter on arrays. Arrays are one of the simplest structures in programming, you should learn everything you can about them now.

This post has been edited by lordofduct: 08 December 2010 - 01:49 PM

Was This Post Helpful? 1
  • +
  • -

#6 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 956
  • View blog
  • Posts: 3,676
  • Joined: 02-July 08

Re: Can one ►variable◄ have/contain more than one value ?

Posted 08 December 2010 - 02:12 PM

Also check out generics List(Of T) a type specific container with simple .Add, .AddRange, .Remove, and of course the .Contains functions. No more Redim statements!

Dim myIntegerList As New List(Of Integer)

Was This Post Helpful? 1
  • +
  • -

#7 monkeyB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-December 10

Re: Can one ►variable◄ have/contain more than one value ?

Posted 08 December 2010 - 02:19 PM

View Postlordofduct, on 08 December 2010 - 12:46 PM, said:

Arrays have a contains function... it's static and it's not called 'contains'...

Dim arr() As Integer = New Integer() {5,10,15,21}

If Array.IndexOf(arr,5) >= 0 AndAlso Array.IndexOf(arr, 15) >= 0 Then
    ''-1 is returned if nothing is found... otherwise a 0 or positive index is returned
    ''do stuff
End If




@monkeyB - pick up your textbook (preferrably a .Net text, if you don't have one GET ONE) and open to the chapter on arrays. Arrays are one of the simplest structures in programming, you should learn everything you can about them now.

Didnt tried it yet, will try tomorrow or so but i guess it will work.
Thanks.

View Posthawkvalley1, on 08 December 2010 - 01:12 PM, said:

Also check out generics List(Of T) a type specific container with simple .Add, .AddRange, .Remove, and of course the .Contains functions. No more Redim statements!

Dim myIntegerList As New List(Of Integer)

Aww, so there is such a thing called list, cool. i will check it out tomorrow
Was This Post Helpful? 0
  • +
  • -

#8 lordofduct  Icon User is online

  • I'm a cheeseburger
  • member icon


Reputation: 2055
  • View blog
  • Posts: 4,050
  • Joined: 24-September 10

Re: Can one ►variable◄ have/contain more than one value ?

Posted 08 December 2010 - 02:35 PM

yes there's all sorts of different lists, dictionaries, stacks... etc

they're called 'collections' and exist in the System.Collections namespace:

http://msdn.microsof...ollections.aspx

preferred over collections though is the generic versions of them (which is the List(Of T) that hawkeye brings up). They are in the System.Collections.Generic namespace:

http://msdn.microsof...ns.generic.aspx

Generic versions allow you to hold onto type safety with your collection (by defining the type when you create it).



Where an array is just a simple group of objects or values in a static sized container. These are more dynamic object oriented collections that give you a lot of freedoms over arrays. Most of them can be implemented with an array, but some of the methods would be pretty convoluted when working with an array (for instance add range mid way through an array... requires resizing, shifting existing, and adding the range).

This post has been edited by lordofduct: 08 December 2010 - 02:38 PM

Was This Post Helpful? 1
  • +
  • -

#9 south73paw  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 84
  • Joined: 06-October 08

Re: Can one ►variable◄ have/contain more than one value ?

Posted 08 December 2010 - 06:45 PM

Variables can only store ONE piece of data at a time although this data can change over time (such as with each click of a button).

Arrays can store MULTIPLE pieces of data at the same time, so long as they are of the SAME DATA TYPE.

Use a STRUCTURE (i.e. array of records) if you want to store multiple columns of data of different types, e.g. string and integer).

Hope that helps! :bigsmile:
Was This Post Helpful? 1
  • +
  • -

#10 monkeyB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-December 10

Re: Can one ►variable◄ have/contain more than one value ?

Posted 09 December 2010 - 12:40 PM

Well, im sorry but can i ask for a example code that says
if v1 have at least 2 value in v2 then ?
Was This Post Helpful? 0
  • +
  • -

#11 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 956
  • View blog
  • Posts: 3,676
  • Joined: 02-July 08

Re: Can one ►variable◄ have/contain more than one value ?

Posted 09 December 2010 - 12:43 PM

You'll have to tell us what type your using to hold these values.
Was This Post Helpful? 1
  • +
  • -

#12 monkeyB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-December 10

Re: Can one ►variable◄ have/contain more than one value ?

Posted 09 December 2010 - 02:00 PM

View Posthawkvalley1, on 09 December 2010 - 11:43 AM, said:

You'll have to tell us what type your using to hold these values.

I will use them as check variables, so they may be string, number(integer) etc. doesnt really matter and i do believe i can change it, and understand it(or at least i will try to do)
off-topic: What are "kudos" ?
Was This Post Helpful? 0
  • +
  • -

#13 lordofduct  Icon User is online

  • I'm a cheeseburger
  • member icon


Reputation: 2055
  • View blog
  • Posts: 4,050
  • Joined: 24-September 10

Re: Can one ►variable◄ have/contain more than one value ?

Posted 09 December 2010 - 02:13 PM

I think he meant collection type (List, Dictionary, Array, etc)


here's with IList (a lot of collections implement IList, including List and List(Of T)). It's just a naive search and if col1 has duplicates it will consider them as unique. Meaning:

{2,2,1} {2,3} shares 2 because the first has 2 2's in it. Where as if you flipped it would only share 1. Hence why I call it naive.


    Public Shared Function SharesSomeValues(ByVal col1 As IList, ByVal col2 As IList, ByVal shareCount As Integer) As Boolean

        Dim cnt As Integer = 0

        For Each val1 In col1
            If col2.Contains(val1) Then
                cnt += 1
            End If
        Next

        Return (cnt >= shareCount)
    End Function


Was This Post Helpful? 1
  • +
  • -

#14 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 956
  • View blog
  • Posts: 3,676
  • Joined: 02-July 08

Re: Can one ►variable◄ have/contain more than one value ?

Posted 09 December 2010 - 08:26 PM

Quote

What are "kudos" ?


Kudos are points we receive when we submit tutorials or code snippets. On the Black bar near the top you will find both - explore them both as there you will find much coolness!
Was This Post Helpful? 1
  • +
  • -

#15 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 929
  • View blog
  • Posts: 6,316
  • Joined: 18-October 08

Re: Can one ►variable◄ have/contain more than one value ?

Posted 09 December 2010 - 08:33 PM

If you are interested in the generic List(Of T) I have a rather simple introduction to them here.
http://www.dreaminco...lections-lists/
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2