Hi All,
I've been given the unenviable task of converting someone's VB6 code for an upload form into C#. I'm not doing to bad at the minute but I'm struggling with some of the conversion.
For example I have the following code (in C#):
======================
Hashtable strLang = new Hashtable();
Hashtable strAllowedExtensions = new Hashtable();
private bool SetLang()
//*** SetLang()
//* Add language strings to the array collection
//***
{
strLang.Add("INFO_FILE_SPEC", "- Is one of the following formats {0}, and is no bigger than {1}");
strLang.Add("ERROR_MAXSIZE_EXCEEDED", "The CV is bigger than the maximum size allowed.");
strLang.Add("ERROR_INVALID_FILETYPE", "The CV is in an unsupported format.");
strLang.Add("ERROR_SYSTEM_SAVE", "The system encountered an error while trying to save your CV. Please contact {0}.");
strLang.Add("ERROR_SYSTEM_EMAIL", "Error saving your CV: {1}. Please contact {0}.");
strLang.Add("INFO_EMAIL_TEXT", "Dear {0}," + '\n' + '\n' +
"A new CV has been uploaded - it is also attached to this e-mail." + '\n' + '\n' +
"The confirmation number of this upload is: {1}." + '\n' +
"The locally-saved filename is: {2}." + '\n' + '\n' +
"This e-mail was sent using the CV Upload Application.");
strLang.Add("INFO_EMAIL_SUBJECT", "A new CV has been uploaded.");
strLang.Add("DISPLAY_BYTE", "Bytes");
strLang.Add("DISPLAY_KBYTE", "KB");
strLang.Add("DISPLAY_MBYTE", "MB");
return true;
}
private bool SetExtensions()
//*** SetExtensions
//* Add extensions to the array collection
//***
{
strAllowedExtensions.Add("DOC", "Microsoft Word");
strAllowedExtensions.Add("PDF", "Adobe PDF");
return true;
}
private void GenerateFileSpec()
//*** GenerateFileSpec()
//* Return a list of specifications the upload must conform to
//***
{
string strSpec = "";
foreach (object objItem in strAllowedExtensions.Values)
{
strSpec = strSpec + ", " + objItem;
// Remove leading ", " from strSpec
strSpec = strSpec.TrimEnd(2); // THIS IS WRONG ALSO
GenerateFileSpec = string.Format(strLang("INFO_FILE_SPEC").ToString, strSpec.ToString, FormatMaxSize());
}
}
======================
I'm struggling to convert this piece of code
(strLang("INFO_FILE_SPEC").ToString
into something that will compile as it throws this error:
'_Default.strLang' is a 'field' but is used like a 'method'
I know it's because it's still VB6 and not C# but I'm not sure how to get it converted!
Can anyone point me in the right directions
Thanks,
Brett

Help a newbie! Converting VB6 to C#
BobS777
private string GenerateFileSpec()
//*** GenerateFileSpec()
//* Return a list of specifications the upload must conform to
//***
{
string strSpec = "";
foreach (object objItem in strAllowedExtensions.Values)
{
strSpec += (", " + objItem.ToString());
}
// Remove leading ", " from strSpec
strSpec = strSpec.Substring(2, strSpec.Length - 2);
return string.Format(strLang("INFO_FILE_SPEC").ToString(), strSpec.ToString(), FormatMaxSize());
}
But don't try to translate word by word, because VB 6.0 is very different from VB.NET, and even more from C#. Also if you work in C# 2.0 there are much more differences. If you have time, consider replacing Hashtable with Dictionary<string, string>.
Mohanraj_k
That works but I now get 'unreachable code detected' for
return strSize;
=============
private string FormatMaxSize(int maxUploadSize)
//*** FormatMaxSize()
//* Format the maximum upload size with Bytes/KBytes/MBytes etc
//**
{
string strSize;
//int intDisplaySize;
{
if (maxUploadSize < 1024)
return maxUploadSize + strLang["DISPLAY_BYTE"].ToString();
else if (maxUploadSize < 1048576)
return maxUploadSize / 1024 + strLang["DISPLAY_KBYTE"].ToString();
else
return maxUploadSize / 1048576 + strLang["DISPLAY_MBYTE"].ToString();
}
return strSize;
}
wpf michelle
try GenerateFileSpec = string.Format(strLang["INFO_FILE_SPEC"].ToString(), strSpec.ToString(), FormatMaxSize());
Alex Prado Romero
Thanks for your help so far; I'm getting there! Only 24 more errors!!!
The next part of the code is also throwing up some errors...
=============
private int FormatMaxSize()
//*** FormatMaxSize()
//* Format the maximum upload size with Bytes/KBytes/MBytes etc
//**
{
string strSize;
private int intMaxUploadSize = 1048576; //in Bytes -1048576 = 1MB
int intDisplaySize;
switch (intMaxUploadSize)
{
case 0:
intMaxUploadSize < 1024;
strSize = intMaxUploadSize + " " + strLang["DISPLAY_BYTE"];
case 1:
intMaxUploadSize >= 1048576;
intDisplaySize = intMaxUploadSize / 1048576;
strSize = intDisplaySize + " " + strLang["DISPLAY_MBYTE"];
default:
intDisplaySize = intMaxUploadSize / 1024;
strSize = intDisplaySize + " " + strLang["DISPLAY_KBYTE"];
}
FormatMaxSize = strSize;
}
================
Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Cannot assign to 'FormatMaxSize' because it is a 'method group'
are the errors in this piece of code.
Why am I getting an error on intMaxUploadSize and not on intDisplaySize
Brett
Boulderdude
Hello Brett,
the string.Format(string,object,object) function formats string and replaces values in objects in string. The FormatMaxSize method is returning noting, it needs to returns something(seems int in this case) Checkout for this method
evilc
private string FormatMaxSize()
//*** FormatMaxSize()
//* Format the maximum upload size with Bytes/KBytes/MBytes etc
//**
{
string strSize;
int intDisplaySize;
private int intMaxUploadSize = 1048576; //in Bytes -1048576 = 1MB
switch (intMaxUploadSize)
{
case intMaxUploadSize < 1024:
strSize = intMaxUploadSize + " " + strLang["DISPLAY_BYTE"];
break;
case intMaxUploadSize >= 1048576:
intDisplaySize = intMaxUploadSize / 1048576;
strSize = intDisplaySize + " " + strLang["DISPLAY_MBYTE"];
break;
default:
intDisplaySize = intMaxUploadSize / 1024;
strSize = intDisplaySize + " " + strLang["DISPLAY_KBYTE"];
break;
}
return strSize;
}
I'm getting an error on intMaxUploadSize in the first two case statements saying 'cannot convert bool to int', but it's already an int so why is it asking me to convert it
Brett
Javad Khadivi
That is because you are returning before you reach to "return strSize".
You may like to change the code to
private string FormatMaxSize(int maxUploadSize)
//*** FormatMaxSize()
//* Format the maximum upload size with Bytes/KBytes/MBytes etc
//**
{
string strSize = string.Empty;
//int intDisplaySize;
{
if (maxUploadSize < 1024)
strSize = maxUploadSize + strLang["DISPLAY_BYTE"].ToString();
else if (maxUploadSize < 1048576)
strSize = maxUploadSize / 1024 + strLang["DISPLAY_KBYTE"].ToString();
else
strSize = maxUploadSize / 1048576 + strLang["DISPLAY_MBYTE"].ToString();
}
return strSize;
}
OR
private string FormatMaxSize(int maxUploadSize)
//*** FormatMaxSize()
//* Format the maximum upload size with Bytes/KBytes/MBytes etc
//**
{
string strSize;
//int intDisplaySize;
{
if (maxUploadSize < 1024)
return maxUploadSize + strLang["DISPLAY_BYTE"].ToString();
else if (maxUploadSize < 1048576)
return maxUploadSize / 1024 + strLang["DISPLAY_KBYTE"].ToString();
else
return maxUploadSize / 1048576 + strLang["DISPLAY_MBYTE"].ToString();
}
}
Francis Shanahan
That worked but it's had a knock-on effect!
if
(objFileUpload.FileBytes.GetLength(0) > intMaxUploadSize)
{objFileUpload = null;
lblStatus = SetError(lblStatus, strLang["ERROR_MAXSIZE_EXCEEDED"].ToString());
return false;}
if
(strAllowedExtensions.Contains(Right(objFileUpload.FileName, 3).ToUpper) = false);
{
objFileUpload = null;
lblStatus = SetError(lblStatus, strLang["ERROR_INVALID_FILETYPE"].ToString());
return false;
}
try
{
objFileUpload.SaveAs(strFileName);
}
catch (Exception ex)
{
lblStatus = SetError(lblStatus, string.Format(strLang["ERROR_SYSTEM_SAVE"].ToString(), strContact, ex.Message));
return false;
}
'Cannot implicitly convert type 'object' to 'system.web.ui.webcontrols.label.
How do I cast object to a webcontrol
Brett
gm64
I do not understand what you are trying to do when you say
intMaxUploadSize < 1024;
and
intMaxUploadSize >= 1048576;
Well, I think you wanted to chek this as case so you might like to do following (as boban.s said)
private string FormatMaxSize(int maxUploadSize)
{
if (maxUploadSize < 1024)
return maxUploadSize + strLang["DISPLAY_BYTE"];
else if (maxUploadSize < 1048576)
return maxUploadSize/1024 + strLang["DISPLAY_KBYTE"];
else
return maxUploadSize/1048576 + strLang["DISPLAY_MBYTE"];
}
erzfezsf
private string FormatMaxSize(int maxUploadSize)
{
if (maxUploadSize < 1024)
return maxUploadSize + strLang["DISPLAY_BYTE"];
else if (maxUploadSize < 1048576)
return maxUploadSize/1024 + strLang["DISPLAY_KBYTE"];
else
return maxUploadSize/1048576 + strLang["DISPLAY_MBYTE"];
}
Is this source that you translate really exists and works. I doesn't seems to me that VB code works something.
R1ZWAN
You might like to have following in your code
if
(objFileUpload.FileBytes.GetLength(0) > intMaxUploadSize)
{
objFileUpload = null;
lblStatus.Text = SetError(lblStatus, strLang["ERROR_MAXSIZE_EXCEEDED"].ToString());
return false;
}
if
(strAllowedExtensions.Contains(Right(objFileUpload.FileName, 3).ToUpper) == false);
{
objFileUpload = null;
lblStatus.Text = SetError(lblStatus, strLang["ERROR_INVALID_FILETYPE"].ToString());
return false;
}
try
{
objFileUpload.SaveAs(strFileName);
}
catch (Exception ex)
{
lblStatus.Text = SetError(lblStatus, string.Format(strLang["ERROR_SYSTEM_SAVE"].ToString(), strContact, ex.Message));
return false;
}
I do not know what are the parameters for SetError() method.... if the error is there, you need to let me know the signature of SetError()
Eddie Garcia
Whoops, seems I have written something wrong while I was just trying to convert your code. Well, youcan use the code provided by boban.s
private string FormatMaxSize(int maxUploadSize)
{
if (maxUploadSize < 1024)
return maxUploadSize + strLang["DISPLAY_BYTE"];
else if (maxUploadSize < 1048576)
return maxUploadSize/1024 + strLang["DISPLAY_KBYTE"];
else
return maxUploadSize/1048576 + strLang["DISPLAY_MBYTE"];
}
Hope that helps you. Do let me know if you still find any issues.
Jonathan Hseu
That worked but threw up some more errors for that line:
GenerateFileSpec = string.Format(strLang["INFO_FILE_SPEC"].ToString(), strSpec.ToString(), FormatMaxSize());
The best overloaded method match for 'string.Format(string, object, object)' has some invalid arguments (string is underlined) and
Argument '3': cannot convert from 'void' to 'object' (FormatMaxSize) is underlined.
Any ideas
Brett
TRACEYMS
void FormatMaxSize()
change it to something like
object FormatMaxSize() // just replace the object to a more specific type like string, int, etc.