Visual Basic 2010 Binary Calculator Problem/Questions

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 4858 Views - Last Post: 04 November 2011 - 10:12 AM Rate Topic: -----

#1 h1z1  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 21-September 11

Visual Basic 2010 Binary Calculator Problem/Questions

Posted 01 November 2011 - 09:23 AM

Basicly I'm trying to make a 1 lined binary converter. Ill send you guys some screenshots if you want to help, I'm not begging for you guys to do it. I saw on the dash it said you guys would be happy to help.
Code:
If (tb1.Text) * (CInt(tb2.Text) + (tb3.Text)) * (CInt(tb3.Text) + (tb4.Text)) * (CInt(tb4.Text)) Then Label6.Text

Screenshots:
What it's going to look like:
Posted Image
My code:
Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: Visual Basic 2010 Binary Calculator Problem/Questions

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Visual Basic 2010 Binary Calculator Problem/Questions

Posted 01 November 2011 - 09:29 AM

Are we trying to convert binary to decimal or decimal to binary here?

If it's binary to decimal just iterate through all of the textbox's, if the user has inputed a '1' then add the corresponding number to a running total, if they have entered a '0' then skip on to the next one.

What are you trying to accomplish here?
Was This Post Helpful? 1
  • +
  • -

#3 h1z1  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 21-September 11

Re: Visual Basic 2010 Binary Calculator Problem/Questions

Posted 01 November 2011 - 09:32 AM

Basicly it's coverting a binary code to what number it represents. I want it so that if you enter a binary code into the textboxes then it will covert the code to what number it represents.
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Visual Basic 2010 Binary Calculator Problem/Questions

Posted 01 November 2011 - 09:38 AM

In that case take a look through my previous post.

You can also make good use of the .tag property in each textbox. If you put the number that they represent in the tag, when it comes to adding on the numbers, it will be a lot easier.

Essentially what you need to do is go through each textbox, look at the .Text property, if it's a "1", then add the number of the .tag to a running total variable. Don't do anything if the .Text property is not a "1". Then after you have visited all the textboxes, display the running total variable in the result label.
Was This Post Helpful? 1
  • +
  • -

#5 h1z1  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 21-September 11

Re: Visual Basic 2010 Binary Calculator Problem/Questions

Posted 01 November 2011 - 09:39 AM

Sadly I'm not quite following you, care to give an example?
Was This Post Helpful? 0
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Visual Basic 2010 Binary Calculator Problem/Questions

Posted 01 November 2011 - 09:44 AM

Well I won't give you the complete solution but -

if tb1.Text = "1" Then
   runningTotal += 8;


Then you do the same for the rest of the textbox's. Of course this is a rather crude way of doing it with all the if statements but it will get the job done.

This post has been edited by Ryano121: 01 November 2011 - 09:45 AM

Was This Post Helpful? 1
  • +
  • -

#7 h1z1  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 21-September 11

Re: Visual Basic 2010 Binary Calculator Problem/Questions

Posted 01 November 2011 - 09:49 AM

Any way I can use that method but with my one and only line of code? My tutor says it can be done with only 1 line of code under the Button1.Click

I can't figure out what you mean by "runningTotal" and "+=8;"
Was This Post Helpful? 0
  • +
  • -

#8 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Visual Basic 2010 Binary Calculator Problem/Questions

Posted 01 November 2011 - 09:51 AM

Well you might be able to do it with one line of code but its not always a good thing to do. It reduces the readability of your code and makes it a lot harder for someone else to figure out what it's meant to do.

runningTotal += 8 is the same as runningTotal = runningTotal + 8
Was This Post Helpful? 1
  • +
  • -

#9 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Visual Basic 2010 Binary Calculator Problem/Questions

Posted 01 November 2011 - 09:57 AM

If you really want a one line solution you might be able to use Convert.ToString() so -

resultLabel.Text = Convert.ToString(tbl1.Text + tbl2.Text ..., 10)

This is untested but it should work.


Scrap that there is no string, int overload.

This post has been edited by Ryano121: 01 November 2011 - 09:59 AM

Was This Post Helpful? 1
  • +
  • -

#10 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2538
  • View blog
  • Posts: 4,639
  • Joined: 24-September 10

Re: Visual Basic 2010 Binary Calculator Problem/Questions

Posted 01 November 2011 - 10:02 AM

Why do you have different text boxes for each digit in the binary number? You can't do this with 1 textbox?

Binary IS a number, it's just the base 2 representation of a number. We commonly write numbers in base 10, because that's what we humans are comfortable reading numbers in. It is postulated that it derives from the fact we have 10 fingers and 10 toes... because we tend to count. by. fingers.

And yes a conversion can be done in one line if you have the full string that user typed in. Either use ONE textbox, or combine the text in all text boxes. Then you parse the value, parsing is the act of reading a string and interpretting it as a non-string value. In this case you are reading a numeric value written in binary, and parsing it to a usable numeric value. A function exists for doing this and it's Convert.ToInt32(), one of its overloads supports base specification for the common bases (2/binary, 8/octal, 10/decimal, 16/hexadecimal).

http://msdn.microsof...y/1k20k614.aspx

It's also supported for conversions to all the other numeric types like byte, long, double, etc if need be. But Int32 is the one you probably care about right now.

THis of course just does the conversion for you, you may want to research about base conversion so you can understand that there is a big difference between the written out "5398" and the actual value of five thousand three hundred ninety eight. Think of it this way, see how I just wrote that number 2 different ways yet they're equal... well that's what binary is as well. Just another way to write it.

This post has been edited by lordofduct: 01 November 2011 - 10:04 AM

Was This Post Helpful? 1
  • +
  • -

#11 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Visual Basic 2010 Binary Calculator Problem/Questions

Posted 01 November 2011 - 10:04 AM

No scrap that again I found a solution -

resultLabel.Text = Convert.ToInt32(tb1.Text + tb2.Text ..., 2).ToString()


Edit - Beaten to it.

This post has been edited by Ryano121: 01 November 2011 - 10:05 AM

Was This Post Helpful? 1
  • +
  • -

#12 h1z1  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 21-September 11

Re: Visual Basic 2010 Binary Calculator Problem/Questions

Posted 02 November 2011 - 07:14 AM

So how would I do this with my original code?
Was This Post Helpful? 0
  • +
  • -

#13 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: Visual Basic 2010 Binary Calculator Problem/Questions

Posted 02 November 2011 - 07:29 AM

It would be a simple
Label6.Text = Convert.ToInt32(tb1.Text & tb2.Text & tb3.Text & tb4.Text, 2)

You want to concatenate the values in the text boxes so you end up with a full binary string, then run that through the convert function, with the 2 as the 2nd parameter to tell it you're passing a binary number to it.
Was This Post Helpful? 1
  • +
  • -

#14 h1z1  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 21-September 11

Re: Visual Basic 2010 Binary Calculator Problem/Questions

Posted 03 November 2011 - 09:55 AM

See, it HAS to be done using my original code, that means using multiplication and addition. I showed my instructor the code and he said it was a "cheap and easy solution". He said I would fail the project if I didn't use the original code.
Was This Post Helpful? 0
  • +
  • -

#15 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Visual Basic 2010 Binary Calculator Problem/Questions

Posted 03 November 2011 - 10:00 AM

What's wrong with a cheap and easy solution? That's what we all aim for when we are programming? Why overcomplicate something just for the hell of it when a much neater and easier solution exists?

I understand that you are just wanting to pass this project, but it really makes no sense at all.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2