|
You have some IndexOutOfRange problems...
As a little VB exercise, here an alternative, spanning a larger range of values; the result is slightly different, notation is float or integer as appropriate:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim pow As Long = 1
For i = 0 To 63
Dim fraction As Long = pow \ 32
If fraction < 1 Then fraction = 1
test(-pow)
test(pow - fraction)
test(pow)
test(pow + fraction)
If pow >= &H4000000000000000 Then Exit For
pow = 2 * pow
Next
End Sub
Public Sub test(ByVal value As Long)
Dim s As String
s = FormattedFileSize(value)
Console.WriteLine(value.ToString().PadLeft(20) & " = " & value.ToString("X16") & " = " & s & "B")
End Sub
Public Function FormattedFileSize(ByVal sizeInBytes As Long) As String
Dim negative As Boolean = False
Dim result As String
Dim value As Long = sizeInBytes
Dim remainder As Long = 0
Dim suffixIndex As Integer
If value < 0 Then
negative = True
value = -value
End If
For suffixIndex = 0 To 20
If value <= 1023 Then Exit For
remainder = value Mod 1024
value = value \ 1024
Next
If remainder > 0 Then
Dim d As Double = value + remainder / 1024
result = d.ToString("F3")
Else
result = value.ToString()
End If
If suffixIndex > 0 Then result = result & " KMGTPEZY"(suffixIndex)
If negative Then result = "-" & result
Return result
End Function
Homework: find and fix the value(s) that still fail.
|
|
|
|
|
I knew it would error for extremely large sizes and failed on negatives so here's a fix. (Not sure the negatives a right.)
Public Function FormattedFileSize(ByVal sizeInBytes As Long) As String
Dim suffix() As String = New String() {"Oops", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "XX"}
Dim units As Double = Math.Abs(sizeInBytes) 'Fudge to deal with negatives. Should it error with neg file size?
Dim index As Integer = 0
Do
units = units / 1024.0
index += 1
Loop While units >= 1024.0
Return Format(units, "###,###,##0.000") + suffix(index)
End Function
I've not done homework for years.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
The only valid measurement of code quality: WTFs/minute.
|
|
|
|
|
Thank you all for your input! I certainly was happy that this issue had a lot of interest.
"i would hope that anybody who is working in IT knows the relationships between bits bytes k, m, g, t.....and on and on." -- knowing the relationship between bytes, KB, MB, etc.. was irrelevant as I was asking if there was a class/method that automatically did the conversions in VB.Net. And yes, I know the relationship between them :P
"I think he meant: no need for me to test it, just publish and wait a while." -- Yea I'm sure all programmers are perfect testers of THEIR own code. I did some testing, but it obviously wasn't good enough. I suppose that's what learning is.. no?
"What if file is exactly 1024 or 1048576 bytes?" -- Good point! I was swamped with work that I overlooked that fault. Thank you.
"But there's even worse when you look at it more closely e.g. a file of 1023 bytes returns 1,023.00KB." -- Another good point. I need to address all eventualities more closely!
"I decide to benefit and wrote something that does work." -- Thank you. I will work through that code.
Thanks again all! This made my day.
|
|
|
|
|
Hi,
I'm new to the forums so apologies if I've posted this to the wrong section. Also pretty new to vb so if some of my terminology is off then please let me know.
I'm trying to develop an asp page that will take user submitted data and post it to a web service. The web service is hosted (and developed by) a third party.
I've created a reference to the web service in my project, and written the code to send the required data to the web service, but every time I send data I get a response back from the web service to say it is duplicate data and won't be processed. Having done what debugging my limited knowledge allows, it looks as though both the public property and private field for each class of the web service is being sent, one after the other, which is causing the duplicate data message.
I'm guessing I haven't correctly defined/coded something so can someone suggest where I may be going wrong please. Below is the code I'm using but let me know you need anything else.
Thanks
Rob.
Protected Sub button1_click() Handles Button1.Click
Dim strResult As String
Dim strResultErrorCode As String
Dim strResultErrorId As String
Dim strResultErrorMessage As String
Dim strResultMessage As String
Dim WebService As VWTestdrive.TestdriveService = New VWTestdrive.TestdriveService()
Dim Request As New VWTestdrive.Request
Dim Result As New VWTestdrive.Result
Dim Customer As New VWTestdrive.Customer
Dim Address As New VWTestdrive.Address
Dim Phones() As VWTestdrive.Phone = New VWTestdrive.Phone() _
{New VWTestdrive.Phone, New VWTestdrive.Phone, New VWTestdrive.Phone, New VWTestdrive.Phone}
Dim HomePhone As New VWTestdrive.Phone
Dim MobilePhone As New VWTestdrive.Phone
Dim WorkPhone As New VWTestdrive.Phone
Dim FaxPhone As New VWTestdrive.Phone
Dim UnkPhone As New VWTestdrive.Phone
Dim NextPurchase As New VWTestdrive.NextPurchase
Dim Fleet As New VWTestdrive.Fleet
Dim DataProtection As New VWTestdrive.DataProtection
Dim Channels() As VWTestdrive.ContactChannel = New VWTestdrive.ContactChannel() _
{New VWTestdrive.ContactChannel, New VWTestdrive.ContactChannel, New VWTestdrive.ContactChannel, New VWTestdrive.ContactChannel}
Dim ChMail As New VWTestdrive.ContactChannel
Dim ChEmail As New VWTestdrive.ContactChannel
Dim ChSMS As New VWTestdrive.ContactChannel
Dim ChPhone As New VWTestdrive.ContactChannel
Dim CurrentVehicle As New VWTestdrive.CurrentVehicle
Dim FutureVehicle As New VWTestdrive.FutureVehicle
Dim Loyalty As New VWTestdrive.BrandLoyalty
Dim VehicleRequests() As VWTestdrive.VehicleRequest = New VWTestdrive.VehicleRequest() _
{New VWTestdrive.VehicleRequest, New VWTestdrive.VehicleRequest, New VWTestdrive.VehicleRequest, New VWTestdrive.VehicleRequest}
Dim Campaign As New VWTestdrive.Campaign
' get credentials
Dim Credentials As New NetworkCredential
Credentials = ReturnCredentials(3)
WebService.Credentials = Credentials
'Try
' set customer data
Customer.title = "Mr"
Customer.firstName = "Duplicate"
Customer.lastName = "Test"
Customer.email = "test.test@test.com"
' need to test that db field is populated before adding to request object
'HomePhone.type = VWTestdrive.PhoneType.Home
'HomePhone.Value = ""
MobilePhone.type = VWTestdrive.PhoneType.Mobile
MobilePhone.Value = "07123456789"
WorkPhone.type = VWTestdrive.PhoneType.Work
WorkPhone.Value = "01234567890"
'FaxPhone.type = VWTestdrive.PhoneType.Fax
'FaxPhone.Value = ""
'UnkPhone.type = VWTestdrive.PhoneType.Unspecified
'UnkPhone.Value = ""
' need to add a loop here dependent on the number of numbers populated
Phones(0).type = MobilePhone.type
Phones(0).Value = MobilePhone.Value
'Phones(0).preferred =
'Phones(0).preferredSpecified =
Phones(1).type = WorkPhone.type
Phones(1).Value = WorkPhone.Value
'Phones(1).preferred =
'Phones(1).preferredSpecified =
'Phones(2).type = HomePhone.type
'Phones(2).Value = HomePhone.Value
'Phones(2).preferred =
'Phones(2).preferredSpecified =
'Phones(3).type = FaxPhone.type
'Phones(3).Value = FaxPhone.Value
'Phones(3).preferred =
'Phones(3).preferredSpecified =
'Phones(3).type = UnkPhone.type
'Phones(3).Value = UnkPhone.Value
'Phones(3).preferred =
'Phones(3).preferredSpecified =
' set address data
Address.pafValidated = True
Address.pafValidatedSpecified = True
Address.type = VWTestdrive.AddressType.Residential
Address.typeSpecified = True
Address.houseNumber = "1"
Address.houseName = ""
Address.line1 = "Test Avenue"
Address.line2 = ""
Address.line3 = ""
Address.line4 = "LONDON" ' map to town
Address.line5 = "" ' map to county
Address.postcode = "AA1 1AA"
Customer.address = Address
Customer.phones = Phones
' need to test that db field is populated before adding to request object
ChMail.option = True
ChMail.type = VWTestdrive.ChannelType.Whitemail
ChEmail.option = True
ChEmail.type = VWTestdrive.ChannelType.Email
ChSMS.option = True
ChSMS.type = VWTestdrive.ChannelType.SMS
ChPhone.option = True
ChPhone.type = VWTestdrive.ChannelType.Phone
' need to add a loop here dependent on the number of fields populated
Channels(0).option = ChMail.option
Channels(0).type = ChMail.type
Channels(1).option = ChEmail.option
Channels(1).type = ChEmail.type
Channels(2).option = ChSMS.option
Channels(2).type = ChSMS.type
Channels(3).option = ChPhone.option
Channels(3).type = ChPhone.type
' next purchase
NextPurchase.date = "2009-08-25"
NextPurchase.dateSpecified = True
'NextPurchase.timeframeId =
Fleet.responsibility = False
Fleet.responsibilitySpecified = True
'Fleet.size = "0"
DataProtection.group = True
DataProtection.thirdParty = True
' need to get modelcode from lookup
CurrentVehicle.modelCode = "1J1" ' Golf
CurrentVehicle.registration = "AA11 AAA"
' add future vehicle here
Loyalty.previousOwner = True
Loyalty.previousOwnerSpecified = True
' need to add loop for populating all possible brochures
' will also need to split brochure array into separate elements
For i As Integer = 0 To 3
Dim Vehicle As New VWTestdrive.VehicleRequest
If i = 0 Then
Vehicle.derivativeCode = "VW0302"
Vehicle.type = VWTestdrive.VehicleRequestType.B
ElseIf i = 1 Then
Vehicle.derivativeCode = "VW0303"
Vehicle.type = VWTestdrive.VehicleRequestType.B
ElseIf i = 2 Then
Vehicle.derivativeCode = "VW0302"
Vehicle.type = VWTestdrive.VehicleRequestType.T
ElseIf i = 3 Then
Vehicle.derivativeCode = "VW0303"
Vehicle.type = VWTestdrive.VehicleRequestType.B
End If
VehicleRequests(i).derivativeCode = Vehicle.derivativeCode
VehicleRequests(i).type = Vehicle.type
Next
' campaign
Campaign.code = "UKVW-CE-VWINTERNET02"
Campaign.source = "60"
'Campaign.freePrizeDraws = ""
' set request data
' defaults
' customer
Request.customer = Customer
' info
Request.nextPurchase = NextPurchase
Request.fleet = Fleet
Request.dataProtection = DataProtection
' contact channels
Request.futureContactChannels = Channels
Request.currentVehicle = CurrentVehicle
Request.brandLoyalty = Loyalty
Request.vehicleRequests = VehicleRequests
' campaign
Request.campaign = Campaign
' evaluate response codes
Result.success = WebService.CallTestDrive(request:=Request).success
strResult = Result.success.ToString
UserMsgBox(strResult)
Result.errors = WebService.CallTestDrive(Request).errors
strResultErrorCode = Result.errors(0).errorCode.id.ToString
strResultErrorId = Result.errors(0).id.ToString
strResultErrorMessage = Result.errors(0).errorMessage.ToString
strResultMessage = "Id: " & strResultErrorId & vbCrLf & _
"Code: " & strResultErrorCode & vbCrLf & _
"Message: " & strResultErrorMessage & vbCrLf
UserMsgBox(strResultMessage)
'Catch ex As Exception
'End Try
End Sub
|
|
|
|
|
Hi
I think your best bet is to get hold of the 3rd party who developed the Web Service and get their support. I seriously doubt anyone here would be able to help with such a specific problem, nor do I think anyone will bother reading so much code to figure out what is going on. There are lots of people here who are more than willing to help you if you have a more general problem regarding VB.Net, or any other language, so please don't be discouraged if you don't receive many replies to this question.
|
|
|
|
|
Your best bet is contacting the people who wrote/own the web service. Either the data already exists in some form in the web services database, or you're supplying incorrect information in the objects.
|
|
|
|
|
Hello Everybody,
In VB6 the Function char(0) returns special character
but in VC++ it return blank.
Which reflect on the value of Software.
so please help me what is the solution.
Thanks
If you can think then I Can.
|
|
|
|
|
eg_Anubhava wrote: so please help me what is the solution.
The solution to what? Please try and explain what you are trying to achieve, and whether this is a VB or C++ question.
It's time for a new signature.
|
|
|
|
|
I want to know that what is the reason and how to solve it.
I want to get the same result in VC++.
If you can think then I Can.
|
|
|
|
|
eg_Anubhava wrote: I want to know that what is the reason and how to solve it.
The reason for what? char(0) in C++ returns 0 as it should; what value did you expect?
It's time for a new signature.
|
|
|
|
|
char(0) returns '' in C++
char(0) returns 'o' in VB6
I want in C++ that character which VB6 char(0) is retured.
If you can think then I Can.
|
|
|
|
|
char(0) is the NULL character in any language, so your VB6 code cannot possibly be correct.
To get a lower case 'o' as you have shown is char(111) .
I don't know how that would be coded in C++. for that the C++ forum[^] would be your best bet.
|
|
|
|
|
I have no idea where you are getting this information from but you are wrong in both cases. Try again to explain exactly what you are trying to do, and maybe someone can help you. Maybe you should post any code that you have written where you think it is generating either of these values.
It's time for a new signature.
|
|
|
|
|
In VB6, strings can contain any character including character code 0; the system explicitly keeps track of the length. When the system prints out a string, the print-out routine is told how many characters to print. In C, rather than storing the length of a string, many routines simply scan through the string until they hit a character code 0 and then stop. Character code 0 in VB6 show up as a funny box character; so do many other codes. In C, character code 0 is not valid within a string, but some other characters that yield the funny box character are valid, and may appear within a string.
|
|
|
|
|
Yes you are absolutely correct.
If you can think then I Can.
|
|
|
|
|
In C\C++ char(0) isn't a function, it's an explicit cast.
I suppose you tried to print it somehow as a string. The NULL character is a string terminator so it's not printed.
Cheers
I don't like my signature at all
modified on Wednesday, May 5, 2010 5:28 AM
|
|
|
|
|
|
We'll set aside that you are showing C# code in the VB forum, shall we?
Check the properties of your tableAdapterManager, there should be a bit somewhere, that says something like update command. Apparently Visual Studio could not create the necessary sql sentence, or it got corrupted.
My advice is free, and you may get what you paid for.
|
|
|
|
|
Hi all,
I'm trying to bind a child repeater within a parent repeater by counting through all the rows of the parent repeater and for every row I would like to bind the child repeater.
I need help with the way it works.. this is what I have so far:
Sub BindParent()
'Bind Parent
End Sub
Public Sub rpParent_Databound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
For Each Item As RepeaterItemCollection In rpParent.Items
'Bind Child
Next
End Sub
Currently each Child row has a Parent row. I would like there to be a Parent row with many Child rows, and then the next Parent row would appear.
Basically it is not counting correctly. I hope all this makes sense
|
|
|
|
|
Hi
I wish to have fun a little with the 3D under VB.net, and for some time I get information a little to know what to use.
Until recently, I thought that there were only Direct3D or OpenGL, but I have just discovered XNA and SlimDX, but I am lost a little.
Apparently, XNA is not supported by VB, and as I do not know too C#, it is apparently not the good solution for me.
In many forum topics, one can read that DirectX is not supported any more.
Consequently, I am lost, and I wonder what is the best solution. I do not wish to make a game, only a mesh viewer and some calculations on the position of the points of the mesh.
My true goal would be to make a 3D scanner, but already, a mesh viewer would be already well.
What would advise me to be used? Especially, that is what will be simplest?
It is really disadvised using Direct3D as many examples of it are found?
Thanks in advance for your advises ... and sorry for my bad english
edit:
I have forgotten WPF. I don't know WPF very well,I am more at ease with the winforms.
is this an good idea to make a component WPF and to import it in a winform?
that annoys to me to pass in WPF right for the 3D.
|
|
|
|
|
There isn't any good VB.NET solution for 3D. Everything that uses managed code uses C#. There are other solutions out there that use a flavor of BASIC though.
As far as C#, you know more about it than you realize. VB.NET and C# are not all that different. Most of what you're typing is using .NET classes and methods anyway and the structure of each of those statements is very similar almost like you're typing VB.NET and putting a semicolon on the end.
Seriously, if you want to pigeon-hole yourself into VB.NET, that's fine. But if you want to expand your skills beyond just VB.NET, then now is the perfect time to learn C#. You'll find a ton of XNA samples and examples, all written in C#.
norrisMiou wrote: I have forgotten WPF. I don't know WPF very well,I am more at ease with the winforms.
is this an good idea to make a component WPF and to import it in a winform?
that annoys to me to pass in WPF right for the 3D.
Pickup a book on WPF. It's the best way to go to learn it in an organized manner.
|
|
|
|
|
norrisMiou wrote: and sorry for my bad english
Don't appologise for trying.
Steve Jowett
-------------------------
Real programmers don't comment their code. If it was hard to write, it should be hard to read.
|
|
|
|
|
Thanks for your replies.
I know that I should upgrade to C#, but I hate this semicolons and brackets ... I am just a small "sunday programmer" as we say in France
Some people on others forums say that XNA works with VB.Net ... that's right ? It seemed me to have read that many functions of XNA were not supported by VB.
|
|
|
|
|
Ok. I tested XNA with C#, and I acknowledge that it is not so hard which I thought it. In 5 minutes, I succeeded with loading a 3D mesh and that seems easier to set up than with MDX.
I then sought how to load an XNA window in a traditional winform while following the examples on their site.
That doesn't seems as simple as I tought it. There is a whole package of additional classes of which I did not really understand the utility. Ok, it is not very serious, I could spend a little time understanding them.
It would seem that one is obliged to install XNA Game Studio to make function these examples and that the redistribuable DLL is not enough.
What I believed to understand, which it is XNA or WPF, both function with DirectX? Therefore, I don't understand why we could not continue to use it.
Therefore, if MDX is deprecated and that XNA needs the Game Studio to function, there does remain only WPF which would be even sufficed for him?
What does we have to use if we just want to reach information of the graphic card ?
I am thus say “Ok, continuous with MDX”. But I have upgraded to .Net 4 and this have not DirectX in .Net references
Very crazy thing ! no ? Or then there is something which I did not understand.
|
|
|
|
|
You do NOT need XNA Studio to run the finished .EXE. Read this[^].
|
|
|
|
|